Sree
Sree

Reputation: 2787

How to capitalize button text pre-Lollipop?

I tried using adding it into styles.xml like so:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Main theme colors -->
        <!--   your app branding color for the app bar -->
        <item name="colorPrimary">@color/primary</item>
        <!--   darker variant for the status bar and contextual app bars -->
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <!--   theme UI controls like checkboxes and text fields -->
        <item name="colorAccent">@color/accent</item>

        <!-- buttons will not have shadows -->
        <item name="android:buttonStyle">@style/Widget.AppCompat.Button.Borderless</item>
        <item name="android:textAppearanceButton">@style/DefaultButtonTextStyle</item>

        <item name="android:colorBackground">@android:color/white</item>

    </style>

<!--The button text style to be used throughout the app-->
    <style name="DefaultButtonTextStyle" parent="@style/Base.TextAppearance.AppCompat.Button">
        <!-- Ensure that the buttons are all caps even pre Lollipop-->
        <item name="android:textAllCaps">true</item>
        <item name="textAllCaps">true</item>
    </style>

Upvotes: 0

Views: 109

Answers (1)

Anirudh Sharma
Anirudh Sharma

Reputation: 7964

You only need to create a custom style for your button

<style name="CustomButton">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:textAllCaps">true</item>
</style>

Then set it to your button like:

<Button 
     android:id="@+id/total_bill"
     style="@style/CustomButton" />

Or you can do something like:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="buttonStyle">@style/MyButton</item>
    </style>

    <style name="MyButton" parent="Widget.AppCompat.Button">
        <item name="android:textAllCaps">true</item>
    </style>

The second method will change it for all Button across the application.One another way is to extend the Button and create a custom Button where textcaps is set to true and use that Button instead of default one.

Upvotes: 1

Related Questions