5

I cannot seem to change the background image of my image button. Heres the code i'm currently trying to use:

ImageButton imgButton = (ImageButton) findViewById(R.id.showSportsButton); imgButton.setBackgroundResource(R.drawable.tab2_selected); 

However this seem to be placing the new image on top of the old image leaving me with 2 images overlapping each other.

Does anyone know why this is??

3
  • I guess you'll have to use .setImageResource inherited from ImageView Commented Sep 4, 2013 at 11:01
  • please add imgButton.setImageResource(null); imgButton.setImageResource(R.drawable. tab2_selected); Commented Sep 4, 2013 at 11:13
  • What's the old image - the default one, meaning the grey (for example, depending of the Theme) imagE? Commented Sep 4, 2013 at 11:14

4 Answers 4

10

For solving this question you should implement

imgButton.setImageResource(R.drawable. tab2_selected); 
Sign up to request clarification or add additional context in comments.

Comments

1

Use this method:

imgButon.setBackground(getActivity().getDrawable(R.drawable.your_icon)); 

Comments

0

in xml file, <Button> write: android:backgroud="@drawable/your_file"

Comments

0

Make sure you are on same activity. If you are changing background of different activity first create the constructor then use object to change button.

Activity obj= new activity(); obj.imgButton.setBackgroundResource(R.drawable.tab2_selected); 

and also check that oncreate() method has void return type if you are using only findviewbyid.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.