I'm trying to create a PdfDocument with android out side the view of the user. Basically, I want to generate PdfDocument that is different than the view that is currently being displayed. Once this is created, it is emailed to the user. I added all my content that I want in the pdf to an AlertDialog and on the dismiss, I save the view to pdf and send the email.
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final View dialogView = inflater.inflate(R.layout.receipt_layout, null);
... add stuff to view
builder.setView(dialogView);
final Dialog dialog = builder.create();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
... save pdf and send email
}
}
dialog.show();
WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
dialog.getWindow().setLayout(370, (int)dpHeight);
//dialog.dismiss(); // when uncommented, it produces a blank document
This works fine when the alert displays and I manually trigger the dismiss. However, when I add the dialog.dismiss() after showing the dialog, it produces a blank document. I'm not sure if there is an event I can tap into indicating that the view is visible/prime for pdf creation and then trigger the dismiss()? Or maybe there is an easier way to do this outside the view of the user (a flicker is fine)?