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?
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:".
When I open a new android studio project, the default color for button is purple.
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 bgnorm
are 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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With