Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change color of Button in Android when Clicked?

Tags:

android

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?

like image 242
Android_programmer_camera Avatar asked Oct 07 '10 13:10

Android_programmer_camera


People also ask

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 default color of button in android?

When I open a new android studio project, the default color for button is purple.


2 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. Hope this helps.

like image 102
methode Avatar answered Oct 04 '22 20:10

methode


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> 
like image 35
3 revs, 2 users 99% Avatar answered Oct 04 '22 21:10

3 revs, 2 users 99%