2
votes

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>
2
For TextView : <TextView android:id="@android:id/title" style="?android:attr/windowTitleStyle" ... /> and for ListView : `<ListView android:id="@android:id/list" style="?attr/preferenceListStyle" .../>. Try and let me know whether it works.Manish Mulimani
Thanks! but style="?android:attr/windowTitleStyle" is not the default style of dialog title of listPreferenceHelloCW
I think it is style="?attr/preferenceFragmentStyle"/ for listviewAK42

2 Answers

1
votes

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.

0
votes

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