Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In EditText, popup a dialog when clicking on specific subset of text

Found two solutions - please see selected answer

When the user clicks in a certain region of an EditText, I want to popup a dialog. I used onClick to capture the click. This partially works: the first time the user taps the EditText, the soft keyboard pops up and the dialog doesn't. Subsequent taps bring up the keyboard and then the dialog (and the keyboard disappears).

I suspect this has something to do with the EditText gaining focus.

Here's a code snip:

public class PrefixEditText extends EditText implements TextWatcher, OnClickListener
{
  public PrefixEditText (Context context)
  {
    super (context);
    setOnClickListener (this);
  }

  @Override
  public void onClick(View v)
  {
    int selStart = getSelectionStart();
    if (selStart < some_particular_pos)
      bring_up_dialog();
  }
}

IMPORTANT: I don't want to completely disable the normal EditText behavior. I want the user to be able to make region selections (for copy & paste). I probably still want it to gain focus (so I don't break the model when people with physical keyboards use the app). And it's ok for the click to set the cursor position. Thus, solutions that override onTouch and block all onTouch actions from the EditText will not work for me.

UPDATE I've discovered a bit more. If the EditText is gaining focus, onFocusChange gets called and onClick does not. If it already has focus, onClick gets called and onFocusChange does not.

Secondly, it's possible to hide the keyboard by calling

setInputType (InputType.TYPE_NULL);

Doing so in onFocusChange works - the keyboard never shows up. Doing so in onClick (assuming the keyboard was hidden before the click) apparently is too late - the keyboard shows up and then disappears.

The next idea to try would be to hide the keyboard during onTouch. However, I'm afraid to mess with that code - seems that whatever I figure out would be very fragile with respect to future versions of EditText.

Any thoughs on this?

like image 772
Peri Hartman Avatar asked Feb 12 '13 05:02

Peri Hartman


Video Answer


2 Answers

May be this can work

 EditText e = new EditText(context);
    e.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
        if(hasFocus)
        {
            //dialogue popup
        }
        }
    });

or u can use e.hasFocus(); and then use e.setFocusable(false); to make it unfocus

/////////////// my code

e.setInputType(InputType.TYPE_NULL);

e.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        AlertDialog.Builder sa = new Builder(ctx);
        sa.create().setOnDismissListener(new OnDismissListener() {

            public void onDismiss(DialogInterface dialog) {
                // TODO Auto-generated method stub
                e.setInputType(InputType.TYPE_CLASS_TEXT);
            }
        });
        sa.show();
    }
});
like image 189
Kishor datta gupta Avatar answered Oct 30 '22 05:10

Kishor datta gupta


try change capture click by onClick to onTouch

    this.editText.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                                //dialogue popup
            }
            return false;
        }
    });
like image 38
Intathep Avatar answered Oct 30 '22 05:10

Intathep