14

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?

azhar
  • 1,709
  • 1
  • 19
  • 41
Luke Sleeman
  • 1,636
  • 1
  • 16
  • 27
  • Did you try using `dp` instead of `sp`? – Apoorv Feb 05 '14 at 05:30
  • Just tried then - it makes no difference and the compiler gives you a warning that you should use sp for font sizes. – Luke Sleeman Feb 05 '14 at 05:54
  • Maybe for some reason the size you want is too big? see what happens if you use 30sp or something – gilonm Feb 05 '14 at 06:29
  • refer this question http://stackoverflow.com/questions/15909672/how-to-set-font-size-for-text-of-dialog-buttons – Praveena Feb 05 '14 at 06:30
  • Yes, I took a look at that and it seems a good solution if you don't want to control the size from styles.xml. Unfortunately for me I am using the Preferences framework, which builds its own dialogs, so the above solution wont work. For me it seems its styles.xml or nothing ... – Luke Sleeman Feb 05 '14 at 10:33
  • Just to respond to gilonm - yes I have tried using a smaller size, it still doesn't work. – Luke Sleeman Feb 10 '14 at 23:10
  • try android:textAppearance="?android:attr/textAppearanceLarge" and make sure not updating size in layout file. – Sharjeel Feb 10 '14 at 23:14
  • Sharj - Just tried it and it doesn't work. – Luke Sleeman Feb 10 '14 at 23:16
  • you added those other two styles but the one for the button is not used, are you sure you are changing the style to the custom one? – Ayoub Feb 13 '14 at 12:15
  • I know it's been a long time, but did you end up solving this? – arsent Mar 17 '17 at 04:08
  • Unfortunately not. I believe I dug through the code, only to find that the size was hard coded in java! The thing is, we are now using material design + support library, not holo, so things may have changed quite a bit – Luke Sleeman Mar 29 '17 at 13:02

4 Answers4

7

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"?

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • I tried making the change you proposed. Strangely the button lost its background colour, though the textAllCaps still remains in effect. The button font did not become any larger. I don't have any specific java code or XML for the dialog - I want all dialogs throughout my app to appear this way. Some are produced via code but others are generated automatically inside the preferences API – Luke Sleeman Feb 18 '14 at 10:34
  • @LukeSleeman My apologies. The proposed change would work with custom content - when you set a custom layout to the dialog. If you're using the layout provided by the system, the text-size will is fixed in xml. There are ways to circle around that - update text-size after creating the dialog (and before showing it) - you would do this by `findViewById(android.R.id.button1)` in dialog and setting the text-size. Another way is to extend Dialog class and define your own default layout which relies on the theme to supply text-size. Neither of these satisfy your requirements though. – Vikram Feb 18 '14 at 18:17
1

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>
Simon
  • 10,932
  • 50
  • 49
  • 1
    I just tried overriding android:buttonBarButtonStyle and again, I can change the background colour and textAllCaps however the font size does not change. Based on the layout from the framework you have posted above wouldn't it be impossible to change the textSize from styles as the textSize attribute is set in the layout, which will override whatever is coming from style="?android:attr/buttonBarButtonStyle" – Luke Sleeman Feb 18 '14 at 10:57
  • You could try and set the ```android:textAppearance``` attribute in your buttonBarButtonStyle style. I can't remember which takes precedence. There are a few other methods you can apply these changes app-wide. I'll see if I get time later to suggest a few. – Simon Feb 18 '14 at 12:21
0

I know that it is not answer to your problem but why you don't want to style Activity to look like Dialog? I had the same problem like you and styling Android Dialog is really painful. Using Activity you can put whatever you want as xml content and style it as you want. Also you have ActionBar so you can add menu to your Dialog and use custom views in ActionBar. Maybe it is time to change your Android Dialogs to Activity with Dialog style.

Piotr Ślesarew
  • 2,826
  • 1
  • 17
  • 17
  • As I already mentioned I am using a variety of dialogs through my app, including those generated by the preferences API. – Luke Sleeman Feb 18 '14 at 10:38
-2

Try this 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>
and_dev
  • 3,723
  • 1
  • 20
  • 28
Krizzz
  • 1