Android button button background color after click

Problem: I want to change the background color of a button after clicking it.

This is a simple problem, but I spent a lot of time searching on Google and I cannot find a solution. They all refer to the same XML as shown below.

My XML is as follows:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/black_button_pressed" android:state_focused="true" />
    <item android:drawable="@drawable/black_button_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/black_button_normal"/>
</selector>

So, when a person hangs over a button, it becomes gray. When a person presses a button, the shutter background should be grayed out after clicking, but I don’t see it being grayed out. After printing, it returns to normal.

Do you have any tips and / or pointers to help me?

+5
source share
2 answers

android . , , Up. , , , - , .

btn.setBackgroundResource(R.drawable.btn_selected_blue);

+2

( onClick):

button.setBackgroundResource(R.drawable.button_selected);

.

0

All Articles