Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android R.java file

Tags:

android

Static id in R.java files are generated automatically but can i give then custom values to make my work easier. I have 8 Imagebuttons i need to set images on them by using this code for every button.

ImageButton button4 = (ImageButton)findViewById(R.id.iButton4);
setImagesOnButtons(myContactList.get(3).getPhotoId(),button4);

instead of doing this can i change ids of button in R.java to 1,2,3... and put the above code in a for loop like this

 for(i=0;i<8;i++)
 {
ImageButton button4 = (ImageButton)findViewById(i);
setImagesOnButtons(myContactList.get(3).getPhotoId(),i);
} 
like image 515
Abhay Kumar Avatar asked Aug 10 '26 12:08

Abhay Kumar


1 Answers

EDIT: Sean Owen's answer is nicer and more compact than this one.

You could keep a map from your internal values to the unique IDs in R.java. You only need to do this once, on startup:

static final Map<Integer,Integer> buttonMap = new HashMap<Integer,Integer>();

...
buttonMap.put(4, R.id.iButton4);
buttonMap.put(3, R.id.iButton3);
...

Then you can have your loop like this:

for(i=0;i<8;i++)
{
    ImageButton button = (ImageButton)findViewById(buttonMap.get(i));
    setImagesOnButtons(myContactList.get(3).getPhotoId(),i);
} 
like image 151
Graham Borland Avatar answered Aug 13 '26 03:08

Graham Borland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!