Android: change button color when pressed

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

I searched like crazy, and the only information I managed to extract was the color change when the button was pressed, that is, when the user holds the button (this code will be written below). However, I want the color to change when the user presses (presses and releases) the button and then changes back as soon as 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> 
+6
source share
2 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 an XML shortcut and name it pink_button.xml and put 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 do 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 create a mock demo activity, 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 action, 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 every state enter image description here

+8
source

You are on the right track. Add other states, for example:

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

then in your onClick method add a few switches:

 v.setSelected( !v.isSelected() ); 
0
source

Source: https://habr.com/ru/post/985552/


All Articles