Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to make a ListView that looks similar to Preferences?

Tags:

android

I do not want to use the framework provided Preferences, rather I would like to create a ListView that looks similar. In particular, I would like it to use the same font size and style for the TextViews.

like image 206
ab11 Avatar asked Apr 13 '11 21:04

ab11


2 Answers

This is not about the ListView itself, but rather the child views that appear inside the ListView. They are created by the getView method of your adapter.

To create views similar to Android, you can use the Android source code, specifically the relevant XML file layouts. For example, preference.xml looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:maxLines="4" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />

</LinearLayout>

You will not be able to use this content directly, as some of the constants used are private to Android, and you'll have to further dig down through other xml's.

Anyhow, you should take into account that Android preferences look different on different versions of Android, and on different themes, so make sure you use the constants provided by Android, and not your own hard-coded values, to make sure your list-view items resemble the actual preferences provided by Android.

like image 95
Lior Avatar answered Sep 22 '22 04:09

Lior


Add this attribute to the activity tag in your AndroidManifest.xml

 android:theme="@android:style/Preference.PreferenceScreen"
like image 37
Jim Blackler Avatar answered Sep 21 '22 04:09

Jim Blackler