2012-09-18

Custom Android PreferenceActivity

PreferenceActivity is a Activity combined with Preference. How do I add a View or Layout into PreferenceActivity?

 

Before start, you should have your own preference screen xml already, for example: settings_pref.xml

Create another layout xml for your new PreferenceActivity, including new Button or Layout you want added into PreferenceActivity.

For example:

settings_pref_layout.xml

<?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"
    android:background="@drawable/bg1"> 
    <Button  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="Button" />

    <ListView android:id="@android:id/list" 
        android:background="#00000000"
        android:cacheColorHint="#00000000"
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"></ListView> 
</LinearLayout>

Please notice that ListView in this xml is your Preference list, which will reference to your Perference screen xml. The id is fixed android:id="@android:id/list", you can’t change it.

Then, add code into your Activity:

public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
        
       addPreferencesFromResource(R.xml.mypreference); 
       setContentView(settings_pref_layout); 
}

 

Reference link: http://stackoverflow.com/questions/2697233/how-to-add-a-button-to-preferencescreen

No comments: