Search in sources :

Example 1 with SimpleTextWatcher

use of eu.davidea.common.SimpleTextWatcher in project FlexibleAdapter by davideas.

the class EditItemDialog method onCreateDialog.

@SuppressLint({ "InflateParams", "HandlerLeak" })
@Override
public AlertDialog onCreateDialog(Bundle savedInstanceState) {
    //Pick up bundle parameters
    Bundle bundle;
    if (savedInstanceState == null) {
        bundle = getArguments();
    } else {
        bundle = savedInstanceState;
    }
    mTitle = bundle.getString(ARG_TITLE);
    mPosition = bundle.getInt(ARG_ITEM_POSITION);
    //Inflate custom view
    View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_edit_item, null);
    final EditText editText = (EditText) dialogView.findViewById(R.id.text_edit_title);
    //, R.style.AppTheme_AlertDialog);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.dialog_edit_title).setView(dialogView).setNegativeButton(R.string.CANCEL, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Utils.hideSoftInputFrom(getActivity(), editText);
            dialog.dismiss();
        }
    }).setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            getListener().onTitleModified(mPosition, editText.getText().toString().trim());
            Utils.hideSoftInputFrom(getActivity(), editText);
            dialog.dismiss();
        }
    });
    final AlertDialog editDialog = builder.create();
    editDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            updateOkButtonState(editDialog, null);
        }
    });
    if (mTitle != null) {
        editText.setText(mTitle);
        editText.selectAll();
    }
    editText.requestFocus();
    editText.addTextChangedListener(new SimpleTextWatcher() {

        private static final long DELAY = 400L;

        private static final int TRIGGER = 1;

        private Handler mHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                if (msg.what == TRIGGER) {
                    updateOkButtonState(editDialog, editText);
                }
            }
        };

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            updateOkButtonState(editDialog, null);
        }

        @Override
        public void afterTextChanged(Editable s) {
            mHandler.removeMessages(TRIGGER);
            mHandler.sendEmptyMessageDelayed(TRIGGER, DELAY);
        }
    });
    editDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    return editDialog;
}
Also used : EditText(android.widget.EditText) AlertDialog(android.support.v7.app.AlertDialog) Message(android.os.Message) DialogInterface(android.content.DialogInterface) Bundle(android.os.Bundle) Handler(android.os.Handler) View(android.view.View) SuppressLint(android.annotation.SuppressLint) SimpleTextWatcher(eu.davidea.common.SimpleTextWatcher) Editable(android.text.Editable) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 DialogInterface (android.content.DialogInterface)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Message (android.os.Message)1 AlertDialog (android.support.v7.app.AlertDialog)1 Editable (android.text.Editable)1 View (android.view.View)1 EditText (android.widget.EditText)1 SimpleTextWatcher (eu.davidea.common.SimpleTextWatcher)1