How to do something after user clicks on my EditText
Unlike most other controls, EditText
s 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" />
When the edit text is clicked how to set a value in edit screen of edittext then user can change it
Implement OnTouchListener
on your EditText & set/append text on MotionEvent.ACTION_DOWN
.
A better way to OnClick for EditText fields?
In General
You can achieve what you want to do via a combination of onFocus
and clearing the text field, similar to what the two commenters under your post already suggested. A solution would look like this:
EditText myEditText = (EditText) findViewById(R.id.myEditText);
myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// Always use a TextKeyListener when clearing a TextView to prevent android
// warnings in the log
TextKeyListener.clear((myEditText).getText());
}
}
});
Please always use a TextKeyListener to "clean" EditText, you can avoid a lot of android warnings in the log this way.
But...
I would much rather recommend you to simply set the following in your xml:
<EditText android:selectAllOnFocus="true"/>
Like described here. This way your user has a much better UI-feeling to it, he or she can decide on his/her own what to do with the text and won't be annoyed because it clears out every time!
android edittext remove focus after clicking a button
Put this in your button listener:
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
EDIT
The solution above will break your app if no EditText
is focused on. Modify your code like this:
add this method to you class:
public static void hideSoftKeyboard (Activity activity, View view)
{
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
Then, in your button listener, call the method like this:
hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of the class and v is the View parameter used in the button listener method onClick.
EditText inside TextInputLayout onclick requires 2 click ?! Android
Set the attribute android:focusableInTouchMode
to false
android:focusableInTouchMode="false"
in your edittext
xml code.
Explanation, as from docs, android:focusableInTouchMode
is:
Boolean that controls whether a view can take focus while in touch
mode. If this is true for a view, that view can gain focus when
clicked on, and can keep focus if another view is clicked on that
doesn't have this attribute set to true.
and the EditText
is true by default.
In other words: the first click will make the edittext
to gain focus and second click is the one that triggers the ClickListener
. So you should disable gaining focus on touch.
Edittext click event focus
Set the EditText to unFocusable. That way the first touch registers as a click.
In your XML for the EditText(s) in question add:
android:focusableInTouchMode="false"
Related Topics
Android Alternate Row Colors in Listview
Technical Details of Android Garbage Collector
Android Hello, Gallery Tutorial -- "R.Styleable Cannot Be Resolved"
Android Gridview Draw Dividers
Enabling Wifi on Android Emulator
Multiple Dex Files Define Lorg/Apache/Cordova/Buildhelper
Where Does Android View.Scrollto(X, Y) Scroll To
Android Taking Screenshot of Offscreen Page
How to Combine Bottomappbar + Fab with Bottomnavigationview
How to Save File from Website to Sdcard
Detect Back Button But Don't Dismiss Dialogfragment
Android - Hide All Shown Toast Messages
Voice Recognition Stops Listening After a Few Seconds
How to Write Text Over a Picture in Android and Save It
Java.Lang.Illegalstateexception: Fragment Not Attached to Activity