How to do something after user clicks on my EditText

AndroidAndroid EdittextOnclicklistener

Android Problem Overview


I have an EditText that shows time. After user clicks the EditText I want to show a TimePickerDialog, so I set a View.OnClickListener to my EditText.

But the OnClickListener is behaving weirdly - I touch the EditText and then software keyboard appears (which I don't want). When I touch again, OnClickListener.onClick() is finally called and the dialog appears.

What should I do if I want the dialog to appear immediately?

Android Solutions


Solution 1 - Android

Unlike most other controls, EditTexts are focusable while the system is in 'touch mode'. The first click event focuses the control, while the second click event actually fires the OnClickListener. If you disable touch-mode focus with the android:focusableInTouchMode View attribute, the OnClickListener should fire as expected.

<EditText
        android:text="@+id/EditText01"
        android:id="@+id/EditText01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="false" />

Solution 2 - Android

Another solution is to use the ontouchlistener:

edittext.setOnTouchListener(new OnTouchListener() {
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(MotionEvent.ACTION_UP == event.getAction()) {
			mQuaternion_1.setText("" + mQ1);
		}

		return true; // return is important...
	}
});

If it returns true the event is handled and keyboard wont popup. If you'd want the keyboard to still popup and register click you'd have it return false.

Solution 3 - Android

It sounds like you don't want the user to actually be able to type in the EditText. You just want them to be able to pick a time via a time picker. So why not just a button that pops up a TimePickerDialog? You could display the time that was picked in a TextView.

Or you could just replace the EditText view with a TimePicker view (not a dialog, just a regular view).

Solution 4 - Android

I solved this by using a customized Button like this:

<Button
        android:id="@+id/btTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="test"
        android:textSize="20dp"
        android:background="@android:drawable/edit_text" />

Solution 5 - Android

Instead of touch Listener use Text Change Listener:

editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //yourFunction();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Solution 6 - Android

If I understand correctly you just need something like

<EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
	android:layout_width="wrap_content" android:layout_height="wrap_content"
	android:clickable="true" android:inputType="none" />

not editable and clickable. Set the OnClickListener and you're done. In theory, in the practice you should add too

android:editable="false" 

which is deprecated but does the trick.

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
QuestionfhuchoView Question on Stackoverflow
Solution 1 - AndroidKaiView Answer on Stackoverflow
Solution 2 - AndroidWarpzitView Answer on Stackoverflow
Solution 3 - AndroidMark BView Answer on Stackoverflow
Solution 4 - AndroidfhuchoView Answer on Stackoverflow
Solution 5 - AndroidKumar VLView Answer on Stackoverflow
Solution 6 - AndroidDiego Torres MilanoView Answer on Stackoverflow