ListPreference without the radio [duplicate] - android

This question already has an answer here:
ListPreferences without any radio buttons?
(1 answer)
Closed 9 years ago.
I am making the preference screen for my app. Inside the preference screen, I have a ListPreference to “Spread the word” about the app. However, I don't want the radio buttons there. I want the whole dialog seem as if it is a list of things the user can do, and the user would select one option from the menu which will be executed. How do I do it? I am new to Android, coming from iOS background.
Thanks in advance!
I have this in my pref_settings.xml.
<PreferenceCategory
android:key="pref_key_tell_friends"
android:title="#string/pref_header_tell_friends" >
<ListPreference
android:entries="#array/spread_the_word"
android:entryValues="#array/spread_the_word"
android:key="pref_key_spread"
android:title="#string/pref_title_spread" />
</PreferenceCategory>
and this the fragment I’m loading in my activity —
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_settings);
}
}

A preference works just like any button when it is specified as a Preference in the XML, instead of a ListPreference, or any other type. So, I just added a listener to the preference, and opened an AlertDialog in the listener. Here’s the code —
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_settings);
Preference myPref = (Preference) findPreference("pref_key_spread");
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
createListPreferenceDialog();
return false;
}
});
}
private void createListPreferenceDialog() {
Dialog dialog;
final String[] str = getResources().getStringArray(R.array.spread_the_word);
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setTitle("Spread the Word");
b.setItems(str, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int position){
Log.I(“Clicked the AlertDialog", + str[position]);
}
});
dialog = b.create();
dialog.show();
}
}
And here’s the changed XML —
<PreferenceCategory
android:key="pref_key_tell_friends"
android:title="#string/pref_header_tell_friends" >
<Preference
android:key="pref_key_spread"
android:title="#string/pref_title_spread" />
</PreferenceCategory>
Thanks #njzk2 for the pointer.

Related

Android PreferencesActivity showing previous fragment

I created a Preferences screen by extending PreferencesActivity as per the example here.
The first screen appears okay but when I click to see the second screen I see them one on top of the other.
So, taking advice from a number of other thread, I changed the background of the second fragment to black.
This worked in as much as I no longer see them both. But instead I see the FIRST one only except for the header.
The first screen looks like this:
and the second one looks like this:
Only the header line changed whereas the rest remained the same.
This is the code in my activity:
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesMain()).commit();
}
/**
* This fragment contains a second-level set of preference that you
* can get to by tapping an item in the first preferences fragment.
*/
public static class PreferencesMain extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
#Override
public void onStart() {
super.onStart();
View view = getView();
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}
}
/**
* This fragment contains a second-level set of preference that you
* can get to by tapping an item in the first preferences fragment.
*/
public static class PreferencesNotifications extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences_notifications);
}
#Override
public void onStart() {
super.onStart();
View view = getView();
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}
}
#Override
protected boolean isValidFragment (String fragmentName) {
return true;
}
}
preferences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/settings_notifications">
<!-- This PreferenceScreen tag sends the user to a new fragment of
preferences. If running in a large screen, they can be embedded
inside of the overall preferences UI. -->
<PreferenceScreen
android:fragment="activities.SettingsActivity$PreferencesNotifications"
android:title="#string/settings_notifications_managePushTitle"
android:summary="#string/settings_notifications_managePushSummary">
android:background="#android:color/black"
<!-- Arbitrary key/value pairs can be included for fragment arguments -->
<extra android:name="someKey" android:value="somePrefValue" />
</PreferenceScreen>
</PreferenceCategory>
preferences_notifications.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/settings_notifications_managePushHeader">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="#string/settings_notifications_managePushEntry1Title"
android:summary="#string/settings_notifications_managePushEntry1Summary" />
</PreferenceCategory>
Add a header.xml to manage settings, and use onBuildHeaders method.
Resource headers.xml:
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header android:title="Digle"
android:fragment="activities.SettingsActivity$PreferencesMain"/>
</preference-headers>
Class SettingsActivity:
public class SettingsActivity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Remove all code in here
}
#Override
public void onBuildHeaders(List<Header> target) {
super.onBuildHeaders(target);
loadHeadersFromResource(R.xml.headers, target);
}
...
}
EDIT:
An alternative could be use subscreens. You have to place the group of Preference objects inside a PreferenceScreen.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="Settings"
android:title="#string/app_name">
<PreferenceCategory
android:title="#string/settings_notifications">
<PreferenceScreen
android:background="#android:color/black"
android:summary="#string/settings_notifications_managePushSummary"
android:title="#string/settings_notifications_managePushTitle">
<PreferenceCategory
android:title="#string/settings_notifications_managePushHeader">
<CheckBoxPreference
android:key="checkbox_preference"
android:summary="#string/settings_notifications_managePushEntry1Summary"
android:title="#string/settings_notifications_managePushEntry1Title"/>
</PreferenceCategory>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>

Programmatically close Preference Header

In my application I am displaying Preference Headers in the recommended way.
I extend PreferenceActivity and add the headers from the XML resource as so:
#Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}
One such Header is:
<header
android:fragment="com.example.FragmentSettings"
android:summary="Tap to configure"
android:title="General Settings" />
Which launches the main Preference Fragment which extends PreferenceFragment
A particular Setting allows a user to wipe all of the data the application has access to and deauthorise their account.
If the user selects this option, I wipe everything, cache, shared preferences etc etc and I would then like to return them to the initial authorisation screen.
The problem is that the Preference Header Screen persists, even when all Activities call finish(); and even if I call System.exit(0);
How can I make the damn thing close!?
Help!
Given the small amount of detail you've given, I'll try to provide an answer assuming you want the activity backstack cleared when you open the authorisation screen.
MyPreferenceActivity - only 'onBuildHeaders' as an example (also, the activity is opened when a button is clicked)
preference-headers - only 'FragmentSettings'
FragmentSettings:
public class FragmentSettings extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_settings);
Preference myPref = (Preference) findPreference("pref_clearAllData");
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(FragmentSettings.this.getActivity(), AuthActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return true;
}
});
}
}
preference_settings (I'm assuming you're not using a checkbox, but it works as an example):
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="General Settings" >
<CheckBoxPreference
android:key="pref_clearAllData"
android:title="Clear all data"
android:summary="Clears all data and shows the authorisation screen"
android:defaultValue="false" />
</PreferenceCategory>
please try this:
protected void onListItemClick (ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
finish();
}

Adding action to preferences

I'm new to Android Preferences which I am using for a settings menu and I just had a few questions. I looked on the API site and couldn't find a way to add action to them. I have an Activity:
public class SettingsActivity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
addPreferencesFromResource(R.xml.preferences);
}
}
}
which works fine, it displays my xml PreferenceScreen page which contains 4 Preference tags. My question is how do I add action when clicking on those preferences. For example I want a separate pop up window to be displayed where I can change a number value and for that to be saved each time I open the app. If someone could provide an example or something I would really appreciate it
I want a separate pop up window to be displayed where I can change a number value and for that to be saved
This is easy, you just have to make that Preference an EditTextPreference at your xml file like so:
<EditTextPreference
android:title="#string/title"
android:key="preferenceKey" />
You can customize it more: if you only want integers android:numeric="integer" , if you want to set the max length android:maxLength="3" and the default value android:defaultValue="10" . You don't have to do anything in the Java class
how do I add action when clicking on those preferences
If you want to add some more complicated action, use a Preference.OnPreferenceClickListener. You can use it like so:
Preference preference = // some preference
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
#Override
public boolean onPreferenceClick(Preference p){
//do something
return false;
}
});
You can find more at this question. Hope this helps
PS. If you want to customize the preference a lot (i.e. not only the action onClick, but also the layout etc), you should consider creating a custom Preference, as said at another answer.
It sounds like what you are trying to do would be appropriately handled by creating a custom Preference. The Android docs has a decent tutorial on this: http://developer.android.com/guide/topics/ui/settings.html#Custom
Based on your requirement, you can work through intents for your preference tags. below is my example code in Menu.java: and also you can follow the developer tutorial at : http://developer.android.com/guide/topics/ui/settings.html#Custom
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.about:
/*Intent aboutIntent = new Intent(Menu.this, About.class);
startActivity(aboutIntent);*/
Intent aboutIntent = new Intent("com.example.myfirstapp.ABOUT");
startActivity(aboutIntent);
break;
case R.id.preference:
Intent prefsIntent = new Intent("com.example.myfirstapp.PREFS");
startActivity(prefsIntent);
break;
case R.id.exit:
finish();
break;
}
return false;
}
Code in About.java
public class About extends Activity {
TextView TV1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
TV1 = (TextView) findViewById (R.id.textView1);
}
}

Is it possible to build DialogPreference from DialogFragment?

I have implemented directory picker as a DialogFragment and now I would like to use it in DialogPreference. Is it possible? How to do it?
It's not obvious to me what exactly you want to do, but I'm assuming you have a Fragment layout which you want to show on a Preference click. Here is the code for it:
preferences.xml
<PreferenceScreen ... >
...
<Preference android:title="Dialog Fragment Displayer"
android:key="dialog_preference"
android:selectable="true"/>
...
</PreferenceScreen>
PreferenceActivity.java (or wherever your main settings control is)
final Activity activity = getActivity(); //replace with this if in an Activity
Preference p = findPreference("dialog_preference");
p.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean OnPreferenceClick(Preference preference) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
View view = activity.getLayoutInflater().inflate(R.layout.your_fragment, null, false);
builder.setView(view);
builder.create().show();
}
});

Button in preference file

I have set the two buttons in preference.xml file. I want to go to main activity when I click the "ok" button. So for that code is where to write?
You can set an onPreferenceClickListener in your PreferenceActivity.
in preference xml :
<Preference android:title="Preference Button" android:summary="This works almost like a button" android:key="mypref" />
in preferences activity :
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// Get the custom preference
Preference mypref = (Preference) findPreference("mypref");
mypref.setOnPreferenceClickListener(new OnPreferenceClickListener() { });
}
}

Categories

Resources