Reputation: 129
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 with code...
UPDATE: THE CONDITION IS FALSE I WANT IT TO BE TRUE THE IMAGES DO 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);
}
});
Upvotes: 8
Views: 15214
Reputation: 80
as @Nihal has answered is wonderful, however getResources() has now been depricated, in which case you can go straight to getDrawable()
FOR JAVA:
if(button.getBackground().getConstantState()==getDrawable(R.drawable.name1).getConstantState())
{
//Your code
}
else if(button.getBackground().getConstantState()==getDrawable(R.drawable.name2).getConstantState())
{
//Your code
}
else
{
//Your code
}
FOR KOTLIN
if(button.background.constantState==getDrawable(R.drawable.name1)?.getConstantState){
//Your code
}
else if(button.background.constantState==getDrawable(R.drawable.name2)?.getConstantState){
//Your code
}
else
{
//Your code
}
Upvotes: 1
Reputation: 740
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
Upvotes: 10
Reputation: 1
ImageView imageView = (ImageView) v.getTag();
if (hashMap.get(imageView) == R.drawable.hover) {
imageView.setBackgroundResource(R.drawable.hover1);
imageView.setTag(imageView);
hashMap.put(imageView, R.drawable.hover1);
} else {
imageView.setBackgroundResource(R.drawable.hover);
imageView.setTag(imageView);
hashMap.put(imageView, R.drawable.hover);
}
imageView.setScaleType(ScaleType.FIT_CENTER);
Upvotes: 0
Reputation: 2668
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.
Upvotes: 1
Reputation: 947
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;
}
}
Upvotes: 5
Reputation: 488
Drawable aImg = (Drawable)done.getBackground();
Drawable bImg = getResources().getDrawable(R.drawable.ic_launcher);
if(aImg==bImg){
}
Upvotes: -1
Reputation: 2188
You can do something like this..
1.Whatever background you set for your button also set this background as an Id of button.Means button.setId(R.drawable.image);
2.Then check condition..
int temp = button.getId();
if(temp == R.drawable.image){
//Do something...
}
Hope it helps you...
Upvotes: -1
Reputation: 1644
I think getBackground() is what you are looking for. It returns a drawable, so you can check it like this:
Button myButton = ...;
Drawable myDrawable = getResources().getDrawable(R.drawable. ... ); //the one you are looking for
if(myButton.getBackground().equals(myDrawable)){
....
}
Upvotes: 0