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
.
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.
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 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
Padding Between Actionbar's Home Icon and Title
How to Build a Native (Command Line) Executable to Run on Android
How to Read and Edit Android Calendar Events Using the New Android 4.0 Ice Cream Sandwich API
Android Hello, Gallery Tutorial -- "R.Styleable Cannot Be Resolved"
Understanding Android's <Layer-List>
How to Put the Text on the Left of a Radio Button in Android
Android Ndk C++ Jni (No Implementation Found for Native...)
Start a Service in a Separate Process Android
Firebaseinitprovider: Firebaseapp Initialization Unsuccessful
Android Scrollview Layout Problem
Login Error: There Is an Error in Logging You into This Application. Please Try Again Later
Eclipse, Android, Scala Made Easy But Still Does Not Work
Difference Between a Clickable Imageview and Imagebutton
Ontouchevent Not Working on Child Views
Camera.Setparameters Failed in Android
Multiple Dex Files Define Lorg/Apache/Cordova/Buildhelper