androidandroid-preferencesdialog-preference

ListPreference without the radio


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!

enter image description here

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);
        }
    }

Solution

  • 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.