EditText onClickListener in Android - android

I want an EditText which creates a DatePicker when is pressed. So I write the following code:
mEditInit = (EditText) findViewById(R.id.date_init);
mEditInit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DATEINIT_DIALOG);
}
});
But when I press the EditText the action is the typical: a cursor waiting for typing text instead show the Dialog I want.

The keyboard seems to pop up when the EditText gains focus. To prevent this, set focusable to false:
<EditText
...
android:focusable="false"
... />
This behavior can vary on different manufacturers' Android OS flavors, but on the
devices I've tested I have found this to to be sufficient. If the keyboard still pops up, using hints instead of text seems to help as well:
myEditText.setText("My text"); // instead of this...
myEditText.setHint("My text"); // try this
Once you've done this, your on click listener should work as desired:
myEditText.setOnClickListener(new OnClickListener() {...});

Normally, you want maximum compatibility with EditText's normal behaviour.
So you should not use android:focusable="false" as, yes, the view will just not be focusable anymore which looks bad. The background drawable will not show its "pressed" state anymore, for example.
What you should do instead is the following:
myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// showMyDialog();
}
});
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// showMyDialog();
}
}
});
By setting the input type to TYPE_NULL, you prevent the software keyboard from popping up.
By setting the OnClickListener and OnFocusChangeListener, you make sure that your dialog will always open when the user clicks into the EditText field, both when it gains focus (first click) and on subsequent clicks.
Just setting android:inputType="none" or setInputType(InputType.TYPE_NULL) is not always enough. For some devices, you should set android:editable="false" in XML as well, although it is deprecated. If it does not work anymore, it will just be ignored (as all XML attributes that are not supported).

I had this same problem. The code is fine but make sure you change the focusable value of the EditText to false.
<EditText
android:id="#+id/date"
android:focusable="false"/>
I hope this helps anyone who has had a similar problem!

Default working of EditText:
On first click it focuses and on second click it handles onClickListener so you need to disable focus. Then on first click the onClickListener will handle.
To do that you need to add this android:focusableInTouchMode="false" attribute to your EditText. That's it!
Something like this:
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:inputType="text" />

Here is the solution I implemented
mPickDate.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
showDialog(DATE_DIALOG_ID);
return false;
}
});
OR
mPickDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
showDialog(DATE_DIALOG_ID);
}
});
See the differences by yourself.
Problem is since (like RickNotFred said) TextView to display the date & edit via the DatePicker. TextEdit is not used for its primary purpose.
If you want the DatePicker to re-pop up, you need to input delete (1st case) or de focus (2nd case).
Ray

If you use OnClick action on EditText like:
java:
mEditInit = (EditText) findViewById(R.id.date_init);
mEditInit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DATEINIT_DIALOG);
}
});
or kotlin:
editTextChooseDate.setOnClickListener {
showDialog(DATEINIT_DIALOG)
}
So, it will work perfectly if you put into xml of your EditText the following lines:
android:inputType="none"
android:focusable="false"
android:cursorVisible="false"
For example:
<EditText
android:id="#+id/date_init"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:hint="Select Date"
android:inputType="none"
android:focusable="false"
android:cursorVisible="false"/>
or for MaterialDesign
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/layoutEditTextChooseDate"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/date_init"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:hint="Select Date"
android:inputType="none"
android:focusable="false"
android:cursorVisible="false"/>
</com.google.android.material.textfield.TextInputLayout>

See https://gist.github.com/Reacoder/0b316726564f85523251:
editText.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
// onClick(editText)
}
}
editText.setOnTouchListener { _, motionEvent ->
if (motionEvent.action == MotionEvent.ACTION_DOWN) {
// onClick(editText)
}
false
}
=== Old answer ===
Nice topic. Well, I have done so.
In XML file:
<EditText
...
android:editable="false"
android:inputType="none" />
In Java-code:
txtDay.setOnClickListener(onOnClickEvent);
txtDay.setOnFocusChangeListener(onFocusChangeEvent);
private View.OnClickListener onOnClickEvent = new View.OnClickListener() {
#Override
public void onClick(View view) {
dpDialog.show();
}
};
private View.OnFocusChangeListener onFocusChangeEvent = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
dpDialog.show();
}
};

IMHO I disagree with RickNotFred's statement:
Popping a dialog when an EditText gets
focus seems like a non-standard
interface.
Displaying a dialog to edit the date when the use presses the an EditText is very similar to the default, which is to display a keyboard or a numeric key pad. The fact that the date is displayed with the EditText signals to the user that the date may be changed. Displaying the date as a non-editable TextView signals to the user that the date may not be changed.

The following works perfectly for me.
First set your date picker widget's input to 'none' to prevent the soft keyboard from popping up:
<EditText android:inputType="none" ... ></EditText>
Then add these event listeners to show the dialog containing the date picker:
// Date picker
EditText dateEdit = (EditText) findViewById(R.id.date);
dateOfBirthEdit.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
showDialog(DIALOG_DATE_PICKER);
}
return false;
}
});
dateEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
showDialog(DIALOG_DATE_PICKER);
} else {
dismissDialog(DIALOG_DATE_PICKER);
}
}
});
One last thing. To make sure typed days, months, or years are correctly copied from the date picker, call datePicker.clearFocus() before retrieving the values, for instance via getMonth().

This Works For me:
mEditInit = (EditText) findViewById(R.id.date_init);
mEditInit.setKeyListener(null);
mEditInit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
mEditInit.callOnClick();
}
}
});
mEditInit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DATEINIT_DIALOG);
}
});

Here is what worked for me
Set editable to false
<EditText android:id="#+id/dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Date of Birth"
android:inputType="none"
android:editable="false"
/>
Then add an event listener for OnFocusChange
private View.OnFocusChangeListener onFocusChangeDOB= new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
showDialog(DATE_DIALOG_ID);
}
}
};

As Dillon Kearns suggested, setting focusable to false works fine.
But if your goal is to cancel the keyboard when EditText is clicked, you might want to use:
mEditText.setInputType(0);

Why did not anyone mention setOnTouchListener? Using setOnTouchListener is easy and all right, and just return true if the listener has consumed the event, false otherwise.

The problem with solutions using OnFocusChangeListener is that they interpret any focus gain as a click. This is not 100% correct: your EditText might gain focus from something else than a click.
If you strictly care about click and want to detect click consistently (regardless of focus), you can use a GestureDetector:
editText.setOnConsistentClickListener { /* do something */ }
fun EditText.setOnConsistentClickListener(doOnClick: (View) -> Unit) {
val gestureDetector = GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() {
override fun onSingleTapUp(event: MotionEvent?): Boolean {
doOnClick(this#setOnConsistentClickListener)
return false
}
})
this.setOnTouchListener { _, motionEvent -> gestureDetector.onTouchEvent(motionEvent) }
}

For kotlin, you could use this
editText!!.showSoftInputOnFocus = false

Related

Edittext Keyboard Not Showing After First Input

So I need an input box for text in my android app.
I have this in my xml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/inputCodePA"
android:textSize="18sp"
android:inputType="text"
android:hint="#string/inputHint"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/arrowRightPA"
android:layout_toEndOf="#+id/arrowRightPA">
<requestFocus/>
</EditText>
and this:
public void setOnClickListners(){
final EditText inputBox = (EditText)findViewById(R.id.inputCodePA);
inputBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputBox.clearFocus();
inputBox.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputBox, InputMethodManager.SHOW_FORCED);
}
});
inputBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
inputBox.setText("");
inputBox.clearFocus();
inputBox.requestFocus();
return false;
}
});
}
For my code in my activity's class (setOnClickListner() is called in my onCreate()).
But, whenever after I type something into the EditText box and press Enter, I can not open the keyboard to enter text again.
I know I'm making a really basic error, but I can't seem to figure out what.
After all, this is the first time an EditText failed me.
HA!
I found out the problem!
It was because I had a ScrollView below the EditText in the code (which made the ScrollView become 'in front'), which did not allow the user to click on the view at all!
Hope this helps other people who makes these stupid mistakes!

android EditText imeOption OnClick

With a Button it is simple,
<Button
android:blablabla="blabla"
...
android:onClick="doSomething" />
this will preform the doSomething(View) function.
How can we mimic this with an EditText ?
I have read about this and i read that most people use an imeOptions (which still seems necessary) and then implement a actionListener on that EditText object.
This is were i'm lost.
Is there a way to implement the "Done"-action (or send or...) from our keyboard to a onClick function like we do with a Button, or do we need to explicitly implement the listener ?
Regards !
The below code will perform some action when you press the Done key in the softkeyboard.
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do your actions here that you like to perform when done is pressed
//Its advised to check for empty edit text and other related
//conditions before preforming required actions
}
return false;
}
});
Hope it helps !!
I am assuming what you are wanting to do is run some code when the EditText is clicked?
If so, I have found a solution from another thread on the site:
EditText myEditText = (EditText) findViewById(R.id.myEditText);
myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
then do this code here
}
}
});
via: A better way to OnClick for EditText fields?

Android click on EditText won't show softkeyboard

It happens when the user dismisses the softkeyboard and then tries to click/gain focus on the EditText again, nothing happens only the cursor is shown - I want to show the keyboard again.
I've tried:
Using an onclick event
Using a focuschanged event
Changing properties of the EditText (focusable etc...)
Note: I am currently using Paranoid Android. The EditText is Multiline.
I found the solution, I just had to remove the following attribute from my EditText:
android:textIsSelectable="true"
Please start by omitting any requestFocus defined for your EditText.
there's a known bug that prevents keyboard from showning if the latter is set.
If that doesn't work for you, create a focus listener and in it programmatically open the virt keyboard:
editTxt.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
// show keyboard
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editTxt, 0);
}
}
});
Here is a solution:
final InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText e= (EditText) findViewById(R.id.editText1);
e.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
imm.showSoftInput(e, InputMethodManager.SHOW_IMPLICIT);
}
});
e.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if(actionId==EditorInfo.IME_ACTION_GO){
imm.hideSoftInputFromWindow(e.getWindowToken(), 0);
//Do you work here
}
return false;
}
});
and the edittext will be:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionGo"/>

have to click two times to call an onclick method of edittext android

I have this editText in my android activity
<EditText
android:id="#+id/payment_expiration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/payment_expiration_label"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:clickable="true"
android:onClick="update_expiration_date"
android:editable="false"
android:layout_marginTop="-4dp"
android:cursorVisible="false"
android:maxLength="7"
android:padding="10dp"
android:textSize="13dp" />
as you can see when the user click on
I call this method which launch a datePickerDialog :
public void update_expiration_date(View v){
Log.i("","cliqué");
picker.show();
can_update_expiration_date = true;
}
the problem I encouter is : in the first time just when I open this activity, the user must click two times to launch the dialog
but after that, one click is sufficient
how can I fix this issue
Check Similar Question
"The first click just sets the focus to the TextBox then the second click actually gets handled as a click. "
try setting android:focusable="false"
You could try the focusable solution, or just add a state tracking variable...
public class YourActivity extends Activity {
private boolean clicked;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
clicked = false;
}
public void update_expiration_date(View v){
if(clicked == false){
clicked = true;
return;
} else {
Log.i("","cliqué");
picker.show();
can_update_expiration_date = true;
}
}
}
begiPass solution works but there are a better.
With focusable false the user never can´t see which edittext is selected.
The better solution is using onTouchListener. My example code:
edit.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
edit.setText("");
v.setOnTouchListener(null);
return false;
}
});
Don´t forget set OnTouchListener to null. We want just clear the hint (only once time).
Regards!
Use OnTouchListener handle it:
when (view) {
edtName -> {
if (motionEvent.action == MotionEvent.ACTION_UP) {
edtName.requestFocus()
//TODO
}
}
edtPassword -> {
if (motionEvent.action == MotionEvent.ACTION_UP) {
edtPassword.requestFocus()
//TODO
}
}
}
return false
}```

android how to write onclick event of edittext

I have a editText contianing text Enter ur name.. so i want to when user just click on that editText then the text disappear and editText should be empty and user can write anything their. so how can i do like this. plz give me ans
final EditText edittxt = (EditText) findViewById(R.id.youredittext);
edittxt.setOnFocusChangeListener(new OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus == true)
{
if (edittxt.getText().toString().compareTo("Your hint") == 0) // default text
{
edittxt.setText("");
}
}
}
});
For your requirement you don't need onClick event listener.
try this
<EditText
android:hint="Enter you name"
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
or from java code
editText.setHint("Enter you name");
android:hint = "text that you want the user to be assisted with while typing" is used for this feature.
This works well with EditText, autocompleteText view as well both in android and monodroid.
Try this code..
editText.setOnClickListener(new OnClickListener(){
public void onClick(View v){
((EditText)v).setText("");
}
});

Categories

Resources