Cady
Cady

Reputation: 49

Having problems closing dialog box

I am a new to android development and need help. Please explain in detail and not just supply me with an answer that would be ideal.

My issue: I created a dialog box for my app and well it displays great, it dims the app and just opens the box but I am having issues closing it. If someone wanted to exit out of it, they would have to press the back arrow button. Yeah, this is not hard-work, but I would like my app to be nicely done and clean-cut. So I was wondering if there was a way to put an "X" at the top right corner to exit the dialog box?

If someone could add on to my code that would be perfect.Like I said, I am new to this and someone telling me just add this. I would not know where to add that code to.

My Code:

 @Override
   protected void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
   }

   public boolean onCreateOptionsMenu(Menu menu){
   super.onCreateOptionsMenu(menu);
   MenuInflater library = getMenuInflater();
   library.inflate(R.menu.main_menu, menu);
   return true;
}

   public boolean onOptionsItemSelected(MenuItem item){
      switch (item.getItemId()){
      case R.id.menuAus:
         startActivity(new Intent("com.tester.web.AUS"));
         return true;
      }
      return false;
   }

Upvotes: 2

Views: 546

Answers (1)

Ahmed Faisal
Ahmed Faisal

Reputation: 4427

You can also use a close button (BUTTON_NEGATIVE) if you like within the Dialog Box.

Also check out this answer: How to display a Yes/No dialog box in Android?

You should be easily able to customize that code as per your requirements. Good Luck!

Edit:

Put this line in OnCreate()

Context mcontext=this;

Now use this variable in following code

final AlertDialog.Builder alert = new AlertDialog.Builder(mcontext);  
alert.setTitle(title);                       
alert.setMessage(description);             
alert.setNegativeButton("Ok",new DialogInterface.OnClickListener()

//You can also run this without the overiding the method

{
              @Override                         
               public void onClick(DialogInterface dialog, int which)               
               {
                   dialog.cancel();
    });   
                          alert.show(); 

Try this code.. It is running successfully..you might need to customize it a bit as per your needs..

Upvotes: 1

Related Questions