Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6736393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:07:28+00:00 2026-05-26T11:07:28+00:00

I’m trying to set the images of some ImageButtons in my layout programmatically. To

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:07:28+00:00Added an answer on May 26, 2026 at 11:07 am

    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);
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.