views:

37

answers:

1

There are 2 parts to my question but both are related. I have searched all over the place but cannot find a way to put in an array, all file names from R.raw, such as MP3s in string format.

I also want to create new buttons on the fly in Java, as opposed to using the XML files to lay out buttons.

psuedocode:

array[] = put all file names from R.raw into this array with file name;

count = count num of rows in array[];

//I want to be able to do this with that array for (int i = 0; i < count; i++) { create new button and assign it a sound(onclick); this.button should be placeable anywhere i want without using XML for layout }

Thanks in advance to any help anyone can provide!

+2  A: 

Short answer: You can't.

Long answer: R.raw and the likes are generated at build time, thus static in your code (you can't dynamically add entries to these objects from your app). Basically, R is just a class that looks something like:

package com.yourapp;
public class R {
    public class raw {
       public static final int file_1 = 123456; // where 123456 is the address where that file will be found
       public static final int file_2 = 789012;
       // ...
    }
}

Thus, there's no way (that I know of) of getting an array of these attributes.


I recommend you use the assets/ directory instead of R.raw. This way, you'll be able to use AssetManager.list() to get a list of files (and the other AssetManager functions for actually working with the files).

Felix
Thank you, sir. Forgive me for being an amateur. Just starting to learn Java.
s5Android
I also found a great example here.http://thedevelopersinfo.com/2009/11/17/using-assets-in-android/
s5Android
@s5Android: If you consider this the correct answer to your question, please click the checkmark to its left.
Felix