How to add a button to PreferenceScreen How to add a button to PreferenceScreen android android

How to add a button to PreferenceScreen


There is another solution for customizing the appearance of the preferences.

Design a normal XML layout with buttons or whatever you want to add to the standard preferences. Include a ListView in your layout and give it the ID @android:id/list.

Let's say we call the layout file res/layout/main.xml. It could look something like this:

<?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">    <Button android:text="This is a button on top of all preferences."            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    <ListView android:id="@android:id/list"              android:layout_width="match_parent"              android:layout_height="wrap_content" /></LinearLayout>

In your PreferenceActivity, add these two lines to your onCreate:

addPreferencesFromResource(R.xml.preferences);setContentView(R.layout.main);

The ListView in your layout will then be replaced by the preferences defined the usual way in res/xml/preferences.xml.


I know this is a bit late, but I just found a solution i like better than Max's praised solution.

You can simply add a footer (or if you like the button to be on top, a header) to the PreferenceActivity's ListView like so:

public class MyActivity extends PreferenceActivity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        addPreferencesFromResource(R.xml.preferences);        ListView v = getListView();        v.addFooterView(new Button(this));    }}

I hope this helps someone.


This example below will render a button at the bottom of the page (in case anybody is still interested).

In case of a LinearLayout you could also apply weights; this is needed because the Listview is set to *fill_parent*.I usually do this by adding *android:layout_weight* 's:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="fill_parent"              android:layout_height="fill_parent"              android:orientation="vertical">    <ListView android:id="@android:id/list"              android:layout_width="fill_parent"              android:layout_height="fill_parent" android:layout_weight="10"/>    <Button android:text="This is a button on top of all preferences."            android:layout_width="wrap_content"            android:layout_height="wrap_content" android:layout_weight="1"/></LinearLayout>

The explanation below isn't propbably 100% but it will help you understand...

+-- View Port (linear layout)| +-- List View (this is where the preferences will go)| || || +--+--  +--  | Button (which was pushed out of view by the fillparent of ListView  +--

You could also say, because the Button has no weight; the button is rendered at 0dp height.

Now with the layout_weigths added it will lett the button render inview

+-- View Port (linear layout)| +-- List View (this is where the preferences will go)| || || +--| +--| | Button (which was pushed out of view by the fillparent of ListView| +--+--