Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Any would be Android developers out there?


Recommended Posts

Posted

I'm currently developing a tiny app that loads an AlertDialog. At the moment I have an activity and this loads the AlertDialog straight away. This method has two problems:

 

1) The main view loads behind the AlertDialog. I want the AlertDialog to display over the top of the home screen instead.

2) The AlertDialog is the only part of the app. When the user presses the back button it currently closes the AlertDialog and sits at the main view until the user presses back again. What I want is so this AlertView is the activity if that makes sense, so when the user presses back once it goes back to the home screen.

 

What's the best way to achieve this?

Posted

I've achieved 1 by simply using a translucent theme, as well as a hidden title bar. How do I go about testing for the alertdialog to close and then closing the app when it does.

 

when (alertdialog is not open)

{

close activity

}

Posted

    public boolean onOptionsItemSelected(MenuItem item) {
//        Button clickBtn;
       // make an alert dialog box for the autoChangeTime when over speed limit
      AlertDialog.Builder alert = new AlertDialog.Builder(this);

       alert.setTitle("Auto Change");
       alert.setMessage("Enter new value (in secs) for automatic speed limit change when in red zone");

       // Set an EditText view to get user input 
       final EditText input = new EditText(this);
       alert.setView(input);

       alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int whichButton) {
         Editable value = input.getText();
         autoChangeTime =  Integer.parseInt(value.toString());
         finish();

         }
       });

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


       debug("onOptionsItemSelected: " + item.getItemId() + " save: " + R.id.menuSaveKml);
       switch (item.getItemId()) {
       case R.id.menuAutoChangeTime:
               alert.show();
       
       break;

 

I just added the finish() inside my OK listener and it seems to do the trick.

 

regards

 

Simon

Posted
I just re-read the OP and realised you want it on the back button but hopefully sticking the finish in the cancel method will do the same trick :)

 

Simon

 

by cancel method I assume you mean setNegativeButton? If so, this didn't work.

Posted

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

    alert.setOnCancelListener(new DialogInterface.OnCancelListener() {
		public void onCancel(DialogInterface arg0) {
			// TODO Auto-generated method stub
	    	  finish();
		}
	});

Ok - i added the finish() to the setNegativeButton listener and added a new listener for the general cancel event (which seems to be invoked when I press the back button) :)

Seems to do the job for me :)

 

regards

 

Simon

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...