2

I have dialogue box on my screen. When I touched on screen dialogue box closes. I want dialogue box should not close on outside touch.My code is given below :-

public class Dialogue extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialogue);
        this.setFinishOnTouchOutside(false);
        displayDialogue();

    }
    private void displayDialogue(){
        final AlertDialog.Builder myDialogue = new AlertDialog.Builder(this);
        myDialogue.setMessage("Please check your voice input output settings.It should be ON" );
        TextView messageView = new TextView(this);
        messageView.setGravity(Gravity.CENTER);
        myDialogue.setView(messageView);
        myDialogue.setPositiveButton("OK",  new DialogInterface.OnClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent i = new Intent(Dialogue.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        });

        AlertDialog dialog = myDialogue.create();
        dialog.show();
    }
CRUSADER
  • 5,486
  • 3
  • 28
  • 64

6 Answers6

3

setCancelable(boolean cancelable) : Sets whether the dialog is cancelable or not. Read More.

myDialogue.setCancelable(false);
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
3

You just need to set cancelable to false, so that when on touch outside the dialog will remain open as it is. Try this in your code:

myDialogue.setCancelable(false);
Pihu
  • 1,041
  • 11
  • 35
2

add this line

myDialogue.setCancelable(false);

Hope this helps..

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
2

set the cancelable property false

private void displayDialogue(){
final AlertDialog.Builder myDialogue = new AlertDialog.Builder(this);
myDialogue.setMessage("Please check your voice input output settings.It should be ON" );
TextView messageView = new TextView(this);
messageView.setGravity(Gravity.CENTER);
myDialogue.setView(messageView);

myDialogue.setCancelable(false);

myDialogue.setPositiveButton("OK",  new DialogInterface.OnClickListener()
{

    @Override
    public void onClick(DialogInterface dialog, int which) {
        Intent i = new Intent(Dialogue.this, MainActivity.class);
        startActivity(i);
        finish();
    }
});

AlertDialog dialog = myDialogue.create();
dialog.show();
}
Preet_Android
  • 2,332
  • 5
  • 25
  • 43
1

inisde displayDialog() method after .setMessage(), add the below line:

myDialogue.setCancelable(false);
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
1

You probably already got this but for future people this worked for me: you can override outside touches to do nothing on the dialog as shown in here https://stackoverflow.com/a/5831214

Community
  • 1
  • 1