Issue
I wanted to have a custom button look, so I followed the answer here https://stackoverflow.com/a/29848987/5616309
- I created xml files for the shape of the different states in app/res/drawable
- I created a xml file for the selector listing the files for all the states in app/res/drawable
This works, I can create a button with the right apparence if I change the property to refer to my custom button.
Now I wanted to make it default, so each button I add on the layout has the good look, automatically. So I changed then file app/res/values/style.xml to be like this (the lines referring to MyButton are the lines I added)
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="buttonStyle">@style/MyButton</item>
</style>
<style name="MyButton" parent="Widget.AppCompat.Button">
<item name="android:background">@drawable/my_button</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textColor">#FFFFFFFF</item>
<item name="android:shadowColor">#FF000000</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">-1</item>
<item name="android:shadowRadius">0.2</item>
<item name="android:textSize">16dip</item>
<item name="android:textStyle">bold</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
It doesn't work. I have to manually add style="@style/MyButton"
to each button to make it work. What have I missed?
Solution
Make sure that your Activity
is using the AppTheme
theme.
Add android:theme="@style/AppTheme"
in section activity android:name=".MainActivity
int manifest.
Answered By - Gabriele Mariotti
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.