Android: Using default style in a custom dialog title

Jean-Marc Astesana picture Jean-Marc Astesana · Sep 2, 2013 · Viewed 15.1k times · Source

I'm trying to create a custom multiple choice alert dialog that allows the user to select/deselect all items in one click. I achieve this using a custom title with an additional checkbox. Everything works fine except that I don't know how to make my custom title looking like the default alert dialog title (using the same style).

Here is what I'm trying to do (The example uses the theme in the Dialogs documentation. That's just an example, what I really try to have is the application theme).

enter image description here

I created a custom view for the custom title I use, but I don't know how to get the attributes of the default style title bar, so, I obtain:

enter image description here

(No blue bar below the title, and wrong title color)

Here is the layout of my custom title:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/title"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Dialog title" />

<TextView
    android:id="@+id/all"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="All" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

It seems obvious to me that I need to define the title attributes, and the background of the layout ... but I'm crawling the web since hours searching how to get the attributes of default title view.

Any idea?

Answer

Vikram picture Vikram · Sep 2, 2013

See if this is what you are looking for:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="22sp"
            android:textColor="#ff33b5e5"
            android:text="Dialog title" />

        <TextView
            android:id="@+id/all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="All" />

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <View android:id="@+id/titleDivider"
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background="#ff33b5e5" />

</LinearLayout>

How I got this:

Browse to the sdk directory on your hard drive > platforms > android-XX(17, for example) > data > res > layout > dialog_title_holo.xml. Look at the View with id titleDivider. It's background attribute: background="@android:color/holo_blue_light". Look up the value of this color in res/values/colors.xml.

From styles_device_defaults.xml:

<style name="TextAppearance.DeviceDefault.DialogWindowTitle" parent="TextAppearance.Holo.DialogWindowTitle" >

Looking at styles.xml:

<style name="TextAppearance.Holo.DialogWindowTitle">
    <item name="android:textSize">22sp</item>
    <item name="android:textColor">@android:color/holo_blue_light</item>
</style>

The textColor is the same as the line color. Text size is specified as 22sp. And, style="?android:attr/textAppearanceLarge" is not required because we are setting the textSize="22sp":

<style name="TextAppearance.Large">
    <item name="android:textSize">22sp</item>
</style>