13

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?

A--C
  • 36,351
  • 10
  • 106
  • 92
Jean-Marc Astesana
  • 1,242
  • 3
  • 16
  • 24

2 Answers2

10

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>
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Thanks a lot for your research, but I'm not looking for which color is the title in Holo theme. I try to create a generic widget that adapts itself to the application's current theme. Nevertheless, looking at Android source code seems to me a very good idea. I will investigate. – Jean-Marc Astesana Sep 02 '13 at 11:51
  • 3
    @Jean-MarcAstesana Sorry if I gave you the impression "look, I found the color you're looking for". I thought I was telling you where to look. For instance, if you look at `dialog_title_holo.xml`(which is a layout file), you will get some pointers as to how android achieves the View you see in the default dialog. The `blue line` is defined exactly the way as above, except that the property `android:background="#ff33b5e5"` is `android:background="@android:color/holo_blue_light"`. – Vikram Sep 02 '13 at 11:59
  • @user255882 Ok, I think (hope) I understand. I had a look at Android-18, and it seems different. So, I understand there's no magic style id for the whole title layout that can be applied to title component to have the application look and feel. The best I can do is to use style="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle" for the title field (on devices after API14, before this style does not exist). Isn't it? – Jean-Marc Astesana Sep 02 '13 at 15:20
  • @Jean-MarcAstesana `The best I can do is to use style="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle" for the title field`. Correct. `(on devices after API14, before this style does not exist). Isn't it?` Yes. Lets say your minSdkVersion is v13. You can specify your own style based on `@android:style/TextAppearance.DeviceDefault.DialogWindowTitle` in `res/values-v13/styles.xml`. And then, create `res/layout-v13/some_layout.xml` where you use your own created style. It takes quite a bit of work to get this right. – Vikram Sep 02 '13 at 16:34
  • Thanks a lot. I will follow your advice and provide user of my dialog a method to specify their own title layout if the built-in isn't the one they want. – Jean-Marc Astesana Sep 02 '13 at 17:00
  • @user2558882: This has helped me greatly. It is exactly the type of stuff I was after but this is coming from a xml stand point? Do you know how I can reference these properties at runtime? I know there is context.obtainStyledAttributes(attrs) but I do think this helps me in this current situation. I basically need to rip android:paddingStart and android:paddingEnd values from dialog_title from whichever the current theme for the dialog is. (During its creation). I know I can't read the XML's during runtime, so I am a little lost. – WORMSS Sep 26 '13 at 14:15
  • @WORMSS Thanks man. This is how you will find the title TextView: `TextView tv= (TextView) dialog.findViewById(android.R.id.title);`. And this is how you will get `paddingStart` value: `tv.getPaddingStart()`. Similarly, `tv.getPaddingEnd()` will get you the `paddingEnd` value. – Vikram Sep 26 '13 at 20:09
  • I am at the "dialog builder" stage. The Dialog is not yet created. – WORMSS Sep 27 '13 at 10:14
  • @WORMSS In that case, the only workaround I can think of is: create the dialog using `AlertDialogBuilder#create()`. Use the method from my previous comment to get padding values. Use those values (if you need to) with your AlertDialogBuilder. Then create the dialog _again_ before calling `AlertDialog#show()`. – Vikram Sep 28 '13 at 10:00
  • @user2558882 that sounds like a plan. I might make it a singleton based so only has to be done the once.. Thank you very much. – WORMSS Sep 28 '13 at 10:31
1

If you just want to use the device default, without needing to customize or extend anything, you can simply add:

android:textAppearance="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle"

to the title view in your xml layout. This will show a different style based on the devices theme.

If you ALWAYS want to show the blue (Holo Theme) title, regardless of the device defaults, then you can add:

android:textAppearance="@android:style/TextAppearance.Holo.DialogWindowTitle"

instead.

Parker
  • 8,539
  • 10
  • 69
  • 98