简体   繁体   中英

Android Change background color when clicked

I'm trying to change the background color of a Button when it is clicked, I can change the background color when I have the Button pressed down when I release the Button the button returns to its default color. I'm not sure where to go from here I have been trying to find a solution to this problem.

`

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.game.dice.MainActivity">

    <Button
        android:id="@+id/but1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:background="@drawable/button_color"
        />
    <Button
        android:id="@+id/but2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:background="@drawable/button_color"/>
</LinearLayout>

`

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@color/button_pressed"/>
<item android:state_focused="true"
    android:drawable="@color/button_focused"/> 
<item android:drawable="@color/button_default"/> 
</selector>

Here is a quick setup in Java on how to do it;

private Button mButton;
mButton = (Button) findViewById(R.id.but1);

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mButton.setBackgroundColor(Color.BLACK);
        }
    });

You basically make a ClickListener than reacts whenever the button is clicked. From here we change the buttons background color to Black. If you want it to change with each click, you can apply more logic.

In your drawable, the last <item> indicates the default state. Change that.

Edit:

I'm sorry i undestood wrong. You have to do this in Java or Kotlin whichever you are using. This is more business logic than it is "view".

Try like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="@color/button_pressed" />
<item android:state_focused="true" android:state_pressed="true" android:color="@color/button_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:color="@color/button_default" />
<item android:color="@color/button_pressed" />

Try this one


<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/on_the_go"
    android:color="@color/colorText" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/on_the_go_selected"
    android:color="@color/colorSkyBlue"/>

<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/on_the_go"
    android:color="@color/colorText"/>
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/on_the_go_selected"
    android:color="@color/colorSkyBlue"/>

<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/on_the_go"
    android:color="@color/colorText"/>
<item android:state_pressed="true" android:drawable="@drawable/on_the_go_selected"
    android:color="@color/colorSkyBlue" />

</selector>

Happy to help you

Did you try this ?

myButton.setBackgroundColor(Color.RED);

This code is in your onClick method.

try this

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.colorBlack));
            }
        });

NOTE if you wan't to reset a button's background color to default than use below code

button.setBackgroundResource(android.R.drawable.btn_default);

这是因为默认情况下,当触摸按钮时,它将单击而不是聚焦。如果要使按钮聚焦并在按下时更改其颜色,请将其添加到xml中的按钮中。

android:focusableInTouchMode="true"

The correct xml is :

`<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/button_pressed"/>
<item android:state_focused="false"
android:drawable="@color/button_focused"/> 
<item android:drawable="@color/button_default"/> 
</selector>`

you can't focused all true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM