Question

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

Was it helpful?

Solution

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"

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top