Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Change button color when clicked

Tags:

java

android

Basically, I'm trying to create a button that when clicked (note: NOT pressed) will change color from color1 to color2. When clicked again, it will change back from color2 to color1.

I have searched like crazy and the only information I managed to extract was how to change color when the button is pressed, that is, when the user holds down the button (this code will be written below). However, I want the color to change when the user clicks (presses and releases) the button, and then change back once the user clicks again.

This file is in res/drawable

<!-- Changes color when user hols down button -->
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">
    <shape>
        <!-- change to color2 -->
    </shape>
</item>

<item>
    <shape>
        <!-- change to color1 -->
    </shape>
</item>
</selector>
like image 969
hokosha Avatar asked Apr 20 '15 20:04

hokosha


People also ask

How can change background color of button in android?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.

How do I change the color of a button?

All style elements in your HTML button tag should be placed within quotation marks. Type background-color: in the quotation marks after "style=". This element is used to change the background color of the button. Type a color name or hexadecimal code after "background-color:".

What is the default button color in android?

Show activity on this post. When I open a new android studio project, the default color for button is purple.

How to change color of button in Android when clicked using Kotlin?

This example demonstrates how to Change color of Button in Android when Clicked using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How do I change the background of a button in Android?

1 Set onClick () attribute with a function name android:onClick=”changeBackground”, 2 After that in your activity that hosts this layout create a function with the same name, or 3 You can instead of using the onClick () attribute directly set the onClickListener () and code its function More items...

How to reset background color when other button is clicked?

Button button = findViewById (R.id.button); button.setOnClickListener (new View.OnClickListener () { @Override public void onClick (View v) { v.setBackgroundColor (Color.parseColor ("#ff0000")); } }); If you want to reset color when the other Button is clicked, you can use a common OnClickListener among the Buttons.

How do I add color to my Android app?

Open the colors.xml file by navigating to the app -> res -> values -> colors.xml Create a color tag inside the resources tag with a name and set a color with its hex code. Add the below lines inside the colors.xml file. Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.


1 Answers

boolean tmp = false;
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
         tmp = !tmp;
         v.setBackgroundColor(tmp ? Color.RED : Color.BLUE);
    }
});

EDIT: apparently you want to have a more complex example

First create a drawable XML in and name it pink_button.xml and place the following code inside

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="#FF5EF1"/>
    <corners android:radius="15dp"/>
    <stroke
        android:width="1dp"
        android:color="#303030"/>

</shape>

Now make a blue_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="#008DFF"/>
    <corners android:radius="15dp"/>
    <stroke
        android:width="1dp"
        android:color="#303030"/>

</shape>

Now make some demo activity layout, I used button_demo_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/btnDemo"
        android:layout_width="150dp"
        android:layout_height="30dp"
        android:layout_centerInParent="true"
        android:layout_marginTop="100dp"
        android:background="@drawable/pink_button"
        android:gravity="center"
        android:text="PINK"
        android:textColor="@android:color/white"
        android:textSize="15sp"/>

</RelativeLayout>

And finally the activity, name it whatever you want I used ButtonDemoActivity

public class ButtonDemoActivity extends Activity {


    private Button btnDemo;
    private boolean isPink = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_demo_activity);

        btnDemo = (Button) findViewById(R.id.btnDemo);
        btnDemo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isPink = !isPink;
                int resId = isPink ? R.drawable.pink_button : R.drawable.blue_button;
                btnDemo.setBackgroundResource(resId);
                btnDemo.setText(isPink ? "PINK" : "BLUE");
            }
        });
    }
}

And this is what the final look of the buttons will be in each state enter image description here

like image 63
Bojan Kseneman Avatar answered Nov 15 '22 09:11

Bojan Kseneman