2

I have read the article How to customize list preference radio button

I use the following xml lyaout to customize my Dialog, I think that most of default UI of android Dialog is very good, I hope to inherit att, and only change font size.

I have find the default att of CheckedTextView is android:checkMark="?android:attr/listChoiceIndicatorSingle".

Now I hope to find the default android att of both <TextView android:id="@+id/dialog_title" ...> and <ListView android:id="@android:id/list"..>, could you tell me? Thanks!

BTW, the default style of dialog title of listPreference isn't style="?android:attr/windowTitleStyle"

BTW, I can't the same Title effect if I use style="?android:attr/dialogTitle"

a.PNG is the effect of using style="?android:attr/dialogTitle" a.png

b.PNG is the effect of default dialog title of ListPreference b.png

You can try it, use the sample How to customize list preference radio button

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkMark="@drawable/btn_radio_holo_light"
    android:gravity="center_vertical"
    android:minHeight="@dimen/list_item_minheight"
    android:paddingLeft="@dimen/list_item_paddingLeft"
    android:paddingRight="@dimen/list_item_paddingLeft" />



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

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:textColor="@color/title_color"
        android:textSize="22sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/divider_color" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

To Vikram:

  1. android:textColor="@android:color/holo_blue_light" don't work under API 9, how can I set color value for android:textColor? BTW, android:textColor="#ff4d4dff" is wrong.
  2. android:listSelector="?android:attr/selectableItemBackground" don't work under API 9, how can I setfor API 9? At present, I use android:background="@android:color/white". BTW, the default background of API 9 is black.
  3. I have to add android:background="@android:color/white" for LinearLayout android:id="@+id/title_template", because the default background of API 9 is black.

Modified For API 9

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dip"
    android:layout_marginEnd="8dip"
    android:orientation="vertical">

    <LinearLayout android:id="@+id/topPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout android:id="@+id/title_template"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical|start"
            android:minHeight="64dip"
            android:layout_marginStart="16dip"
            android:background="@android:color/white"
            android:layout_marginEnd="16dip">
            <TextView android:id="@+id/dialog_title"
                android:textSize="22sp"
                android:textColor="@android:color/black"                
                android:singleLine="true"
                android:ellipsize="end"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <View android:id="@+id/titleDivider"
            android:layout_width="match_parent"
            android:layout_height="2dip"
            android:visibility="visible"
            android:background="@android:color/black" />
        <!-- If the client uses a customTitle, it will be added here. -->
    </LinearLayout>

    <LinearLayout android:id="@+id/contentPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:minHeight="64dp">

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"  
            />
    </LinearLayout>

</LinearLayout>
Community
  • 1
  • 1
HelloCW
  • 843
  • 22
  • 125
  • 310

2 Answers2

1

You can base your dialog layout on android's:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dip"
    android:layout_marginEnd="8dip"
    android:orientation="vertical">

    <LinearLayout android:id="@+id/topPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout android:id="@+id/title_template"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical|start"
            android:minHeight="64dip"
            android:layout_marginStart="16dip"
            android:layout_marginEnd="16dip">
            <TextView android:id="@+id/alertTitle"
                android:textSize="22sp"
                android:textColor="@android:color/holo_blue_light"
                android:singleLine="true"
                android:ellipsize="end"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <View android:id="@+id/titleDivider"
            android:layout_width="match_parent"
            android:layout_height="2dip"
            android:visibility="visible"
            android:background="@android:color/holo_blue_light" />
        <!-- If the client uses a customTitle, it will be added here. -->
    </LinearLayout>

    <LinearLayout android:id="@+id/contentPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:minHeight="64dp">

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:listSelector="?android:attr/selectableItemBackground" />
    </LinearLayout>

</LinearLayout>

Using this layout, you can customize your dialog the way you want:

Regular(just like default):

enter image description here

Smaller text size:

enter image description here

Different text color and divider color:

enter image description here

To customize, change the text_size attribute of TextView with id alertTitle. You can also change the textColor.

To change the divider color/width, look at the View right beneath the TextView.

Edit:

I'll try and address the three points you have mentioned:

#1. android:textColor="@android:color/holo_blue_light" don't work under API 9, how can I set color value for android:textColor? BTW, android:textColor="#ff4d4dff" is wrong.

Correct: #ff4d4dff is not holo_blue_light. Try using #ff33b5e5.

#2. android:listSelector="?android:attr/selectableItemBackground" don't work under API 9, how can I setfor API 9? At present, I use android:background="@android:color/white". BTW, the default background of API 9 is black.

For API 9, you can use ?selectableItemBackground from the support-v7 appcompat library. If you are already using this support library, change android:listSelector="?android:attr/selectableItemBackground" to android:listSelector="?attr/selectableItemBackground". To know why we're doing this, see this answer: Link.

But if you are not using this library, or don't wish to include this library for only one drawable, you can build selectableItemBackground based on API 17 on your own.

I have assembled the necessary resources for this. Download this zipped folder: Link. In the folder, you will find drawable-XXXX folders. Copy the stuff from these to respective drawable folders in your project. How to use the drawable:

android:listSelector="@drawable/item_background_holo_light"

#3. I have to add android:background="@android:color/white" for LinearLayout android:id="@+id/title_template", because the default background of API 9 is black.

Yes, this looks okay to me.

Community
  • 1
  • 1
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • To Vikram: Thanks! Your code works well in API 17, but I have to modify code for API 9, I havn't tested the code undr other APIs, I don't know if your code can work well under other APIs. I have updated my question, would you please see the snippet "To Vikram" in my question? Thanks again – HelloCW Aug 22 '14 at 01:35
  • @HelloCW You are very welcome. I have added an `Edit` section to my answer above. If you have any more questions, please leave a comment here. – Vikram Aug 22 '14 at 03:58
0

You can actually check there style attributes in the android developers website. You can look at the XML attribute for the default style used in it.

Since ListPreference inherits from DialogPreference they will have the same title style which is ?android:attr/dialogTitle as stated in the website.

For the ListView since you are using the default id of it then you can just call

?android:attr/listViewStyle for the default ListView style.

If you are looking for other class that use default style then developer website is the place to go and look.

EDIT:

Use the default style of android window title from HOLO

<TextView
    android:id="@+id/dialog_title"
    style="@android:style/TextAppearance.Holo.DialogWindowTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="adsdasdasdasd" />
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Thanks! But I can't the same Title effect if I use style="?android:attr/dialogTitle" https://www.dropbox.com/s/31vbjg39xq1ag5o/a.PNG is effect of using style="?android:attr/dialogTitle" https://www.dropbox.com/s/xekgmh0dbocc5yi/b.PNG is effect of default dialog title of ListPreference You can try it, use the sample http://stackoverflow.com/questions/10460715/how-to-customize-list-preference-radio-button/14790665#14790665 – HelloCW Aug 17 '14 at 15:00
  • @HelloCW now that varies on what theme you are using, in the picture it is a light theme you should use the light theme title style from HOLO – Rod_Algonquin Aug 17 '14 at 16:00
  • Thanks! How can I apply the light theme to my dialog title android:id="@+id/dialog_title" – HelloCW Aug 18 '14 at 00:25
  • Thanks! I get the error when I use style="@android:style/TextAppearance.Holo.DialogWindowTitle" @android:style/TextAppearance.Holo.DialogWindowTitle requires API level 13 (current min is 8). I try to use TextAppearance.DialogWindowTitle and TextAppearance.Theme.Dialog, but I can't get the same UI effect. – HelloCW Aug 18 '14 at 06:32
  • @HelloCW That is the only way I can find, ya those wont work, only in HOLO – Rod_Algonquin Aug 18 '14 at 06:39
  • @HelloCW what api are you running those images? – Rod_Algonquin Aug 21 '14 at 01:10
  • The API I'm running is 17 – HelloCW Aug 21 '14 at 01:32
  • And more, it seems that Android system can adjust title style automacically by different API. – HelloCW Aug 21 '14 at 01:34
  • Thanks! I think I was in an error way, I should set style for the customize dialog, could you tell me how to set style for the dialog? – HelloCW Aug 21 '14 at 02:28
  • The following code don't work, the error is the style is not public. – HelloCW Aug 21 '14 at 02:49
  • @HelloCW I think you cant just use a full layout style of the dialog, why not just create a layout for dialog itself dont use any of the default style. just create the style of your own. – Rod_Algonquin Aug 21 '14 at 03:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59702/discussion-between-hellocw-and-rod-algonquin). – HelloCW Aug 21 '14 at 06:38