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

4

5

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

Fatherinlaw answered 16/1, 2014 at 15:49 Comment(2)
Where is your onClick listener? What do you have in it? Where and how do you define picker? Share your code please.Omura
Maybe my answer to this similar question may help: https://mcmap.net/q/415558/-i-have-to-click-the-button-twice-for-it-to-workDistillery
F
19

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"

Fifield answered 16/1, 2014 at 15:59 Comment(0)
A
0

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

}

}
A answered 16/1, 2014 at 16:1 Comment(0)
B
0

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!

Betray answered 7/3, 2017 at 18:1 Comment(0)
P
0

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
    }```

Patchy answered 8/4, 2019 at 4:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.