E.Bolandian
E.Bolandian

Reputation: 573

Android - compare between button background to Resource

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.

Upvotes: 1

Views: 484

Answers (1)

Prashant.J
Prashant.J

Reputation: 799

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);
}

Upvotes: 1

Related Questions