OnClick event only works second time on edittext

Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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;
	}
});

Solution 3 - Android

Nothing much to do you just have to

edt.setFocusable(false);

Solution 4 - Android

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" ... />

Solution 5 - Android

make your alert dialog box appear on

> setOnFocusChangedListener()

Solution 6 - Android

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());
    }
});

Solution 7 - Android

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.

Solution 8 - Android

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

It maybe helpful to you.

Solution 9 - Android

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobby SmetView Question on Stackoverflow
Solution 1 - AndroidSimon DorociakView Answer on Stackoverflow
Solution 2 - AndroidCarnalView Answer on Stackoverflow
Solution 3 - Androidraghav chopraView Answer on Stackoverflow
Solution 4 - AndroidneaGazeView Answer on Stackoverflow
Solution 5 - AndroidAshwaniView Answer on Stackoverflow
Solution 6 - AndroidRahim RahimovView Answer on Stackoverflow
Solution 7 - Androiduser2154462View Answer on Stackoverflow
Solution 8 - AndroidVishesh ChandraView Answer on Stackoverflow
Solution 9 - AndroidJoe SeibertView Answer on Stackoverflow