CheckingButton Background Resource Id android

13,738

Solution 1

If you are setting multiple drawable backgrounds for a button then you check it as follows

    if(button.getBackground().getConstantState()==getResources().getDrawable(R.drawable.name1).getConstantState())
{
//Your code
}
else if(button.getBackground().getConstantState()==getResources().getDrawable(R.drawable.name2).getConstantState())
{
//Your code
}
else
{
//Your code
}

You can use button.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.name1).getConstantState()) if above code doesn't work

Solution 2

It seems not to be possible. The ressource is resolved to a Drawable and thats all you can get back in the standard functionality. Maybe there is a way to resolve the drawable back to the id in another way, but this functionality is not impleneted in the buttonclass.

If you need an easy access to that resourceId and you are setting the ressource from code you can write your ButtonClass implementing the Android Button and overload/create the setBackgroundResource to save the id in an additional field you can then access. This, of course, doesn't work, if the button gets his BackgroundRessource not due a function call from your side.

Here some code.

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;

public class MyButton extends Button {
    private int bgId;

    public MyButton(Context context) {
        super(context);
    }
    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void setBackgroundResource(int resId) {
        super.setBackgroundResource(resId);
        bgId = resId;   
    }

    public int getBackgroundId() {
        return bgId;
    }
}

Solution 3

i solved this problem by using hashmap. after a lot of wasting time of searching about how to get background resources. when i want to set background resources but also i don't want to lose what i set, i made something like that :

HashMap<Button,Integer> hashMap;
myButton.setBackgroundResources(R.drawable.my_image);
hashMap.put(myButton,R.drawable.my_image);

now if do you want to compare that image with another one :

if(hashMap.get(myButton)==R.drawable.my_image)
{
 myButton.setBackgroundResources(R.drawable.another_image);
 hashMap.put(myButton,R.drawable.another_image);
}

Remember: if did you put another resource with existing key ,the value will be overwritten, dont forget to initialize hashmap before it's being used that's all hope this help you.

Share:
13,738
Christina
Author by

Christina

Updated on June 08, 2022

Comments

  • Christina
    Christina almost 2 years

    I have a button

    Button button = (Button)findViewById(R.id.button);
    button.setBakgroundResource(R.drawable.icon);
    

    If i want to check which background resource button has, is it possible to do so ? how.

    For example :

    if (button.getResourceId()==R.drawable.icon)
    

    do something...

    UPDATE: THE CONDITION IS FALSE I WANT IT TO BE TRUE THE IMAGES DOES NOT MATCH

    vi.findViewById(R.id.button1).setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                v.findViewById(R.id.button1).setBackgroundResource(R.drawable.boutton_off);
    
                Drawable aImg = (Drawable)v.findViewById(R.id.button1).getBackground();
                Drawable bImg = v.getResources().getDrawable(R.drawable.boutton_off);
    
                if(aImg==bImg){
    
                    System.out.println("On");
    
                }else 
                    System.out.println("off");
    
    
    
                //main.popMessage(position);
    
    
            }
    
        }); 
    
  • njzk2
    njzk2 about 11 years
    so wrong. you are comparing an id from a button to the value of a drawable identifier ? never going to be equal, or at least not in any case you'd want it to.
  • Analizer
    Analizer about 11 years
    I think the problem might be with "==", try "equals" instead. And also, I think v.getResources() won't work this time, as it returns the resources related to the view, not the context. Try context.getResources() instead.
  • Christina
    Christina about 11 years
    can you provide me with a sample please
  • Analizer
    Analizer about 11 years
    I guess, you are doing all this in an activity (e.g. MainActivity), so use MainActivity.this.getResources.getDrawable(R.drawable.boutto‌​n_off); and the if statement shell be like if(aImg.equals(bImg))
  • AndiM
    AndiM about 11 years
    It works fine for me to change the background of button..@njzk2
  • njzk2
    njzk2 about 11 years
    it is a very bad practice. drawables and ids are different things and are not supposed to be mixed up. You can declare your own ids (without adding @+id in the layout) using the res/values/ids.xml file, and use these, but don't use the drawable values for ids. They represent different things
  • Nihal
    Nihal about 7 years
    getResources().getDrawable(R.drawable.example) is deprecated in latest sdk. Instead use this ContextCompat.getDrawable(mContext, R.drawable.example))
  • Nihal
    Nihal about 7 years
    @Dasser Basyouni can you provide more info like OS version, how you are implementing etc. I'll have a lookout. Tested this on my 4.4 KK & 7.1 N roms. Working fine for me.
  • Dasser Basyouni
    Dasser Basyouni about 7 years
    I have tested it on 5.1.1 Huawei 7D-501 Beta B110 ROM, and I have implemented it exactly like your first comment, and to be more sure of my result I make it a Logcat and it displays 2 different outputs for the same drawable: android.graphics.drawable.BitmapDrawable$BitmapState@1011644‌​c And android.graphics.drawable.RippleDrawable$RippleState@2bf0c95
  • Nihal
    Nihal about 7 years
    Okay I'll check and get back
  • Nihal
    Nihal about 7 years
    Meanwhile try to use deprecated method and check. I had used that one.
  • Dasser Basyouni
    Dasser Basyouni about 7 years
    also the same I have tried it before, also == and .equal(), I don't know whats wrong
  • Nihal
    Nihal about 7 years
    Tested with different versions of Android OS from JB 4.1 to N 7.1.2. The above method fails to work in all LP 5.x versions. Other versions work fine. Trying to find a workaround.....