I'm quite new to Android Development and just came across Preferences. I found PreferenceScreens and wanted to create a login functionality with it. The only problem I have it that I don't know hot i could add a "Login" button to the PreferenceScreen. Here's how my Preference View 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 ExitTextPreferences.
Is there a simple solution for this problem? The one solution i found was not working because i use sub Preference Screens.
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?
I don't think there is an easy way to just add a Button to a preference screen. Preferences are limited to:
While using a preference screen, you are limited to the use of these options, and there are no single "button-preference" which could be easily used for your need.
I've been looking for similar functionality, with adding buttons to the preference screen, but it seems like you need to build your own screen for this, managing the change to preferences in your own implementation.
I wanted to add an Exit link to the preferences and was able to modify Jakar's code to make it work like this:
Originally the 'Preference' was a 'EditTextPreference' which I hand edited.
Then in the class:
For the xml:
Then for the java in your
onCreate()
This will appear like a normal Preference, with just a title and a summary, so it will look like it belongs.
Edit: I changed to using
@string/myCoolButton
andgetString(R.string.myCoolButton)
because it's best to use resources for compiler assistance, language translation, and ease of String changes.I suppouse its too late. This is what i have done for a Button Preference.
The preference in preference.xml file in xml folder
and pref_reset_bd_button.xml in layout folder
try android:onClick="myMethod" works like a charm for simple onclick events
You can do it like this to access the button!
Don't forget to add
android:id
to the LinearLayout that contains the button in layoutButtons.xml, i.e.android:id="@+id/mylayout"