I have defined the below files in res folder.
styles_apptheme.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
<item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
</style>
</resources>
themes_apptheme.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="@style/_AppTheme"/>
<style name="_AppTheme" parent="Theme.AppCompat.Light">
<item name="android:editTextBackground">@drawable/apptheme_edit_text_holo_light</item>
<item name="android:buttonStyle">@style/ButtonAppTheme</item>
</style>
</resources>
In my Layout.xml file, i have defined my button as below
<Button
android:id="@+id/btnAddTitle"
android:layout_below="@id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_AddTitle"
android:layout_margin="20dp"
/>
If i add the below line to the button view above,
android:background="@style/ButtonAppTheme"
Application crashes saying that drawable resource should be set for the background attribute.
So i created the below drawable file - abc.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/apptheme_btn_default_normal_holo_light" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/apptheme_btn_default_disabled_holo_light" />
<item android:state_pressed="true"
android:drawable="@drawable/apptheme_btn_default_pressed_holo_light" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/apptheme_btn_default_focused_holo_light" />
<item android:state_enabled="true"
android:drawable="@drawable/apptheme_btn_default_normal_holo_light" />
<item android:state_focused="true"
android:drawable="@drawable/apptheme_btn_default_disabled_focused_holo_light" />
<item
android:drawable="@drawable/apptheme_btn_default_disabled_holo_light" />
</selector>
If i set android:background="@drawable/abc"
i dont see the style set in the button.
So please let me know how i can set the style for the button.
Thanks.
To add a button, that has an Android style all you need to do is to drag and drop a button from the Palette to your layout. For most versions that would mean a grey button with all corners at 2dp roundness. Check our blog, if you need to learn more about using Android Studio Layout Editor.
We can set custom shapes on our button using the xml tag <shape> . These xml files are created in the drawable folder too. shape can be used inside selectors . The shape can be set to rectangle (default), oval , ring , line .
To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
It would be rather simple if you do it this way.
First create a button_selector.xml in drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="@color/green_temp" />
<gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/green_temp" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="#971E05" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="@color/bright_green" />
<gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/button_green" />
</shape>
</item>
</selector>
Add these colors in colors.xml of values folder.
<!-- Green -->
<color name="green_temp">#23A96E</color>
<color name="green_dark">#159204</color>
<color name="bright_green">#02D8B0</color>
<color name="button_green">#10a54a</color>
Finally in your desired button of layout.xml put background from above selector.
<Button
android:id="@+id/btnAddTitle"
android:layout_below="@id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_AddTitle"
android:background="@drawable/button_selector"
android:layout_margin="20dp"
/>
Then its all done your button will be styled with color you want.
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