3

I'm trying to set the images of some ImageButtons in my layout programmatically. To do so, I named my ImageButtons ic_1 to ic_5.

During my initialization method, I want to loop through these 5 ImageButtons and set their images according to a list of possible icons. Basically users can change the order in which the icons should be shown, which in turn would change the image on the ImageButtons.

However, i can't seem to reference the buttons since they have an Integer in their ID. The code I use for this is as such:

for (int i = 1; i < 2; i++) {

                    String butid = "ic_"+i;
                    int resID = getResources().getIdentifier(butid, "id",
                            getPackageName());
                    ImageButton button = (ImageButton) findViewById(resID);

However, this returns a NullPointerException as resID returns 0. When I just use "ic_1" in butid, it also returns 0. However if I give an ImageButton an ID as ic_one, it does work. But if I'm going to use pure-text id's, I won't be able to loop through the ImageButtons.

First I thought this meant the ID's werent properly translated into the R.java file, but the buttons are present in it, with their respective id's as shown below.

    public static final int ic_1=0x7f05000e;
    public static final int ic_2=0x7f05000f;

Does anyone know if it's seriously not possible to use an int in the ID of a layout object, and if so, if it's possible to loop through the ImageButtons like I want without the need of an integer in the ID? A simple example would be appreciated.


Some more info for Warren:

Layout specification:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainscreen);
    setMenuImage();
    initMainScreen();
}

Full code that should change the icons:

public void initMainScreen() {
    if (standard == false) {
        retrieveLinks();
        int size = mso.getHome().getIcon().size();

        if (size > 0) {
            for (int i = 1; i < 2; i++) {

                String butid = "ic_"+i;
                int resID = getResources().getIdentifier(butid, "id",
                        getPackageName());

                String buttype = mso.getHome().getIcon().get(i)
                        .getIconName();
                System.out.println(buttype.toLowerCase());
                int typeID = getResources().getIdentifier(buttype.toLowerCase(),
                        "drawable", getPackageName());

                ImageButton button = (ImageButton) findViewById(resID);
                if(button != null){
                button.setImageResource(typeID);
                }else{
                    System.out.println(butid+" "+resID);
                }
            }
        }
    }
}

Specification in the layout file: (same goes for all buttons)

    <ImageButton android:layout_height="wrap_content"
        android:layout_weight="33" android:layout_width="wrap_content"
        android:src="@drawable/empty_icon" android:onClick="iconClick" android:background="@null" android:id="@+id/ic_1"></ImageButton>

3 Answers 3

2

It might not be exactly what you were trying to do, but if you are only dealing with 5 buttons, you could always just declare a static array of the the Button Id's and loop through them.

Something like:

private static final int[] buttons =
     {R.id.ic_1, R.id.ic_2, R.id.ic_3, R.id.ic_4, R.id.ic_5};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainscreen);
    setMenuImage();
    initMainScreen();
}

public void initMainScreen() {
    if (standard == false) {
        retrieveLinks();
        int size = mso.getHome().getIcon().size();

        if (size > 0) {
            for (int i = 1; i < 2; i++) {

                String buttype = mso.getHome().getIcon().get(i)
                        .getIconName();
                System.out.println(buttype.toLowerCase());
                int typeID = getResources().getIdentifier(
                        buttype.toLowerCase(), "drawable", getPackageName());

                //Using the array of button id's directly
                ImageButton button = (ImageButton) findViewById(buttons[i]);
                if(button != null){
                button.setImageResource(typeID);
                }else{
                    System.out.println(butid+" "+resID);
                }
            }
        }
    }
}
3
  • This seems like a much better approach.
    – dmon
    Oct 25, 2011 at 14:17
  • Yeah I had this in mind as my backup plan in case I really can't find a fix for my problem, but it would be nice to find a proper fix instead so I can keep it in mind for future usages. Also, the amount of buttons might go up to 30+ in the future and I'm not really looking forward to making a static array all the time. However I might end up going with this answer, since it does seem like the better approach. Oct 25, 2011 at 14:18
  • Ended up going with your answer since my search for a better answer has not revealed anything usefull. Your method works like a charm though! But I still find it weird that it won't work using my method. Oct 25, 2011 at 14:47
2

you are starting with 0, that is the problem.

start the loop from i =1 ;

1
  • My mistake actually, I messed around a bit before posting here. The original loop did have 1 instead of 0, but that didn't work either. Checked again just to be sure, but int i = 1; doesn't do the trick either. Even added a println to make sure the string is actually ic_1 (it is), but it still doesn't work. Oct 25, 2011 at 14:04
1

You start your loop with i = 0 but your ic_1 starts with 1. Change the loop to start with 1, that should do the trick.

3
  • My mistake actually, I messed around a bit before posting here. The original loop did have 1 instead of 0, but that didn't work either. Oct 25, 2011 at 14:03
  • Can you show us how you specify the layout that should be used? Oct 25, 2011 at 14:04
  • Added more info in the opening post. Hopefully that will make things clearer. Oct 25, 2011 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.