Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using listpreference and getting the key works but no ok button

I'm using listpreference in my android app and getting my key values and all is well and works good (now that you guys have helped me) BUT - when my listpreference menus popup, they only contain a cancel button.

Let's say the user is choosing between red, blue, and green. When the listpreference dialog first pops-up, the dialog only shows a cancel button. Because of that, the dialog disappears as soon as the user selects their choice. I would like it so that when the user chooses their setting, they see the radio button get highlighted and then they go ahead and click the ok button...but I don't have an ok button and can't figure out why. Any help would be awesome...al

like image 819
Allan Avatar asked Mar 25 '10 22:03

Allan


2 Answers

You can clone and reimplement ListPreference to work the way you want, making your own custom Preference class as a result.

However, ListPreference is set up to only use a negative ("Cancel") button. As the source code says:

    /*
     * The typical interaction for list-based dialogs is to have
     * click-on-an-item dismiss the dialog instead of the user having to
     * press 'Ok'.
     */
like image 59
CommonsWare Avatar answered Oct 04 '22 16:10

CommonsWare


I have done what the previous answer suggested and implemented my own ListPreference based on Android source code. Below is my implementation that adds the OK button.

myPreferenceList.java

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.preference.ListPreference;
import android.util.AttributeSet;


public class myPreferenceList extends ListPreference implements OnClickListener{

    private int mClickedDialogEntryIndex;

    public myPreferenceList(Context context, AttributeSet attrs) {
        super(context, attrs);


    }

    public myPreferenceList(Context context) {
        this(context, null);
    }

    private int getValueIndex() {
        return findIndexOfValue(this.getValue() +"");
    }


    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        super.onPrepareDialogBuilder(builder);

        mClickedDialogEntryIndex = getValueIndex();
        builder.setSingleChoiceItems(this.getEntries(), mClickedDialogEntryIndex, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                mClickedDialogEntryIndex = which;

            }
        });

        System.out.println(getEntry() + " " + this.getEntries()[0]);
        builder.setPositiveButton("OK", this);
    }

    public  void onClick (DialogInterface dialog, int which)
    {
        this.setValue(this.getEntryValues()[mClickedDialogEntryIndex]+"");
    }

}

Then you can use the class in your preference.xml as follow:

<com.yourApplicationName.myPreferenceList
            android:key="yourKey"
            android:entries="@array/yourEntries"
            android:entryValues="@array/yourValues"
            android:title="@string/yourTitle" />
like image 41
Techi50 Avatar answered Oct 04 '22 15:10

Techi50