Question

I have an android app in which when the users touch or click the EditText, the content of the EditText is shown in the AlertDialog. I added a done Button to it, but the AlertDialog does not dismiss. I have to press the done button twice. I am not able to know why this is happening and can any anyone suggest a better alternative other than toast?

Coding Part: I am calling the following function on touch.

public void setalert()
{
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            mContext);

    // set title


    // set dialog message
    alertDialogBuilder
    .setMessage(etDesc.getText().toString())
    .setCancelable(false)


    .setNegativeButton("Done",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            // if this button is clicked, just close
            // the dialog box and do nothing

            dialog.cancel();                                                            

        }
    });                 

    // show it
    alertDialogBuilder.show();

}
Was it helpful?

Solution 2

This under your OnCreate

ettext.setOnClickListener(new View.OnClickListener() 
            {
                    @Override
                    public void onClick(View v) 
                    {
                        setalert2();
                    }

                });

This under your program

 public void setalert2()
        {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    mContext);

                // set title


                // set dialog message
                alertDialogBuilder
                    .setMessage(ettext.getText().toString())
                    .setCancelable(true);







                    // show it
                alertDialogBuilder.show();

                }

OTHER TIPS

Its because touch event is called twice:

1.MotionEvent.ACTION_DOWN

when user finger down on edittext.

2.MotionEvent.ACTION_UP

when user finger up from edittext.

to avoid this do it like this:

@Override
public boolean onTouch(View view, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
       setalert();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top