/*
 * Copyright (c) 2016. Libero Strategies, LLC - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and Confidential
 */

package com.example.android.sunshine.app;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.EditTextPreference;
import android.util.AttributeSet;

/**
 * Created by pink on 11/14/2016.
 */

public class LocationEditTextPreference extends EditTextPreference {
    private static final int DEFAULT_MINIMUM_LOCATION_LENGTH = 2;
    private int mMinLength;

    public LocationEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.LocationEditTextPreference,
                0, 0);
        try {
            mMinLength = a.getInteger(R.styleable.LocationEditTextPreference_minLength,
                    DEFAULT_MINIMUM_LOCATION_LENGTH);
        } finally {
            a.recycle();
        }
    }
}

@@ -220,16 +220,75 @@
 e.app;%0A%0A
+import android.app.AlertDialog;%0Aimport android.app.Dialog;%0A
 import a
@@ -350,16 +350,42 @@
 dArray;%0A
+import android.os.Bundle;%0A
 import a
@@ -441,24 +441,149 @@
 oid.
-util.AttributeSe
+text.Editable;%0Aimport android.text.TextWatcher;%0Aimport android.util.AttributeSet;%0Aimport android.widget.Button;%0Aimport android.widget.EditTex
 t;%0A%0A
@@ -1281,11 +1281,1181 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    protected void showDialog(Bundle state) %7B%0A        super.showDialog(state);%0A        EditText ed = getEditText();%0A        ed.addTextChangedListener(new TextWatcher() %7B%0A            @Override%0A            public void beforeTextChanged(CharSequence s, int start, int count, int after) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void onTextChanged(CharSequence s, int start, int before, int count) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void afterTextChanged(Editable s) %7B%0A                Dialog d = getDialog();%0A                if (d instanceof AlertDialog) %7B%0A                    AlertDialog dialog = (AlertDialog)d;%0A                    Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);%0A                    // Check if EditText is empty.%0A                    if (s.length() %3C mMinLength) %7B%0A                        // Disable OK button.%0A                        positiveButton.setEnabled(false);%0A                    %7D else %7B%0A                        // Re-enable the button.%0A                        positiveButton.setEnabled(true);%0A                    %7D%0A                %7D%0A            %7D%0A        %7D);%0A    %7D
 %0A%7D%0A

Implement limits in custom preference view.