How to display progress dialog before starting an activity in Android?

You should load data in an AsyncTask and update your interface when the data finishes loading. You could even start a new activity in your AsyncTask’s onPostExecute() method. More specifically, you will need a new class that extends AsyncTask: public class MyTask extends AsyncTask<Void, Void, Void> { public MyTask(ProgressDialog progress) { this.progress = progress; } … Read more

How to show a dialog to confirm that the user wishes to exit an Android Activity?

In Android 2.0+ this would look like: @Override public void onBackPressed() { new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle(“Closing Activity”) .setMessage(“Are you sure you want to close this activity?”) .setPositiveButton(“Yes”, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNegativeButton(“No”, null) .show(); } In earlier versions it would look like: @Override public boolean … Read more

Android DialogFragment vs Dialog

Yes, use DialogFragment and in onCreateDialog you can simply use an AlertDialog builder anyway to create a simple AlertDialog with Yes/No confirmation buttons. Not very much code at all. With regards handling events in your fragment there would be various ways of doing it but I simply define a message Handler in my Fragment, pass … Read more

How to dismiss the dialog with click on outside of the dialog?

You can use dialog.setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog. Something like, Dialog dialog = new Dialog(context) dialog.setCanceledOnTouchOutside(true); Or if your Dialog in non-model then, 1 – Set the flag-FLAG_NOT_TOUCH_MODAL for your dialog’s window attribute Window window = this.getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); 2 – Add another flag to windows properties,, … Read more

Android 1.6: “android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application”

Instead of : Context appContext = this.getApplicationContext(); you should use a pointer to the activity you’re in (probably this). I got bitten by this today too, the annoying part is the getApplicationContext() is verbatim from developer.android.com 🙁

How to handle screen orientation change when progress dialog and background thread active?

Edit: Google engineers do not recommend this approach, as described by Dianne Hackborn (a.k.a. hackbod) in this StackOverflow post. Check out this blog post for more information. You have to add this to the activity declaration in the manifest: android:configChanges=”orientation|screenSize” so it looks like <activity android:label=”@string/app_name” android:configChanges=”orientation|screenSize|keyboardHidden” android:name=”.your.package”> The matter is that the system destroys … Read more

Android Activity as a dialog

To start activity as dialog I defined it like this in AndroidManifest.xml: <activity android:theme=”@android:style/Theme.Dialog” /> Use this property inside your activity tag to avoid that your Dialog appears in the recently used apps list android:excludeFromRecents=”true” If you want to stop your dialog / activity from being destroyed when the user clicks outside of the dialog: … Read more

How to prevent a dialog from closing when a button is clicked

EDIT: This only works on API 8+ as noted by some of the comments. This is a late answer, but you can add an onShowListener to the AlertDialog where you can then override the onClickListener of the button. final AlertDialog dialog = new AlertDialog.Builder(context) .setView(v) .setTitle(R.string.my_title) .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick … Read more