Original CheckingButton android resource id

I have a button

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

If I want to check which resource background button is there, can this be done? as.

For instance:

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

to do something...

UPDATE: STATE FALSE I WANT TO BE TRUE, IMAGES ARE 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); } }); 
+4
source share
7 answers

It seems to be impossible. The resource is allowed for Drawable, and that’s all you can return to standard functionality. There may be a way to allow reverse return to id differently, but this functionality is not implemented in the button class.

If you need easy access to this resource and you install the source code from the code, you can write ButtonClass implementing the Android button and reload / create setBackgroundResource to save the identifier in an additional field that you can access. This, of course, does not work if the button receives its BackgroundRessource not because of a function call on your part.

Here is the 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; } } 
+3
source

If you set multiple hand-drawn backgrounds for a button, you test them 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 the above code does not work

+2
source

I solved this problem using hashmap. after spending a lot of time looking for how to get background resources. when I want to install background resources, but also I do not want to lose what I installed, I did something like this:

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

now if you want to compare this image with another:

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

Remember: if you put another resource with an existing key, the value will be overwritten . Do not forget to initialize hashmap before it is used that everyone hopes that this will help you.

+1
source

I think getBackground () is what you are looking for. It returns 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)){ .... } 
0
source
 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); 
0
source

You can do something like this .. 1. If there is any background that you set for your button, also set this background as the button Id .Means button.setId(R.drawable.image);

2. Then check the condition.

 int temp = button.getId(); if(temp == R.drawable.image){ //Do something... } 

Hope this helps you ...

-1
source
 Drawable aImg = (Drawable)done.getBackground(); Drawable bImg = getResources().getDrawable(R.drawable.ic_launcher); if(aImg==bImg){ } 
-1
source

All Articles