I am trying to gate the title of preferencesreen in my program.. How can I get the title?
My code of preference XML is here. I want to get the title "Show List" in my program.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Settings">
<PreferenceScreen android:title="Show List" android:key ="show_list" android.id="#+id/preference_screen">
<intent android:action="android.intent.action.VIEW"
android:targetPackage="com.picknext"
android:targetClass="com.picknext.PickName" />
</PreferenceScreen>
<ListPreference android:title="Remove after pick"
android:key="selection_of_Y_N_Name_Pick" android:entries="#array/selection_of_y_n_namepick"
android:entryValues="#array/value_of_y_n_naempick" android:defaultValue="Yes" />
</PreferenceCategory>
</PreferenceScreen>
You probably have a class that extends PreferenceActivity. Inside this (for example in onCreate()) you have a call to addPreferencesFromResource(). After this you have access to the title via the methods getTitle() and setTitle().
I hope this helps.
Related
I have the following code in my xml resource for my settings menu:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:defaultValue="top_rated"
android:dialogTitle="Search for:"
android:entries="#array/SettingEntries"
android:entryValues="#array/SettingEntriesValue"
android:key="pref_sync"
android:summary="%s"
android:title="Show" />
</PreferenceScreen>
When my app loads the first time, i dont want it to load the stored value from the previous run, but select the first value on the list.
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sharedPrefs.edit().putString("pref_sync", "top_rated");
I tried to change the value in SharedPreferences when the app gets created but that didn't work. Any advice on how to solve the matter?
I want to create a preference screen in which there are three checkboxes; the first one is clickable and the other two are not until the first is checked.
How can I do this? I've seen this tutorial, but there is only one checkbox. Can anyone help me?
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="#string/summary_category"
android:title="#string/title_category">
<CheckBoxPreference
android:key="main"
android:defaultValue="true"
android:summary="#string/summary_main"
android:title="#string/title_main"
/>
<CheckBoxPreference
android:key="firstDependent"
android:summary="#string/summary_firstDependent"
android:title="#string/title_firstDependent"
android:dependancy="main"
/>
<CheckBoxPreference
android:key="secondDependent"
android:summary="#string/summary_secondDependent"
android:title="#string/title_secondDependent"
android:dependancy="main"
/>
</PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>
You can do this simply by setting android:dependancy to the key of the check box in which the respective check boxes must depend on.
Now create a folder named xml in res folder and put your preferences xml file inside that.
Then do the following.
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
You can also do this with fragments which is more recommended. But the above method is much more easy. If you want to do that with fragments, check this which contains everything you need to know about creating a Settings Activity.
Hope this helps.
You have to do it like in that example, but you'll have three checkboxes instead of one. If you want two checkboxes to be disabled until the first one is true you can use the android:dependency property. With this property you need to specify the key of the preference they will depend on.
<PreferenceCategory
android:summary="..."
android:title="..." >
<CheckBoxPreference
android:defaultValue="true"
android:key="first"
android:summary="#string/summary_first"
android:title="#string/title_first" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="second"
android:summary="#string/summary_second"
android:title="#string/title_second" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="third"
android:summary="#string/summary_third"
android:title="#string/title_third" />
</PreferenceCategory>
I have a Preference Activity
But, whenever I call it, the preferences are empty. No title or summaries are visible. You can click on the preference and it will highlight but there is no text. I have tried adding an EditTextPreference and in that case an edit text will pop up on click but there is no text.
Activity:
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="SomePref"
android:summary="Somethingsomething"
/>
</PreferenceScreen>
Thanks
You may try to add this inside PreferenceScreen and see what happens:
<PreferenceCategory
android:title="Preference Category">
<CheckBoxPreference
android:defaultValue="True"
android:key="checkbox_key"
android:title="CheckBox Title"
android:summaryOn="Checkbox is ticked"
android:summaryOff="Checkbox is not ticked" />
</PreferenceCategory>
I figured it out. I'm using the ActionBarSherlock library and I was extending the regular PreferencesActivity when it should have been Sherlock*PreferencesActivity
Everything works great now.
Always, throughout the app save your preferces through getApplicationContext(), if you use this as context it will save with activity context and cannot retreived all the time
I have a preferncescreen page for showing of data only and not saving it for setting
the code is as below
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Member Subscription Status">
<PreferenceScreen
android:title="Subscription Status"
android:summary="Active">
</PreferenceScreen>
<PreferenceScreen
android:title="Next Payment Date"
android:summary="2012-06-21 18:00:00s">
</PreferenceScreen>
<PreferenceScreen
android:title="Plan Type"
android:summary="Family">
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
Right now i am wondering how should i set the summary of the preferncescreen to show on the screen?
You can get the Preference with findPreference() and then set the summary with setSummary().
Found the solution. have to add a key first
android:key="Subscriptionstatus"
then in activity
Preference pref = findPreference( "Subscriptionstatus" );
pref.setSummary("test");
I have write a multi-level Preferences in program with xml File. and i want to know how to return to the previous level without press the back button.
how to write some code to implement an item in preference to return previous Preferences level.
I had the same problem and solve it thanks to Xuelong and getDialog() but without needing to manage onPreferenceTreeClick().
You need to keep an instance (myPreferenceScreen) of the PreferenceScreen you want to return from
You have to give him a key in XML
Retrieve the instance with findPreference("MyPreferenceScreenKey");
Once you have to return , use this method : myPreferenceScreen.getDialog().dismiss()
You will then return from where you came from.
Here is a epurated example :
Xml file :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:key="contactList"/>
<PreferenceScreen android:title="My Sub Preference Screen ..."
android:key="mySubScreenKey">
<EditTextPreference android:key="foo1"/>
</PreferenceScreen>
</PreferenceScreen>
Java file :
public class ParanoidPreferenceManager extends PreferenceActivity {
ListPreference contactList;
EditTextPreference foo1;
PreferenceScreen mySubScreenKey;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Load XML preference file
addPreferencesFromResource(R.xml.preferences);
contactList = (ListPreference) findPreference("contactList");
foo1= (EditTextPreference) findPreference("foo1");
screenContact = (PreferenceScreen) findPreference("screenAddContact");
foo1.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
mySubScreenKey.getDialog().dismiss();
return false;
}
});
}
}
That's it
Sorry for presentation, this is my first post on this site.
Bye dudes
Building on Patrice's and Xuelong's answers, I have made the following functions to easily do the job:
// Refresh the content of a PreferenceScreen by simulating a click (thus it will call the refresh code if it's contained inside the click callback)
private void refreshPreferenceScreenByClick(String prefScreenName) {
// Refresh preference screen (given by its name) by calling the click callback
PreferenceScreen prefScreen = (PreferenceScreen) findPreference(prefScreenName);
Preference.OnPreferenceClickListener click_callback = prefScreen.getOnPreferenceClickListener();
click_callback.onPreferenceClick(prefScreen);
}
// Close the current PreferenceScreen (or any given the name)
// Useful to go back to the previous PreferenceScreen when constructing dynamically nested submenus.
private void closePreferenceScreen(String prefScreenName) {
PreferenceScreen prefScreen = (PreferenceScreen) findPreference(prefScreenName);
prefScreen.getDialog().dismiss();
}
So first if what you do in your submenu define what is shown on the parent menu (eg in a music app: submenu allows to add a new instrument, and the parent menu shows the list of added instruments), you first have to call refreshPreferenceScreenByClick("parentPreferenceScreen"), and then to go back to the parent menu you can just close the current submenu by calling closePreferenceScreen("currentPreferenceScreen").
Alternatively, if you want to just open a submenu programmatically, you can use the openPreference function found here (tested and it works well): https://stackoverflow.com/a/4869034/1121352
NOTE: do NOT use the openPreference() function to go back to parent PreferenceScreen, because after a few iterations you will get a stack overflow and your app will crash (because Android will keep in memory the history of all previous menus, including the ones you open via openPreference()!).
You can simulate the press of the back button:
this.onBackPressed();
or
getActivity().onBackPressed();
if you are in a fragment.
I think you are looking for something like this
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="NotifSettings" android:title="Notification Settings">
<PreferenceCategory android:title="Notification Settings">
<CheckBoxPreference android:key="NotifyOption"
android:title="Notification Status" android:defaultValue="true"
android:summaryOn="Notifications are Enabled." android:summaryOff="Notifications are Disabled."
android:persistent="true" />
<Preference android:title="XXXX"
android:dependency="NotifyOption" android:summary="xxxxxx"
android:key="Xxx" />
</PreferenceCategory>
<PreferenceCategory android:title="YYYYY">
<PreferenceScreen android:title="Configure Notifications"
android:summary="yyyyy">
<CheckBoxPreference android:key="aa"
android:title="aaaaa" android:defaultValue="true"
android:summary="aaaa" />
<CheckBoxPreference android:key="bb"
android:title="bbbbb" android:defaultValue="true"
android:summary="bbbb." />
...
<ListPreference android:title="zzz"
android:summary="zzzz" android:key="zz"
android:defaultValue="0" android:entries="#array/OverDesc"
android:entryValues="#array/OverValues" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="Alert Type">
<CheckBoxPreference android:key="AlertVibr"
android:title="Vibrate" android:defaultValue="true"
android:summaryOn="Vibration Turned ON" android:summaryOff="Vibration Turned OFF" />
<CheckBoxPreference android:key="AlertSound"
android:title="Sound" android:defaultValue="true" android:summaryOn="Sound Turned ON"
android:summaryOff="MUTE" />
</PreferenceCategory>
But what I didnt get is that your want to return to the previous preference screen, without pressing back button. Can you shed some more light on that??