2

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;
   }
Chillie
  • 1,030
  • 10
  • 28
Cady
  • 49
  • 7
  • 1
    Please don't modify the way dialog boxes are displayed / exited :). It's set that way to attempt to create a more unified user experience across all apps for all versions of Android. Your X will only confuse the user as they will then have OK, Cancel, Back button, and an X? – Jack Dec 29 '11 at 16:25
  • The posted code doesn't appear to have anything to do with dialogs. See the below answer, or read the Android help. http://developer.android.com/guide/topics/ui/dialogs.html – StrayPointer Dec 29 '11 at 17:20

1 Answers1

1

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..

Community
  • 1
  • 1
Ahmed Faisal
  • 4,397
  • 12
  • 45
  • 74
  • I tried it and I had no luck. Say I just have a button that is labeled close. What code can I input to close that when pressed? – Cady Dec 30 '11 at 14:26