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 pr
For the xml:
<Preference
android:title="Acts like a button"
android:key="@string/myCoolButton"
android: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.
You can do it like this to access the button!
View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layoutButtons, null, false);
Don't forget to add android:id
to the LinearLayout that contains the button in layoutButtons.xml, i.e.
android:id="@+id/mylayout"
LinearLayout mLayout = (LinearLayout) footerView.findViewById(R.id.mylayout);
Button login = (Button) footerView.findViewById(R.id.loginButton);
login.setOnClickListener(this);
ListView lv = (ListView) findViewById(android.R.id.list);
lv.addFooterView(footerView);
// Lines 2 and 3 can be used in the same way for a second Button!
Add
setContentView(R.layout.buttonLayout);
Below
addPreferencesFromResource(R.xml.yourPreference);
buttonLayout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/top_control_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="@string/saveAlarm"/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="@id/bottom_control_bar"
android:layout_below="@id/top_control_bar"
android:choiceMode="multipleChoice">
</ListView>
Access Button by:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Your event
}
});
You can get the button on top or on bottom of the screen by putting the button in RelativeLayout:
bottom_control_bar
This worked for me. I hope I can help someone with this piece of code.
try android:onClick="myMethod" works like a charm for simple onclick events