0

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?

FredericP
  • 1,119
  • 1
  • 14
  • 27
  • 1
    Is your `Activity` using the `AppTheme` theme? – Gabriele Mariotti Oct 06 '20 at 15:05
  • 1
    Thanks! It worked after adding `android:theme="@style/AppTheme"` in section `activity android:name=".MainActivity"'` in manifests/AndroidManifest.xml. I have to quit and restart AndroidStudio to make it work visually in Design mode, but it worked! Can you rewrite as an answer so I can make it the approved solution? – FredericP Oct 17 '20 at 12:15
  • Thanks for the feedback. Answer added. – Gabriele Mariotti Oct 17 '20 at 12:39

2 Answers2

0

You need to make following changes in your AppTheme -

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    ..............
    <item name="android:buttonStyle">@style/MyButton</item>

</style>

That's it. Happy Coding !!

Nikhil Sharma
  • 897
  • 1
  • 4
  • 18
  • You mean in app/res/values/styles.xml? I did the change and it didn't work either... The button I add continue to default to @android:style/Widget.Material.Light.Button, and not @style/MyButton as I expected... – FredericP Oct 04 '20 at 08:52
  • could you change MyButton's Parent theme to Widget.Material.Light.Button while keeping the changes which I suggested – Nikhil Sharma Oct 04 '20 at 08:58
  • check out [this](https://stackoverflow.com/questions/14675657/how-to-apply-style-to-all-buttons-in-android-application/26625931#26625931) link with similar query – Nikhil Sharma Oct 04 '20 at 09:03
  • Sorry, I don't understand anything on the link you give, none of the value I choose for – FredericP Oct 05 '20 at 20:49
0

Make sure that your Activity is using the AppTheme theme. Add android:theme="@style/AppTheme" in section activity android:name=".MainActivity int manifest.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841