Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a header to a PreferenceActivity

Tags:

android

So I have found that android PreferenceScreen is not very style-friendly. Is there a semi-simple way to just add a header (say an image) to the preferencescreen before the preferences are shown? I am currently extending the PreferenceActivity class, but could someone show me how to add a linear layout to the header?

Thanks

like image 721
Peanut Avatar asked Mar 26 '11 16:03

Peanut


1 Answers

In my app I do exactly what you are requesting like this:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    setContentView(R.layout.preferences_layout);
}

and in the preferences_layout.xml file:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">

<ImageView
    android:id="@+id/myCustomImageView"
    android:src="@drawable/xmas"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
/>
<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_below="@+id/myCustomImageView"
/>

</RelativeLayout>

Notice the android:id set on the ListView in the xml Now you have control over the layout of the preferences screen and use the default style for the preferences as long as you use built-in preferences.

like image 119
Mircea Nistor Avatar answered Oct 16 '22 23:10

Mircea Nistor