58

I have an edittext, and when the user clicks this edittext I want to show an alertdialog.
My code is the following :

            edt.setInputType(InputType.TYPE_NULL);
            edt.setFocusableInTouchMode(true);
            edt.requestFocus();

            edt.setCursorVisible(false);

            edt.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
                }
            });

I don't want the keyboard to show up when the user clicks the edittext, so I set the inputtype to TYPE_NULL.
But when the edittext doesn't have focus and I click it, the onClick event isn't executed. When I click it a second time, the alertdialog shows up correctly.

How do I fix this?

Robby Smet
  • 4,369
  • 6
  • 54
  • 96

9 Answers9

132

Simply try to add this to your XML file. Your keyboard pops up when widget gains focus. So to prevent this behaviour set focusable to false. Then normal use OnClickListener.

<EditText
  android:focusable="false"
  ...
/>

Now, it should works.

Simon Dorociak
  • 32,775
  • 10
  • 65
  • 104
24

You can use onTouch instead of onClick, so it doesn't matter if the EditText has focus or not.

edt.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) { 
        CommentDialog.buildDialog(mContext, identifier, false, edt.getId());                    
        return false;
    }
});
Carnal
  • 20,698
  • 6
  • 55
  • 72
7

Nothing much to do you just have to

edt.setFocusable(false);
raghav chopra
  • 797
  • 3
  • 8
  • 25
7

If focusableInTouchMode is true then touch is triggered in second touch only, so unless you want that case use false for focusableInTouchMode. and if you want to enable the focusability in the view set focusable true

<EditText android:focusable="true"  android:focusableInTouchMode="false" ... />
neaGaze
  • 1,101
  • 20
  • 27
  • I think it would be more helpful for the op and further visotrs if you add some explanations to your intention. – Reporter Jul 01 '14 at 12:01
5

make your alert dialog box appear on

setOnFocusChangedListener()

Ashwani
  • 1,516
  • 14
  • 28
3

You should add onFocusChangeListener:

edt.setKeyListener(null);
edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus)
        {
            edt.callOnClick();
        }
    }
});
edt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
    }
});
Rahim Rahimov
  • 1,227
  • 14
  • 24
3

Avoid using a FocusChangeListener since it will behave erratically when you don't really need it (eg. when you enter an activity). Just set an OnTouchListener along with your OnClickListener like this:

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                view.requestFocus();
                break;
        }
        return false;
    }

This will cause your EditText to receive focus before your onClick call.

Andrei Lupsa
  • 2,496
  • 3
  • 22
  • 18
1

Instead of setting input type use "Editable=false" and "Focus=false" if you don't require keyboard.

It maybe helpful to you.

vishesh chandra
  • 6,701
  • 6
  • 31
  • 38
-1

This was a real problem for me when trying to reproduce a "click" sound from the EditText when the soft keyboard pops up; I was only getting a click every second time. What fixed it for me was the the opposite of what worked for @neaGaze. This worked for me in my_layout.xml :

<EditText android:focusable="true"  android:focusableInTouchMode="true" ... />

It allows the click sound/event to happen each time when user enters the EditText, while also allowing the soft keyboard to show. You have to handle the OnClickListener of course for this to happen, even if you do nothing with it, like so :

myEditText = (EditText) findViewById(R.id.myEditText);
...

// implement the onClick listener so we get the click sound and event if needed
myEditText.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        //do something or nothing; up to you
    }
});

Speaking of that pesky soft keyboard, if I finished from my Dialog style Activity with the soft keyboard up, no matter what I tried the keyboard remained up when I was returned to MainActivity. I had tried all the usual suggestions such as Close/Hide the Android soft keyboard , How to close Android soft keyboard programmatically etc. None of that worked.

In my case I did not need the soft keyboard in MainActivity. What did work was the following in my AndroidManifest.xml file, within the MainActivity section

<activity

    android:name=".MainActivity"
    android:windowSoftInputMode="stateAlwaysHidden">

</activity>