Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android default button styling not working

I'm trying to set up my styles to make all buttons a particular color combination, specifically blue with white text. Here's my main styles.xml:

<resources>
    <style name="CustomTheme" parent="MaterialDrawerTheme.Light.DarkToolbar">
        <!-- various items -->

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

    <!-- a couple of other styles -->

    <style name="ButtonStyle" parent="android:style/Widget.Button">
        <item name="android:textSize">19sp</item>
        <item name="android:textColor">@color/primaryTextContrast</item>
        <item name="android:background">@color/primary</item>
    </style>
</resources>

And in the manifest:

 <application
        android:name=".CustomApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/application_name"
        android:theme="@style/CustomTheme">

color/primary is dark blue, and color/primaryTextContrast is white. On Lollipop, the button looks perfect. On a 4.1 device, it's light gray with black text. Every resource I've found for doing this looks exactly like what I'm doing so I don't know what I'm missing here.

I'm having a similar issue with controlling text size in the base style definition as well.

Update: here are the colors.

<resources>
    <color name="primary">#3F51B5</color>
    <color name="dark">#303F9F</color>
    <color name="accent">#FFCA28</color>
    <color name="background">@android:color/white</color>
    <!-- Color for text displayed on top of the primary or dark color -->
    <color name="primaryTextContrast">@android:color/white</color>
    <!-- Color for text displayed on the background color (which I think will always be white) -->
    <color name="basicText">@color/primary</color>
    <!-- Color for text displayed on the accent color -->
    <color name="accentText">#303F9F</color>
</resources>

Here's v19/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="FullscreenTheme" parent="MaterialDrawerTheme.Light.DarkToolbar.TranslucentStatus">
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

Here's v21:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="CustomTheme">
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>
        <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
        <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    </style>
</resources>

I don't think either of these is what's making it work properly on 5.1.

like image 842
nasch Avatar asked Jun 16 '15 19:06

nasch


People also ask

How to change the style of Buttons in Android?

To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.

How to style Button in Android studio?

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.

What is TextAppearance in Android?

TextAppearance allows you to define text-specific styling while leaving a View 's style available for other uses. Note, however, that if you define any text attributes directly on the View or in a style, those values would override the TextAppearance values.

Where is styles xml in Android studio?

Defining Styles This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file.


2 Answers

Using AppCompat 22.1.+ (22.2.0 should work too), I defined a Style like this:

<style name="MyApp.Button.Red" parent="Base.Widget.AppCompat.Button">
    <item name="colorButtonNormal">@color/primary</item>
    <item name="android:colorButtonNormal">@color/primary</item>
    <item name="android:textColor">@android:color/white</item>
</style>

and then applied the theme in a button using the native theme attribute from android namespace, as said in this awesome post from Chris Banes.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sign_up_button"
    android:theme="@style/MyApp.Button.Red" />
like image 52
Rafael Toledo Avatar answered Nov 02 '22 23:11

Rafael Toledo


I tried adding buttonStyle without the android: prefix and it solved the problem. Yeah, weird.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="buttonStyle">@style/ButtonStyle</item>
    <item name="android:buttonStyle">@style/ButtonStyle</item>
</style>
like image 31
fhucho Avatar answered Nov 02 '22 23:11

fhucho