21

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.

and put two option button in that dialog box cancel and ok.

Thank you...

Jatinkumar Patel
  • 1,094
  • 2
  • 17
  • 34

4 Answers4

33

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.

@Override
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
        }
    });

    alert.setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        });

    alert.show();

}
Ye Lin Aung
  • 11,234
  • 8
  • 45
  • 51
  • Why do you want to implement it on `HOME` key ? Some links for your ref. http://stackoverflow.com/questions/4783960/call-method-when-home-button-pressed-on-android & http://stackoverflow.com/questions/2079691/overriding-the-home-button-how-do-i-get-rid-of-the-choice – Ye Lin Aung Aug 22 '13 at 05:07
  • Fortunately you can't handle `HOME` key press. It would allow applications to do some ugly stuff. – Petr Peller May 22 '14 at 15:33
10

Use can use setCancellable(false); setCanceledOnTouchOutside(false); 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.

IuriiO
  • 611
  • 7
  • 13
9

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(
                    layout, 
                       LayoutParams.WRAP_CONTENT,  
                             LayoutParams.WRAP_CONTENT);

You can also provide click events of button like this:

ImageButton btnChoose = (ImageButton) layout.findViewById(R.id.btnChoose);
            btnChoose.setOnClickListener(new OnClickListener()  {

                @Override
                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);
Armaan Stranger
  • 3,140
  • 1
  • 14
  • 24
7

Try out as below :

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setMessage("Are you sure you want to exit?")
  .setCancelable(false)
   .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
        MyActivity.this.finish();
   }
 })
 .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
   }
});
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: http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME

public static final int KEYCODE_HOME

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

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Just a quick note: you may not want to hardcode "Yes" and "No" strings. Instead, you can use static resources like android.R.string.yes and android.R.string.no. – IgorGanapolsky Jun 03 '14 at 19:28