Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should regroup the code that builds the <code>Dialog</code> in a helper class.<br /> Below is an excerpt of a <code>DialogHelper</code> I have built for myself and that I use to show my applications' help files.</p> <pre><code>public class DialogHelper { public static AlertDialog showHelp(final Context ctx, final int resTitle, final int resFilename, final int resOk, final int resViewOnline, final int resOnlineUrl) { final WebView webview = new WebView(ctx); webview.loadUrl(ctx.getString(resFilename)); AlertDialog.Builder builder = new AlertDialog.Builder(ctx); builder.setTitle(resTitle) .setView(webview) .setCancelable(false) .setPositiveButton(resOk, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }) .setNegativeButton(resViewOnline, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { final Uri uri = Uri.parse(ctx.getString(resOnlineUrl)); final Intent intent = new Intent(Intent.ACTION_VIEW, uri); ctx.startActivity(intent); } } ); final AlertDialog dlg = builder.create(); dlg.show(); return dlg; } } ... other kinds of dialog take place here ... } </code></pre> <p>This way I just call </p> <pre><code>DialogHelper.showHelp(context, R.string.helpTitle, R.string.localizedFilename, R.string.labelOk, R.string.labelViewOnlineHelp, R.string.onlineHelpUrl); </code></pre> <p>from all my applications. That's a lot of parameters but this is workable.</p> <p>There is a non-nice thing in that code: I use <code>setNegativeButton()</code> for something else than its intended purpose. This is something I should refactor, but this doesn't change anything in the approach.</p> <p>About the parameters of <code>showHelp()</code>: they are <code>final</code> as they are used in the anonymous classes built within the method. This is a requirement of the compiler.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload