How do I apply a style to all buttons of an Android application
For Android styles, you reference the preset attributes that Android has laid out in R.attr
. In this case, it looks like you want to to reference android:buttonStyle
. I think this would work:
<style name="ApplicationStyle" parent="android:Theme">
<item name="android:buttonStyle">@style/CKButton</item>
</style>
Changing button style in the whole application
For styling your Button
you can use this:
Go to the drawable
folder and make an XML (e.g. button.xml
) file called "style" which contains the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270" />
<stroke android:width="1px" android:color="#000000" />
<corners android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
</shape>
</item>
</selector>
This is my code, you can make the necessary changes you want
Now call it in your layout XML (mainactivity.xml
) like this
android:background="@drawable/button.xml"
Now to change the font color and style you can use the following which comes as part of the styles.xml
in the values folder
<style name="buttonStyle" parent="@android:style/Widget.Button.Small">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">12sp</item>
<item name="android:textStyle">bold</item>
</style>
Now call this in the layout XML (mainactivity.xml
) like this
style="@style/buttonStyle"
The final code is:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="@string/edit"
android:id="@+id/btnEdit"
android:background="@drawable/button.xml"
style="@style/buttonStyle"
/>
Hope this helps :)
How to apply Material Theme to all buttons in application (activity)?
Figured it out. If you're testing on your phisical device and it is set on dark mode, you should make the changes in your values-night/themes.xml
file, otherwise, working on your values/themes.xml
should be enough
Set all button style in styles.xml
Try this style to change for just buttons in the app
<style name="ThemeIndigo" parent="@android:style/Theme.Material.NoActionBar">
<item name="android:buttonStyle">@style/MyButton</item>
</style>
<style name="MyButton" parent="android:Widget.Material.Button">
<item name="android:textSize">19sp</item>
<item name="android:layout_margin">0dip</item>
<item name="android:background">#ff0000</item>
</style>
Related Topics
What Is "Android.R.Layout.Simple_List_Item_1"
Clear Back Stack Using Fragments
Switching Application-Wide Theme Programmatically
Creating an Android Trial Application That Expires After a Fixed Time Period
End Call in Android Programmatically
How to Add a Dropdown Item on the Action Bar
Android Imagebutton with a Selected State
Android Sdk Asynctask Doinbackground Not Running (Subclass)
Creating Temporary Files in Android
Android Studio Says "Cannot Resolve Symbol" But Project Compiles
Difference Between Oncreateview and Onviewcreated in Fragment
Android Gradle Could Not Reserve Enough Space for Object Heap
Is There Any Reason to Use the Support.V4 Library in Android