DoFlamingo
DoFlamingo

Reputation: 232

show alertdialog while i press the screen

i have a list with surveys and all those surveys contains a picture. when you finish a survey the app returns to the overview where i can see all completed and all uncompleted surveys and all rejected surveys.

What i want:

Now if i click long on the screen and the survey isnt rejected and is completed i want to get a preview of the picture as long as i press the screen and disappear if i release the screen.

The problems are:

Here is my code with an onTouch and a longClick-listener and i want to eliminate one of them:

   row.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
               row.setOnLongClickListener(l -> {
                            // V3.0 only execute long click if the survey hasn't been declined
                            if (getContext() instanceof SurveyListPatientActivity && !erhebung.rejected() && !erhebung.isCompleted()) {
                                ((SurveyListPatientActivity) getContext()).showRejectSurvey(erhebung);

                            }else if(getContext() instanceof SurveyListPatientActivity && !erhebung.rejected() && erhebung.isCompleted()){
                                AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
                                ImageView imageView = new ImageView(getContext());
                                byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
                                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                                imageView.setImageBitmap(bitmap);
                                imageView.setPadding(10,10,10,10);
                                adb.setView(imageView);
                                adb.create().show();
                                if (event.getAction() == MotionEvent.ACTION_UP){
                                    // cancel AlertDialog
                                }
                            }
                            return true;
                        });
                        return false;
                }
        });

Thank you very much!

EDIT:

Whit the help of @brianoqr i changed my code into this. Everything works fine but the Dialog dosent disappear.

row.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
                ImageView imageView = new ImageView(getContext());
                byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                imageView.setImageBitmap(bitmap);
                imageView.setPadding(10,10,10,10);
                adb.setView(imageView);
                dialog = adb.create();
                dialog.show();
                isLongPressed = true;
                return true;
            }
        });
        row.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);
                System.out.println("1");
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    System.out.println("2");
                    if (isLongPressed) {
                        dialog.cancel();
                        System.out.println("canceld");
                        isLongPressed = false;
                    }
                }
                return false;
            }
        });

Upvotes: 2

Views: 98

Answers (1)

brianoqr
brianoqr

Reputation: 91

I probably would do it differently, but to answer the question how to cancel a dialog with out a button:

AlertDialog dialog = adb.create();
dialog.show()
if (event.getAction() == MotionEvent.ACTION_UP){
  dialog.dismiss()
}

The code you have written will not work as you want, the onlongpress is the leading action, you can structure your code something like in this ticket: Android - Detect End of Long Press

And your dialog will then need to be in a different scope to allow for automatic dismissal

EDIT

row.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
          if(!isLongPressed){
            isLongPressed = true;
            AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
            ImageView imageView = new ImageView(getContext());
            byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
            imageView.setImageBitmap(bitmap);
            imageView.setPadding(10,10,10,10);
            adb.setView(imageView);
            dialog = adb.create();
            dialog.show();
          }
            return true;
        }
    });
    row.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            System.out.println("1");
            if (event.getAction() == MotionEvent.ACTION_UP) {
                System.out.println("2");
                if (isLongPressed) {
                    dialog.cancel();
                    System.out.println("canceld");
                    isLongPressed = false;
                }
            }
            return false;
        }
    });

Upvotes: 1

Related Questions