0

I am having a little issue when trying to use both onTouchListener and onCLickListener for the ok button within a dialog. Basically when I click on the OK button to dismiss the dialog, it recognises the onTouch but it doesn't perform the onClick where I want the dialog to close, the dialog remains open. What am I doing incorrectly in my implementation?

    questionButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // custom dialog
            final Dialog dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.custom_dialog);

            Button dialogButton = dialog.findViewById(R.id.dialog_button_OK);
            // if button is clicked, close the custom dialog

            dialogButton.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    setButtonPress(v, event);
                    return true;

                }
            });

            dialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();
        }
    });

public void setButtonPress(View v, MotionEvent event){
            int sdk = android.os.Build.VERSION.SDK_INT;
            Button view = (Button) v;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.love_heart_dark));
                    } else {
                        v.setBackground(getResources().getDrawable(R.drawable.love_heart_dark));
                    }
                    break;

                case MotionEvent.ACTION_UP:
                    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.love_heart));
                    } else {
                        v.setBackground(getResources().getDrawable(R.drawable.love_heart));
                    }
                    break;

                case MotionEvent.ACTION_CANCEL: {
                    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.love_heart));
                    } else {
                        v.setBackground(getResources().getDrawable(R.drawable.love_heart));
                    }
                    break;
                }
            }
Pristin
  • 175
  • 1
  • 2
  • 13
  • You can manage at a time only one listener , Whenever you work on touch listner you have to disable `onClickListnere` or have to work on `onClickListnere` disable touoch listner. – Farhana Naaz Ansari Jun 21 '18 at 05:33
  • have you seen [this](https://stackoverflow.com/questions/11578718/how-to-combine-onclicklistener-and-ontouchlistener-for-an-imagebutton). – Farhana Naaz Ansari Jun 21 '18 at 05:35
  • @farhana So is there a way to perform the ontouchlistener, disable it and then perform the onclick all in one if that makes sense? Basically the onTouch does the background colour change for the button so the user knows they selected it and then the onClick takes affect and navigate? I tried the answer below but see the comment to see my issue – Pristin Jun 21 '18 at 06:09
  • use drawable to selection, how can you manage two listeners simultaneously, you have to keep disable one for other listeners – Farhana Naaz Ansari Jun 21 '18 at 06:13
  • I added the code for setButtonPress so you can see what I am trying to do – Pristin Jun 21 '18 at 06:13
  • @farhana can you show an example please? – Pristin Jun 21 '18 at 06:14
  • You can set background without using any touch listener. , write code for setting background in `onClick` simple. It can change the background as well as your dialog also dismiss and if you have any condition related to setting background then handle these condition in `onClick`. – Farhana Naaz Ansari Jun 21 '18 at 06:20

2 Answers2

0

You cannot use two actions on a single view at once.

Try this

Boolean b = false;

questionButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // custom dialog
        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.custom_dialog);

        Button dialogButton = dialog.findViewById(R.id.dialog_button_OK);
        // if button is clicked, close the custom dialog

        dialogButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(b==false){
                setButtonPress(v, event);

                b=true;
                return true;
                }
                else{
                //perform on click here
                }
            }
        });

        dialog.show();
    }
});
  • I tried this before you posted your answer and it works but I do have one slight issue. Basically it dismisss the dialog when I touch the OK button (even when I hold the ok button). What I kind of want to do is the user see they have clicked the button (the setButtonPress method basically changes the background of the button colour). So on onTouch I cannot see this because the dialog dismisses straight away. I change this to onClick but then it seems to not like MotionEvent. Basically I just want the user to see that they've clicked the button – Pristin Jun 21 '18 at 06:05
  • see update in question so it makes more sense as I added the code to deal with button press – Pristin Jun 21 '18 at 06:14
  • Cool, giving it a go now – Pristin Jun 21 '18 at 06:42
  • Still seems to be doing the same thing unless the else statement is incorrect. In there I placed the dialog.dismiss code and return true statement – Pristin Jun 21 '18 at 06:53
0

Try this

        Button dialogButton = dialog.findViewById(R.id.dialog_button_OK);
        // if button is clicked, close the custom dialog

        dialogButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                setButtonPress(v, event);
                return false;

            }
        });

        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
mehul chauhan
  • 1,792
  • 11
  • 26