Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Android Dialog button text size via styles

I am trying to enlarge the text size on all of my applications dialog buttons via styles. The following code will change the buttons background colour and even the text case but for some reason the textSize item is not honoured:

<style name="MyAppTheme" parent="@android:style/Theme.Holo">
    <item name="android:dialogTheme">@style/MyApp.Dialog</item>
    <item name="android:alertDialogTheme">@style/MyApp.Dialog.Alert</item>
</style>

<style name="MyApp.Dialog" parent="@android:style/Theme.Holo.Dialog">
    <item name="android:borderlessButtonStyle">@style/MyApp.BorderlessButton</item>
</style>

<style name="MyApp.Dialog.Alert" parent="@style/MyApp.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

<style name="MyApp.BorderlessButton" parent="@android:style/Widget.Holo.Button.Borderless">
    <item name="android:textSize">50sp</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:background">#800</item>
</style>

Dialog button with background colour and textAllCaps but no text size

Why is the textSize not being read? What do I have to do to make it larger?

like image 452
Luke Sleeman Avatar asked Feb 05 '14 05:02

Luke Sleeman


People also ask

How do I change the size of the button text?

To change the font size of a button, use the font-size property.

Where is shrink text Size button?

Press Ctrl+A to select all the text in your document. Click Home > Shrink Font one or more times. As you click Shrink Font, Word decreases the size of every font in your document.


2 Answers

You are not assigning the right attribute here:

<style name="MyApp.Dialog" parent="@android:style/Theme.Holo.Dialog">
====>    <item name="android:borderlessButtonStyle">@style/MyApp.BorderlessButton</item>
</style>

The attribute you need to use is:

android:buttonStyle

I propose the following change:

<style name="MyApp.Dialog" parent="@android:style/Theme.Holo.Dialog">
    <item name="android:buttonStyle">@style/MyApp.BorderlessButton</item>
</style>

Why:

borderlessButtonStyle is a conveniently defined style. But it isn't currently wired to any of the default attributes - in this case, buttonStyle. You still need to assign it.

I can't figure out why the background and textAllCaps attributes come into effect. Posting your dialog's/alert dialog's xml might help. Also, how have you defined the string - "i agree" or "I AGREE"?

like image 62
Vikram Avatar answered Oct 14 '22 17:10

Vikram


A buttons in a AlertDialog use the android:buttonBarButtonStyle in Holo. Override that attribute in your themes/styles and set the android:minHeight="0dp" to wrap the content, or of course your can provider your own height.

Correction: I mis-read the title. You of course can set the android:textSize="32sp" on that same style to change the text size.

Here is the layout for it from the framework:

<LinearLayout android:id="@+id/buttonPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/alert_dialog_button_bar_height"
    android:orientation="vertical"
    android:divider="?android:attr/dividerHorizontal"
    android:showDividers="beginning"
    android:dividerPadding="0dip">
    <LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layoutDirection="locale"
        android:measureWithLargestChild="true">
        <Button android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:maxLines="2"
            style="?android:attr/buttonBarButtonStyle"
            android:textSize="14sp"
            android:minHeight="@dimen/alert_dialog_button_bar_height"
            android:layout_height="wrap_content" />
        <Button android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:maxLines="2"
            style="?android:attr/buttonBarButtonStyle"
            android:textSize="14sp"
            android:minHeight="@dimen/alert_dialog_button_bar_height"
            android:layout_height="wrap_content" />
        <Button android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_gravity="end"
            android:layout_weight="1"
            android:maxLines="2"
            android:minHeight="@dimen/alert_dialog_button_bar_height"
            style="?android:attr/buttonBarButtonStyle"
            android:textSize="14sp"
            android:layout_height="wrap_content" />
    </LinearLayout>
 </LinearLayout>
like image 23
Simon Avatar answered Oct 14 '22 18:10

Simon