Hightower Posted October 18, 2010 Posted October 18, 2010 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?
SimpleSi Posted October 18, 2010 Posted October 18, 2010 COuld you acheive 2 by calling finish() type method which would close the app Activity | Android Developers And maybe what you need to do is make your app a widget to acheive 1? regards Simon
Hightower Posted October 18, 2010 Author Posted October 18, 2010 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 }
SimpleSi Posted October 18, 2010 Posted October 18, 2010 I'll have to have a closer look at the code for my app tonight to see if/how to do it Simon
SimpleSi Posted October 18, 2010 Posted October 18, 2010 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
SimpleSi Posted October 19, 2010 Posted October 19, 2010 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
Hightower Posted October 19, 2010 Author Posted October 19, 2010 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.
SimpleSi Posted October 19, 2010 Posted October 19, 2010 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now