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.
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
Give this xml as background to your button.
Programatically write below code on clickListner on your btnGenre,