トラッキング コード

3/10/2012

Create PreferenceActivity with header and Footer.







We can create PreferenceActivity with header and Fotter.

We shoud make following files and call method.
  1. Create Layout with ListView.
  2. Create Preference XML.
  3. Call setContentView() and addPreferencesFromResource() in onCreate().


Create Layout with ListView

Make layout xml file , to set PreferenceActivity#setContentView().
It must have ListView with android:id="@+id/android:list".

you shoud add android:layout_height="0.0dp"and android:layout_weight="1" to ListView, because to show footer in bottom


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

    <!-- Header  -->

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <!-- Show Preference in ListView  -->

    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0.0dp"
        android:layout_weight="1" >
    </ListView>

    <!-- Footer -->

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Footer"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</LinearLayout>


Create Preference XML

Create Preference XML.
Sample.

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

    <CheckBoxPreference
        android:key="checkbox_preference"
        android:summary="summary_checkbox_preference"
        android:title="checkbox_preference" />

    <EditTextPreference
        android:dialogTitle="dialog_title_edittext_preference"
        android:key="edittext_preference"
        android:summary="summary_edittext_preference"
        android:title="title_edittext_preference" />

    <PreferenceScreen
        android:summary="intent_preference"
        android:title="intent_preference" >
        <intent
            android:action="android.intent.action.VIEW"
            android:data="http://www.android.com" />
    </PreferenceScreen>

</PreferenceScreen>


Call setContentView() and addPreferencesFromResource() in onCreate()

You must call setContentView(YOUR_LAYOUT_WIHT_HEADER_FOOTER_ID).

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        addPreferencesFromResource(R.xml.pref);
    }

5 comments:

  1. This article does a lot of help

    ReplyDelete
  2. Thanks Man.
    This served as a perfect starting point for customizing my pref page.
    keep up the good work.

    ReplyDelete
  3. I have a problem. On preview all is fine, but when I run the aplivatio, the list view fit all the screen, and the footer stay beyond the list.

    Thanks!

    How can I fix this ??

    ReplyDelete
  4. I have a problem. On preview all is fine, but when I run the aplivatio, the list view fit all the screen, and the footer stay beyond the list.

    Thanks!

    How can I fix this ??

    ReplyDelete