1

What I want is a dialog without any button, and closing that dialog when I click outside the dialog body. Is it possible?

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openCredit();
    }

   //THIS IS ONE CUSTOM DIALOG
    public void openCredit(){ 
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = MainActivity.this.getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.activity_splash, null));
        builder.show();
    }
}

EDIT: I have search and found two functions setCanceledOnTouchOutside() and setCancelable(). First one is not working with my method of AlertDialog, giving error of "The method setCanceledOnTouchOutside(boolean) is undefined for the type AlertDialog.Builder". And 2nd one is just for pressing back button.

So, I change my code like below and it is now working. Thanks.

public void openCredit(){
        AlertDialog builder = new AlertDialog.Builder(this).create();
        LayoutInflater inflater = MainActivity.this.getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.activity_splash, null));
        builder.setCancelable(true);
        builder.show();
        builder.setCanceledOnTouchOutside(true);
    }
abdfahim
  • 2,523
  • 10
  • 35
  • 71
  • 1
    What did you learn when you Googled and searched here for this? – Simon Nov 28 '13 at 15:14
  • possible duplicate of [How to dismiss the dialog with click on outside of the dialog?](http://stackoverflow.com/questions/8384067/how-to-dismiss-the-dialog-with-click-on-outside-of-the-dialog) – Simon Nov 28 '13 at 15:15
  • my apology .. I tried googling with wrong keywords and missed the appropriate links .... – abdfahim Nov 28 '13 at 15:18

2 Answers2

0

Have you tried this..

builder.setCancelable(true);
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • as per reference, this function is for closing upon pressing back button, not clicking outside. Another functoin setCanceledOnTouchOutside does not works with AlertDialog. http://developer.android.com/reference/android/app/Dialog.html#setCancelable%28boolean%29 – abdfahim Nov 28 '13 at 15:33
  • @AbdullahFahim it must work i checked it and i used it. And `setCancelable(true)` for `AlertDialog` same as `setCanceledOnTouchOutside(true);` for `Dialog`. – Hariharan Nov 29 '13 at 00:35
0

Add this line for doing this

builder.setCancelable(true);
Smogger
  • 553
  • 5
  • 17