How to create modal dialog box in android

i want create modal dialog box for my application.

so when modal dialog box open the other activities are blocked. no event are done like back button press or home button press.

  • How to get finish callback on setImageUrl with Volley library and NetworkImageView?
  • Android Activity Indicator?
  • What size should xhdpi launch icons be?
  • exception raised during rendering:Binary XML file line #-1:error inflating class<unknown>
  • Reset default values of Preference
  • Android Studio: Ice Cream Sandwich (API Level 15) Device Configuration not showing up even though system image is installed
  • and put two option button in that dialog box cancel and ok.

    Thank you…

    Related posts:

    android Maps API v2 with custom markers
    How to unregister BroadcastReceiver
    Check volume button usage when screen is off
    SpannableStringBuilder to create String with multiple fonts/text sizes etc Example?
    Could not resolve
    Notifications disappear as soon as service ends
  • Detecting outgoing call and call hangup event in android
  • Android WebView performance
  • Centering a background image in Android
  • Lacking privileges to access camera service in Android 6.0
  • why marginBottom not working?
  • How to query from MEDIA provider with “group by” option?
  • 4 Solutions collect form web for “How to create modal dialog box in android”

    There are many kind of Dialogs in Android. Please take a look at Dialogs. I guess what you are looking for is something like AlertDialog . This is the example of how you can implement on BackPress button.

    public void onBackPressed() {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Do you want to logout?");
        // alert.setMessage("Message");
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                //Your action here
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

    Use can use
    for the dialog itself, that should stop that dialog from closing by BACK and by tapping outside the dialog.

    You can’t override the HOME button.

    Try this::

    You need to create layout that you want to show in popup. you can create layout XML and use it like this:

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
                View layout = layoutInflater.inflate(R.layout.new_popup_layout, null);  
                final PopupWindow popupWindow = new PopupWindow(

    You can also provide click events of button like this:

    ImageButton btnChoose = (ImageButton) layout.findViewById(;
                btnChoose.setOnClickListener(new OnClickListener()  {
                    public void onClick(View v) {

    and show this popup like this: here you want to show this on button click then button view will be there.

     popupWindow.showAtLocation(anyview,Gravity.CENTER, 0, 0);

    Try out as below :

     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setMessage("Are you sure you want to exit?")
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
     .setNegativeButton(, new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
    AlertDialog alert = builder.create();

    For the Home Key event :

    No, it is not possible to get the Home key event in android.
    From the documentation of the Home keycode:

    public static final int KEYCODE_HOME

    Key code constant: Home key. This
    key is handled by the framework and is never delivered to

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.