Greg Bacchus
Greg Bacchus

Reputation: 2466

Android AppCompat AlertDialog styling with theme

I wish to be able to set a theme to set the message text size in an AppCompat AlertDialog. The theme needs to have parent="@style/Theme.AppCompat.Dialog". I have spent hours searching and trying all the suggestions, but none of them seem to work with that base theme.

If the parent is changed to the Holo theme, then I can alter the message text size using textAppearanceMedium, but the rest of the dialog looks really ugly :S

Currently my theme is (all this is currently hooked up and working):

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyDialogTheme" parent="@style/Theme.AppCompat.Dialog">
    <!-- Used for the buttons -->
    <item name="colorAccent">@color/colorPrimary</item>
    <!-- Button text size -->
    <item name="android:textSize">@dimen/ui_text_size</item>
    <!-- Content text color -->
    <item name="android:textColorPrimary">@color/ui_text_color</item>
    <!-- Title style -->
    <item name="android:windowTitleStyle">@style/MyDialogTitleStyle</item>
    <!-- Button style (except size) -->
    <item name="android:textAppearanceButton">@style/MyDialogButtonTextAppearance</item>
    <!-- Dialog background -->
    <item name="android:windowBackground">@color/ui_background</item>
  </style>

  <style name="MyDialogTitleStyle" parent="@style/RtlOverlay.DialogWindowTitle.AppCompat">
    <item name="android:textAppearance">@style/MyDialogTitleTextAppearance</item>
    <item name="android:textSize">@dimen/ui_large_text_size</item>
  </style>

  <style name="MyDialogTitleTextAppearance">
    <item name="android:textSize">@dimen/ui_large_text_size</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:textColor">@color/ui_title_color</item>
  </style>

  <style name="MyDialogButtonTextAppearance">
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textAllCaps">true</item>
  </style>
</resources>

Upvotes: 1

Views: 1224

Answers (1)

reVerse
reVerse

Reputation: 35264

Seems like there is no way to this via a theme attribute. Let's look at the source code of appcompat-v7 library. Following the TextView that reflects the message of the AlertDialog:

<android.support.v7.widget.AlertDialogLayout ... >

    <!-- ... -->

                <TextView
                    android:id="@android:id/message"
                    style="@style/TextAppearance.AppCompat.Subhead"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="?attr/dialogPreferredPadding"
                    android:paddingRight="?attr/dialogPreferredPadding"/>

    <!-- ... -->

</android.support.v7.widget.AlertDialogLayout>

As you can see the TextView uses the TextAppearance.AppCompat.Subhead as the style. Following its definition:

<style name="TextAppearance.AppCompat.Subhead" parent="Base.TextAppearance.AppCompat.Subhead"/>
<style name="Base.TextAppearance.AppCompat.Subhead">
    <item name="android:textSize">@dimen/abc_text_size_subhead_material</item>
    <item name="android:textColor">?android:textColorPrimary</item>
</style>

The textSize is static and isn't resolved via an attribute like the textColor (which uses the textColorPrimary). Thus there's no option for us to set the textSize. The only way to do it would be to override the TextAppearance.AppCompat.Subhead style by adding it to your own styles.xml file and set the textSize to whatever value you need. But be aware there may be side effects since this style can be used in other places as well.

tl;dr

Options:

  1. Define TextAppearance.AppCompat.Subhead in your styles.xml file and override the textSize attribute.
  2. Do it programatically (find TextView by id and #setTextSize)
  3. Use your own layout in the dialog - the source code of appcompat-v7 may be a good starting point.

Upvotes: 5

Related Questions