How to make a screenshot of the activity with alert dialog shown? - android

I use this code to make a screenshot. However, when I try to call it from the dialog (once user clicked the neutral button or when dialog.show() is called) - the dialog itself is not captured. When (or where) should I use this code to capture the dialog also?

Maybe extending Dialog and override onAttachedToWindow method to call your screenshot method from there would work. Something like this.-
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
takeScreenshot();
}

The code you are using uses drawing of root View to bitmap. Dialogs, Toasts and other extra windows have different View roots, therefore they do not appear in the screenshot.
Try out this library: https://github.com/jraska/Falcon it can solve your problem.

Related

Can a custom view get something like "onClose" or "onDestroy" event?

I have created a custom compound view by extending FrameLayout. That view can be on a dialogue box (among others). But what if I want to do some clean-up work when the view disappears from the screen, such as the user's closing the dialogue box? Can I get some kind of onDestroy event? Or should I make the owner (such as the dialogue box or the fragment) call the view's clean-up method on its (owner's) onDestroy or dismissed event?
try to use
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}

Dialog Problem (result without onclicklistener?)

Ist there a possibility to do something like that:
int selected_value = Dialog("This dialog show a combobox, i want to know which item is selected")
Or
String typed_chars = Dialog("This Dialog show a TextBox, i want to know the typed chars")
It is strongly recommended, that the code will stop while the dialog is shown and resume after dismissing the dialog, like the "showdialogforresult" method in c# or vb.net, I have to show lots of Dialogs and every dialog depends on former choices, I will become crazy if i have to code this with listener or callbacks...
While I don't think this is exactly possible like you do it, you do not need to code an anonymous class per Button callback.
Instead you can designate one class that implements DialogInterface.OnClickListener() and which you specify for all of the buttons. Its onClick() callback gets the information about which Dialog was invoked and which button was clicked. So you can operate within this onClick() method with some switch/case or if/else cascades.
Not perfect, but won't make you crazy :)

How to save a Progress Dialog state correctly?

In my app when i do some long work i'm using a Progress Dialog while the work isn't finished.
I'm looking for it in every place but without sucess. Everything that i founded is all about saving user interface elements states.
Then, i would like to know how i can save progress dialog state correctly ?
I want this working because when the orientation screen change the app crashes.
Shoul i use onSaveInstanceState() method ? How ? I try using saving as a bundle but without succes...
Any advice would be nice...
thanks
I think, your dialog crashes on orientation change because you are not using
#Override
protected Dialog onCreateDialog(int dialogID, Bundle args)
and
showDialog(dialogID);
to show your dialog.
I had the same problem with an alert dialog. As I see it, if you don't create dialog that way, it becomes linked to your activity, and when activity is dead, the system finds that a dialog still try to access that dead activity, not new one.
You need to add this to the manifest file for the activity in which you are showing the dialog:
android:configChanges="keyboardHidden|orientation"
to handle the screen orientation you must use onRetainNonConfigurationInstance()
you can find a more extensive explanation about screen orientation here: Faster Screen Orientation.
The way I do this in my applications is overriding the onConfigurationChanged() method in the Activity like so:
#Override
public void onConfigurationChanged(Configuration config) {
//Just switch the layout without respawning the activity
super.onConfigurationChanged(config);
}
This hasn't given me any issues as of yet and my Activity doesn't reload or restart when the orientation is changed.

Android Class which extends Dialog, how to clear TextViews before it is displayed

I have a class that extends android.app.Dialog, the layout is done in an xml file, and the setup (button listeners, etc) is done on the onCreate method. My problem is that whenever the dialog is displayed, then dismissed, and displayed again, the Editable TextViews are still populated with the information that was displayed previously. What is the common way to clear these text fields? Remember - this is a separate class that extends Dialog - so there is no 'onDialogCreate' like Activity has.
Or, perhaps I am extending the wrong class? There is just a lot of processing being done, and do not want to have all the code in the main Activity. I would like it to be in a separate Class. I tried to extend AlertDialog, but it does not create the border like Dialog does. Any help would be great.
The dialog is shown via the Activity:
protected Dialog onCreateDialog(int id) {
switch(id){
case DIALOG_NEW_SAFE:
return(new NewSafeDialog(this));
default:
return(null);
}
}
onCreateDialog(..) caches the dialog which means the same instance is reused.
3 ways to fix the undesired behavior off my head:
Override onPrepareDialog(..), use findViewById(..) to get whatever you want to clear, clear it.
Don't rely on managed dialogs at all, do new NewSafeDialog(this).show() each time you want to show the dialog.
Add onCancelListener(..), onDismissListener(..) inside your custom dialog that would call a method to clear itself.
The good way to create a dialog is by using showDialog() as you did so don't change it.
The good and easy way to force deletion of a dialog in order to make your creation code recalled again is:
void removeDialog (int id)
So if you simply do the following, it's gonna work ;)
removeDialog(DIALOG_NEW_SAFE);
showDialog(DIALOG_NEW_SAFE);
Try clearing the text in the constructor of the NewSafeDialog i.e. your dialog class.

Showing simple message dialogs

In my activity, I'd like to show simple info dialogs, stuff like:
new AlertDialog.Builder(context).setMessage(message).show();
if I do that, the dialog will leak when I rotate that phone (not to mention it will disappear as well, so the user may miss it). I can use the managed dialogs, but I'm not sure how you use it sensibly for these types of short messages? Looks like you have to do this:
showDialog(SOME_DLG_ID);
...
#Override
onCreateDialog(int id) {
if (id == SOME_DLG_ID) {
new AlertDialog.Builder(context).setMessage(message).show();
}
}
there's no way to pass what the message should be into onCreateDialog since its an override method. I'd hate to make a member variable of the parent activity that just stores whatever the current message should be. How do you all do it?
Thanks
if I do that, the dialog will leak
when I rotate that phone (not to
mention it will disappear as well, so
the user may miss it)
You can add
<activity
android:configChanges="orientation|keyboardHidden"
>
to your AndroidManifest.xml to prevent restarting the activity when the phone rotates. I am using it in my app and my AlertDialog survives the rotation of phone.
You can implement Activity.onPrepareDialog(int, Dialog) to switch out the message before the dialog is shown on the screen. So you could do something like:
#Override protected void onPrepareDialog(int id, Dialog dialog) {
if (id == SOME_DLG_ID) {
((AlertDialog) dialog).setMessage(message);
}
}
You'd still have to keep track of the message you're current showing in your activity, but at least this way, you're not creating a Dialog object for each message you want to show.
Using DialogFragment to manage the dialog ensures that it correctly handles lifecycle events such as when the user rotates the screen or presses the Back button.

Categories

Resources