I am working on Android Application. I want to have 4 buttons to be placed horizontally at the bottom of the screen. In these 4 buttons 2 buttons are having images on them. The border of the buttons should be black color and the border should be as thin as possible. When I click the button, I want the background of the button should be changed to blue color without the color of border to be changed and should be remained in that color for some time. How can I achieve this scenario in Android?
- Possible duplicate of How to change color of vector drawable path on button click I flagged it as duplicate, you can check my answer here: stackoverflow.com/a/55205149/3763032mochadwi– mochadwi2019-03-18 02:21:17 +00:00Commented Mar 18, 2019 at 2:21
- Add android:background="?android:attr/selectableItemBackground" . Vote my answer below thanksAshraf Amin– Ashraf Amin2022-08-01 04:40:51 +00:00Commented Aug 1, 2022 at 4:40
16 Answers
One approach is to create an XML file like this in drawable, called whatever.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/bgalt" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/bgalt" /> <item android:drawable="@drawable/bgnorm" /> </selector> bgalt and bgnormare PNG images in drawable.
If you create the buttons programatically in your activity, you can set the background with:
final Button b = new Button (MyClass.this); b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever)); If you set your buttons' style with an XML, you would do something like:
<Button android:id="@+id/mybutton" android:background="@drawable/watever" /> And finally a link to a tutorial.
8 Comments
Save this code in drawable folder with "bg_button.xml" and call "@drawable/bg_button" as background of button in your xml:
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape> <solid android:color="#004F81" /> <stroke android:width="1dp" android:color="#222222" /> <corners android:radius="7dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <gradient android:startColor="#89cbee" android:endColor="#004F81" android:angle="270" /> <stroke android:width="1dp" android:color="#4aa5d4" /> <corners android:radius="7dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> </selector> 3 Comments
Try This
final Button button = (Button) findViewById(R.id.button_id); button.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_UP) { button.setBackgroundColor(Color.RED); } else if(event.getAction() == MotionEvent.ACTION_DOWN) { button.setBackgroundColor(Color.BLUE); } return false; } }); 1 Comment
Refer this,
boolean check = false; Button backward_img; Button backward_img1; backward_img = (Button) findViewById(R.id.bars_footer_backward); backward_img1 = (Button) findViewById(R.id.bars_footer_backward1); backward_img.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { check = true; backward_img.setBackgroundColor(Color.BLUE); } }); if (check == true) { backward_img1.setBackgroundColor(Color.RED); backward_img.setBackgroundColor(Color.BLUE); } 2 Comments
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- default --> <item android:state_pressed="false" android:state_focused="false"> <shape android:innerRadiusRatio="1" android:shape="rectangle" > <solid android:color="#00a3e2" /> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> </shape> </item> <!-- button focused --> <item android:state_pressed="false" android:state_focused="true"> <shape android:innerRadiusRatio="1" android:shape="rectangle" > <solid android:color="#5a97f5" /> <padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" /> </shape></item> <!-- button pressed --> <item android:state_pressed="true" android:state_focused="false"> <shape android:innerRadiusRatio="1" android:shape="rectangle" > <solid android:color="#478df9"/> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> </shape></item> </selector> Comments
If you want to change the backgorund image or color of the button when it is pressed, then just copy this code and paste in your project at exact location described below.
<!-- Create new xml file like mybtn_layout.xml file in drawable --> <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/pressed" /> <!--pressed --> <item android:drawable="@drawable/normal" /> <!-- Normal --> </selector> <!-- Now this file should be in a drawable folder and use this single line code in button code to get all the properties of this xml file --> <Button android:id="@+id/street_btn" android:layout_width="wrap_content" android:background="@drawable/layout_a" > <!-- your required code --> </Button> Comments
1-make 1 shape for Button right click on drawable nd new drawable resource file . change Root element to shape and make your shape.
2-now make 1 copy from your shape and change name and change solid color. enter image description here
3-right click on drawable and new drawable resource file just set root element to selector.
go to file and set "state_pressed"
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"android:drawable="@drawable/YourShape1"/> <item android:state_pressed="false" android:drawable="@drawable/YourShape2"/> </selector> 4-the end go to xml layout and set your Button background "your selector"
(sorry for my english weak)
Comments
Try this......
First create an xml file named button_pressed.xml These are it's contents.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/icon_1" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/icon_1_press" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/icon_1_press" /> <item android:drawable="@drawable/icon_1" /> </selector> Noe try this on your button.
int imgID = getResources().getIdentifier("button_pressed", "drawable", getApplication().getPackageName()); button.setImageResource(imgID); button_pressed.xml should be in the drawable folder. icon_1_press and icon_1 are two images for button press and normal focus.
Comments
you can try this code to solve your problem
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/login_selected" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/login_mouse_over" /> <!-- focused --> <item android:drawable="@drawable/login" /> <!-- default --> </selector> write this code in your drawable make a new resource and name it what you want and then write the name of this drwable in the button same as we refer to image src in android
1 Comment
public void onPressed(Button button, int drawable) { if (!isPressed) { button.setBackgroundResource(R.drawable.bg_circle); isPressed = true; } else { button.setBackgroundResource(drawable); isPressed = false; } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.circle1: onPressed(circle1, R.drawable.bg_circle_gradient); break; case R.id.circle2: onPressed(circle2, R.drawable.bg_circle2_gradient); break; case R.id.circle3: onPressed(circle3, R.drawable.bg_circle_gradient3); break; case R.id.circle4: onPressed(circle4, R.drawable.bg_circle4_gradient); break; case R.id.circle5: onPressed(circle5, R.drawable.bg_circle5_gradient); break; case R.id.circle6: onPressed(circle6, R.drawable.bg_circle_gradient); break; case R.id.circle7: onPressed(circle7, R.drawable.bg_circle4_gradient); break; } please try this, in this code i m trying to change the background of button on button click this works fine.
Comments
To deal properly for how long you want to have your button stay in your other color I would advise this version:
button.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN: button.setBackground(getResources().getDrawable(R.drawable.on_click_drawable)); break; case MotionEvent.ACTION_UP: new java.util.Timer().schedule( new java.util.TimerTask() { @Override public void run() { ((Activity) (getContext())).runOnUiThread(new Runnable() { @Override public void run() { button.setBackground(getResources().getDrawable(R.drawable.not_clicked_drawable)); } }); } }, BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION); break; default: } return false; } }); BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION indicates after how much time [ms] the button will reset to the previous state (however you might want to use some boolean to check that the button hadn't been used in between, depending on what you want to achieve...).
Comments
Even using some of the comments above this took way longer to work out that should be necessary. Hopefully this example helps someone else.
Create a radio_button.xml in the drawable directory.
<?xml version="1.0" encoding="utf-8"?> <!-- An element which allows two drawable items to be listed.--> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/radio_button_checked" /> <!--pressed --> <item android:drawable="@drawable/radio_button_unchecked" /> <!-- Normal --> </selector> Then in the xml for the fragment should look something like
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioGroup android:layout_width="match_parent" android:layout_height="match_parent"> <RadioButton android:id="@+id/radioButton1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="1" android:button="@drawable/radio_button" android:paddingLeft="10dp" /> <RadioButton android:id="@+id/radioButton2" android:layout_marginLeft="10dp" android:paddingLeft="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:button="@drawable/radio_button" /> </RadioGroup> </LinearLayout> </layout> Comments
hai the most easiest way is this:
add this code to mainactivity.java
public void start(View view) { stop.setBackgroundResource(R.color.red); start.setBackgroundResource(R.color.yellow); } public void stop(View view) { stop.setBackgroundResource(R.color.yellow); start.setBackgroundResource(R.color.red); } and then in your activity main
<button android:id="@+id/start" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="start" android:text="Click"> </button><button android:id="@+id/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="stop" android:text="Click"> or follow along this tutorial