Android NavigationDrawer with PreferenceActivity - android

Can any one suggest me how to implement the Navigation drawer in the preference Activities ?
My JavaCode.
package com.example.android.navigationdrawerexample;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class AppPreferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.as);
}
}
My XML code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference android:title="Your Name"
android:key="username"
android:summary="Please provide your username"></EditTextPreference>
<CheckBoxPreference android:title="Application Updates"
android:defaultValue="false"
android:summary="This option if selected will allow the application to check for latest versions."
android:key="applicationUpdates" />
<ListPreference android:title="Download Details"
android:summary="Select the kind of data that you would like to download"
android:key="downloadType"
/>
</PreferenceScreen>
Please someone advice me. I am awaiting for your responses.

Try using PreferenceFragment with FragmentActivity instead of PreferenceActivity.

It is impossible to use together PreferenceActivity and NavigationDrawer due to two mutually exclusive options:
PreferenceActivity has to inflate its own internal layout.
DrawerLayout must be root element of layout.
So, I think this is the end of the road.

Related

Android checkboxpreference disabled even though it's enabled in XML

I currently have the problem of a disabled checkbox preference which is enabled in the preference.xml. This happened after the migration to Metarial design. I tried to enable the view programmatically but it didn't help.
(I'd like to show a screenshot but my rep is not high enough yet :/)
my preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Network Service" >
<CheckBoxPreference
android:enabled="true"
android:key="#string/communication_option"
android:selectable="true"
android:defaultValue="true"
android:shouldDisableView="false"
android:summary="Allows down- and upload."
android:title="Enable server communication" />
<CheckBoxPreference
android:disableDependentsState="false"
android:enabled="true"
android:dependency="#string/communication_option"
android:key="#string/wifi_sync_option"
android:selectable="true"
android:defaultValue="true"
android:shouldDisableView="true"
android:summary="Do not use mobile bandwidth to download"
android:title="WiFi synchronization only" />
</PreferenceCategory>
</PreferenceScreen>
my preferenceFragment
public class FragmentPreference extends PreferenceFragment implements
SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
// added to make sure it really is enabled
CheckBoxPreference cbp = (CheckBoxPreference) getPreferenceManager()
.findPreference(getString(R.string.communication_option));
cbp.setEnabled(true);
cbp.setShouldDisableView(false);
}
I am using the Theme.AppCompat.Light.NoActionBar theme to be able to add the new toolbar.
An other thing i noticed is, that the preference is shown as enabled it the screen orientation changes to landscape and even though the preference is shown as disabled the preference can still be clicked/checked/unchecked.
After a few trial and error I found out that I just overloaded the Preferences.
The slimmer and much cleaner version that works looks like this:
<PreferenceCategory android:title="Network Service" >
<CheckBoxPreference
android:key="#string/communication_option"
android:defaultValue="true"
android:summary="Allows down- and upload."
android:title="Enable server communication" />
<CheckBoxPreference
android:dependency="communication_option"
android:key="#string/wifi_sync_option"
android:defaultValue="true"
android:summary="Do not use mobile bandwidth to download"
android:title="WiFi synchronization only" />
</PreferenceCategory>
and
public class FragmentPreference extends PreferenceFragment implements
SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
You have to set the default values in you your main activity's onCreate.
PreferenceManager.setDefaultValues(this, R.xml.pref_general, false);

How to know if we are in the Parent PreferenceScreen or in a nested one

I've a settings.xml file like :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceScreen
android:key="GeneralScreen"
android:title="#string/generalParameters" >
<EditTextPreference
android:dialogTitle="#string/companyName"
android:key="CompanyName"
android:title="#string/companyName" />
<EditTextPreference
android:dialogTitle="#string/companyAddress"
android:key="CompanyAddress"
android:title="#string/companyAddress" />
</PreferenceScreen>
</PreferenceScreen>
I've a PreferenceFragment :
public class Settings extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
....
I need a method which returns if 1) I'm in the parent PreferenceScreen 2) if I'm a nested PreferenceScreen (or even a multiple nested)
Do you know a method to find that information ?
Did you try calling in your PreferenceActivity
if(getPreferenceScreen() != null)
or
if(getPreferenceScreen().getTitle()!=null).
If the condition is true - you are in a PreferenceScreen.
Aware that This method was deprecated in API level 11.
This function is not relevant for a modern fragment-based PreferenceActivity.

How create a settings menu with checkbox Android

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>

Android - Preference screen not showing preferences

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

how to return to the previous PrefenenceScreen from nested PreferenceScreen without press the back button

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??

Categories

Resources