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
.
Android: How to use Buttons in Preference Screen
Create a custom DialogPreference
that incorporates a TimePicker
widget. No button required. Should be under 100 lines of code.
Here is a sample project showing, among other things, a custom ColorPreference
.
Add up button to PreferenceScreen
If your complete application is a preferences screen, then you can make your main activity a PreferenceActivity
and the sub-levels can be fragments. This way the 'up' functionality is going to be by default what you are looking for.
Add a button to the bottom of Preferences Screen
You have to include a ListView with the id @android:id/list
like it says in the question you linked to (See Answer).
From your android layout file (R.Layout.Preferences) remove your <ListView>
and put this in it's place.
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
That should solve the problem you were having.
From the LogCat you posted:Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
Edit: Just some clarification of the id attribute on the ListView. From Android XML Layouts.
The syntax for an ID, inside an XML tag is:
android:id="@+id/my_button"
The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:
android:id="@android:id/empty"
With the android package namespace in place, we're now referencing an ID from the android.R resources class, rather than the local resources class.
Related Topics
How to Detect Shake Event with Android
Android Studio 0.4 Duplicate Files Copied in APK Meta-Inf/License.Txt
How to Create Custom Button in Android Using Xml Styles
Use Picasso to Get a Callback with a Bitmap
How to Handle an Asynctask During Screen Rotation
Replace Getmap with Getmapasync
Asynctask VS Thread in Android
Override Home and Back Button Is Case a Boolean Is True
How to Disable Scrolling on a Viewpager
How to Save Sms to Inbox in Android
How to Set Opacity (Alpha) for View in Android
How to Access the Drawable Resources by Name in Android
Spinner: Get State or Get Notified When Opens
Android. How Does Notifydatasetchanged() Method and Listviews Work
Locationclient Getlastlocation() Return Null