How to add a button to a PreferenceScreen?
android, android-button, android-preferences
Solution
For the xml:
<Preference
app:title="Acts like a button"
app:key="@string/myCoolButton"
app:summary="This is a cool button"
/>
Then for the java in your `onCreate()`
Preference button = findPreference(getString(R.string.myCoolButton));
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
//code for what you want it to do
return true;
}
});
This will appear like a normal Preference, with just a title and a summary, so it will look like it belongs.
Notes
- "Adding app:widgetLayout with some edit icon would make it much more clear for the user." - Doron Ben-Ari. Doron expands on this in the comments.
- Older versions of Android used `android:` prefix. Answer is updated with `app:` prefix.
Problem
I'm quite new to Android Development and just came across Preferences. I found `PreferenceScreen` and wanted to create a login functionality with it. The only problem I have is that I don't know how I could add a "Login" button to the `PreferenceScreen`. Here's what my `PreferenceScreen` looks like: ``` <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> ... <PreferenceScreen android:title="@string/login" android:key="Login"> <EditTextPreference android:persistent="true" android:title="@string/username" android:key="Username"></EditTextPreference> <EditTextPreference android:title="@string/password" android:persistent="true" android:password="true" android:key="Password"></EditTextPreference> </PreferenceScreen> ... </PreferenceScreen> ``` The Button should be right under the two `EditTextPreference`s. Is there a simple solution for this problem? The one solution I found was not working because I use sub `PreferenceScreen`s. Update: I figured out that i can add buttons this way: ``` <PreferenceScreen android:title="@string/login" android:key="Login"> <EditTextPreference android:persistent="true" android:title="@string/username" android:key="Username"></EditTextPreference> <EditTextPreference android:title="@string/password" android:persistent="true" android:password="true" android:key="Password"></EditTextPreference> <Preference android:layout="@layout/loginButtons" android:key="loginButtons"></Preference> </PreferenceScreen> ``` and the layout file (`loginButtons.xml`) looks that way: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="fill_parent" android:weightSum="10" android:baselineAligned="false" android:orientation="horizontal"> <Button android:text="Login" android:layout_width="fill_parent" android:layout_weight="5" android:layout_height="wrap_content" android:id="@+id/loginButton" android:layout_gravity="left"></Button> <Button android:text="Password?" android:layout_width="fill_parent" android:layout_weight="5" android:layout_height="wrap_content" android:id="@+id/forgottenPasswordButton"></Button> </LinearLayout> ``` So now the buttons appear but I can't access them in code. I tried it with `findViewById()` but this is returning null. Any ideas how I could access these buttons?