0

Details:

I've an EditText in my activity with right drawable(calender icon). If I click on either EditText or on calendar icon datepicker dialog box should open.

Problem:

If I click on EditText datepicker dialog box is opened properly, but if I click on the Calendar icon at first time nothing is happening. If I click on the same icon one more time, then datepicker dialog is open.

I already implemented ontouch for right drawable given below:

 @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int DRAWABLE_RIGHT = 2;


            if (event.getAction() == MotionEvent.ACTION_UP) {
                mValidationId = (Integer) v.getTag();

                if (mEditText.getCompoundDrawables()[DRAWABLE_RIGHT] != null
                        && !isErrorIconDisplayedForCalendarIcon) {
                    if (event.getRawX() >= (mEditText.getRight() - mEditText.getCompoundDrawables()[DRAWABLE_RIGHT]
                            .getBounds().width())) {
                        isErrorIconDisplayedForCalendarIcon = mValidationId == RegistrationValidator.VALIDATE_DATEOFBIRTH;
                        ViewUtils.hideVirturalKeyboard(mEditText);
                        mListener.OnErrorIconClick(mValidationId);
                        return true;
                    } else {
                        HideErrorIcon();
                        if (mValidationId == RegistrationValidator.VALIDATE_DATEOFBIRTH) {
                            mEditText.setCursorVisible(false);
                                mListener.OnCalendarIconClick();
                            ViewUtils.hideVirturalKeyboard(mEditText);
                        } else {
                            v.requestFocus();
                            ViewUtils.showVirturalKeyboard(mContext.getApplicationContext());

                            if (mValidationId == RegistrationValidator.VALIDATE_USERNAME ||
                                    mValidationId == RegistrationValidator.VALIDATE_PASSWORD) {
                                RegistrationUserCredentialsController controller = (
                                        (RegistrationUserCredentialsController) mDsmApplication
                                                .getController(RegistrationUserCredentialsController.TAG));
                                if (controller != null) {
                                    controller.OnRegistrationItemEditStart(mValidationId);
                                }
                            }
                        }
                    }
                } else {
                    isErrorIconDisplayedForCalendarIcon = false;
                    if (mValidationId == RegistrationValidator.VALIDATE_DATEOFBIRTH) {
                        mEditText.setCursorVisible(false);
                        mListener.OnCalendarIconClick();
                        ViewUtils.hideVirturalKeyboard(mEditText);
                    } else {
                        if (mCurrentEditText != null && !mEditText.equals(mCurrentEditText)) {
                            mListener.OnRegistrationItemEditCompleted(
                                    mCurrentEditText.getText().toString(), (Integer) mCurrentEditText.getTag()
                            );
                        }
                        mCurrentEditText = (CustomTextInputEditText) v;
                        v.requestFocus();
                        ViewUtils.showVirturalKeyboard(mContext.getApplicationContext());

                        if (mValidationId == RegistrationValidator.VALIDATE_USERNAME ||
                                mValidationId == RegistrationValidator.VALIDATE_PASSWORD) {
                            RegistrationUserCredentialsController controller = (
                                    (RegistrationUserCredentialsController) mDsmApplication
                                            .getController(RegistrationUserCredentialsController.TAG));
                            if (controller != null) {
                                controller.OnRegistrationItemEditStart(mValidationId);
                            }
                        }
                    }
                }

            }
            return false;
        }

In the above code, I have an issue with date of birth related Edittext. If I click on right drawable underline color of Edittext is changing and after second click, the date picker dialog is open.

Difster
  • 3,264
  • 2
  • 22
  • 32
shree
  • 158
  • 2
  • 11

2 Answers2

0

Try below may help you.It works for me.

mEditText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final int DRAWABLE_LEFT = 0;
        final int DRAWABLE_TOP = 1;
        final int DRAWABLE_RIGHT = 2;
        final int DRAWABLE_BOTTOM = 3;

        if(event.getAction() == MotionEvent.ACTION_UP) {
            if(event.getRawX() >= (mEditText.getRight() - mEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                // your action here

             return true;
            }
        }
        return false;
    }
});
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57
-1

Add this line in your AndroidManifest.xml for that activity

android:launchMode = "singleInstance"
Sunil P
  • 3,698
  • 3
  • 13
  • 20