package com.afollestad.materialdialogs.prefs;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;

/**
 * Adapted from http://stackoverflow.com/a/27429926/1247248
 *
 * @author Marc Holder Kluver (marchold)
 */
public class MaterialListPreference extends ListPreference {

    private MaterialDialog.Builder mBuilder;
    private Context context;

    public MaterialListPreference(Context context) {
        super(context);
        this.context = context;
    }

    public MaterialListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void showDialog(Bundle state) {
        int preselect = -1;
        if (getEntryValues() != null) {
            for (int i = 0; i < getEntryValues().length; i++) {
                if (getValue() != null && getValue().equals(getEntryValues()[i])) {
                    preselect = i;
                    break;
                }
            }
        }

        mBuilder = new MaterialDialog.Builder(context)
                .title(getTitle())
                .icon(getDialogIcon())
                .positiveText(getPositiveButtonText())
                .negativeText(getNegativeButtonText())
                .items(getEntries())
                .itemsCallbackSingleChoice(preselect, new MaterialDialog.ListCallback() {
                    @Override
                    public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                        onClick(null, DialogInterface.BUTTON_POSITIVE);
                        dialog.dismiss();
                        if (which >= 0 && getEntryValues() != null) {
                            String value = getEntryValues()[which].toString();
                            if (callChangeListener(value) && isPersistent())
                                setValue(value);
                        }
                    }
                });

        final View contentView = onCreateDialogView();
        if (contentView != null) {
            onBindDialogView(contentView);
            mBuilder.customView(contentView, false);
        } else {
            mBuilder.content(getDialogMessage());
        }

        mBuilder.show();
    }
}
@@ -892,303 +892,37 @@
 t = 
--1;%0A        if (getEntryValues() != null) %7B%0A            for (int i = 0; i %3C getEntryValues().length; i++) %7B%0A                if (getValue() != null && getValue().equals(getEntryValues()%5Bi%5D)) %7B%0A                    preselect = i;%0A                    break;%0A                %7D%0A            %7D%0A        %7D
+findIndexOfValue(getValue());
 %0A%0A  
@@ -1005,24 +1005,24 @@
 getTitle())%0A
+
             
@@ -1052,63 +1052,8 @@
 ())%0A
-                .positiveText(getPositiveButtonText())%0A
     
@@ -2128,8 +2128,9 @@
 %0A    %7D%0A%7D
+%0A

Simplify and match stock behaviour