1

I'm trying to compare between button background to resource file. For exmaple, I have button

BUTTON

        <Button
        android:id="@+id/btnGenre"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/frame_genre"
        android:text="POST"
        android:padding="16dp"
        android:focusable="true"
        android:clickable="true"/>

The background has drawable/frame_genre. I'm trying to compare this background with Resouce file.

For example

Drawable btnBackground = holder.btnGenre.getBackground();
if(btnBackground.getConstantState() == (getResources().getDrawable(R.drawable.frame_genre).getConstantState())){
                   Toast.makeText(context, "WORKED", Toast.LENGTH_SHORT).show();
               }

Well this if statement check what background the button has. If its frame_genre then it should continue to the block.

In my case the background Button is frame_genre and the if statement should be TRUE. But for some reason when i log the Button background Log.d(TAG, btnBackground.getConstantState().toString()); and the frame_genre Log.d(TAG,getResources().getDrawable(R.drawable.frame_genre).getConstantState())

It printing two diffrent memory locations.

I cant understand why, after all, I'm pointing to the same file which is frame_genre. Can anyone explain to me whats the diffrent?

And I'm looking for a way to check the Button because i want to change its background once it clicked.

E.Bolandian
  • 553
  • 10
  • 35

1 Answers1

1

You need to create selector in drawable it's a xml file. Give your different images as drawable for the state like pressed, normal or selected, etc

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btnGenre_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/btnGenre_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/btnGenre_normal"></item>

</selector>

Give this xml as background to your button.

        <Button
    android:id="@+id/btnGenre"
    android:layout_width="110dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:background="@drawable/selector_xml_name"
    android:text="POST"
    android:padding="16dp"
    android:focusable="true"
    android:clickable="true"/>

Programatically write below code on clickListner on your btnGenre,

if (Objects.equals(btnGenre.getBackground().getConstantState(), this.getResources().getDrawable(R.drawable.btnGenre_selected).getConstantState()))
{
   star.setBackground(R.drawable.btnGenre_normal);
}else {
  star.setBackground(R.drawable.btnGenre_selected);
}
Prashant.J
  • 739
  • 7
  • 12
  • Alright, and there is a way so i can check programmlly if the current Button has a `btnGenre_selected` or `btnGenre_pressed`? **EDIT** I should use `(getResources().getDrawable(R.drawable.frame_genre).getConstantState()` right? – E.Bolandian Apr 15 '20 at 15:17
  • If you want to achieve it programmatically then yes you need to use ConstantState of drawable. I will update my answer – Prashant.J Apr 15 '20 at 15:23
  • Thank you very much, I really appreciate it – E.Bolandian Apr 15 '20 at 15:26
  • Still not working. The `if` statement return always false. Its still pointing to diffrent memory location. The button background and the `getResources()` – E.Bolandian Apr 15 '20 at 15:54
  • Well i dont think it will work becuase this Button, I'm creating in a Recycler view. I have 9 Buttons, so i think each Button gets its own location memory. But still it doesn't make any sense becuase I have only one Drawable file and i point each Button to that Drawable file. I think I'm missing here something – E.Bolandian Apr 15 '20 at 16:22