Implementing Settings page in Android Application -
Implementing Settings page in Android Application -
i new developer, , i'm trying implement settings page in android, not much luck.
i using checkboxpreference, , i'm trying create sharedpreferences alter when check/uncheck checkboxpreference.
here code:
package com.entu.bocterapp; //imports public class settings extends preferenceactivity implements sharedpreferences.onsharedpreferencechangelistener { checkboxpreference pushnotificationsetting; checkboxpreference locationsupportsetting; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.settings_design); initializeactionbar(); } public void initializeactionbar(){ actionbar actionbar = getactionbar(); actionbar.setbackgrounddrawable(new colordrawable(0xff50aaf1)); getactionbar().setdisplayoptions(actionbar.display_show_custom); getactionbar().setcustomview(r.layout.action_bar_center_image); } @override public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) { if (key.matches("push_notification_preference")) { if (getpushnotificationspreference().equals("true")) { setpushnotificationspreference("false"); } else { if (getpushnotificationspreference().equals("false")) { setpushnotificationspreference("true"); } } toast.maketext(this, getpushnotificationspreference(), toast.length_short).show(); } if (key.matches("location_support_preference")) { if (getlocationsupportpreference().equals("true")) { setlocationsupportpreference("false"); } else { if (getlocationsupportpreference().equals("false")) { setlocationsupportpreference("true"); } } } toast.maketext(this, getlocationsupportpreference(), toast.length_short).show(); } public void setpushnotificationspreference(string value) { sharedpreferences sharedpref = getpreferences(context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); editor.putstring(getstring(r.string.push_notification_preference), value); editor.commit(); } public string getpushnotificationspreference() { sharedpreferences sharedpref = getpreferences(context.mode_private); homecoming sharedpref.getstring(getstring(r.string.push_notification_preference), "true"); } public void setlocationsupportpreference(string value) { sharedpreferences sharedpref = getpreferences(context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); editor.putstring(getstring(r.string.location_support_preference), value); editor.commit(); } public string getlocationsupportpreference() { sharedpreferences sharedpref = getpreferences(context.mode_private); homecoming sharedpref.getstring(getstring(r.string.location_support_preference), "true"); } }
here xml:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preferencecategory android:title="bocterapp settings"> <checkboxpreference android:key="push_notification_preference" android:title="push notifications" android:summary="uncheck if don't want receive force notifications alerts in area (2km radius)." android:defaultvalue="true"/> <checkboxpreference android:key="location_support_preference" android:title="location support" android:summary="filters locations more 15 km away. uncheck if want see alerts." android:defaultvalue="true"/> </preferencecategory> </preferencescreen>
can tell me doing wrong/how implement correctly?
i think, want alter checkboxpreference
summary. can utilize android:summaryon
set summary while value true
. , utilize android:summaryoff
set summary while value false
. looks this:
<checkboxpreference android:key="location_support_preference" android:title="location support" android:summaryon="uncheck if don't want receive force notifications alerts in area (2km radius)." android:summaryoff="filters locations more 15 km away. uncheck if want see alerts." android:defaultvalue="true" />
android settings checkboxpreference
Comments
Post a Comment