I would like to know if the following code would be a good pattern to use Dialogs in Android, following the guidelines of dialogs (developer.android) and promoting encapsulation.
The example shows a dialog for choosing an option. The key point is the ChooseLevel class
that only needs to be modified at the point marked HERE to add constants for the options being presented to the user.
package org.dialogs;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
import org.dialogs.ChooseLevel.Level; // see below
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
public class MainActivity
extends Activity
implements ChooseLevel.Listener
{
// ...........................................................................
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
showDialog(ChooseLevel.ID);
} // ()
// ...........................................................................
@Override
protected Dialog onCreateDialog(int dialogId) {
Dialog dialog;
if (dialogId == ChooseLevel.ID) {
dialog = new ChooseLevel (this, this).getTheDialog();
}
return dialog;
}
// ...........................................................................
public void levelChosen(Level whatLevel) {
Toast.makeText(this, "level = " + whatLevel.toString(), Toast.LENGTH_LONG).show();
}
} //
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
class ChooseLevel
{
// ...........................................................................
public static final int ID = ChooseLevel.class.hashCode();
// ...........................................................................
public interface Listener {
void levelChosen(Level whatLevel);
}
// ...........................................................................
public enum Level {
Easy, Medium, Expert; // HERE: write constants names for the options
public static final String[] names;
static {
Level[] vals = Level.values();
names = new String[vals.length];
for (int i=0; i<vals.length; i++) {
names[i] = vals[i].toString();
}
} // static initializer
}
// ...........................................................................
private Listener theListener;
// ...........................................................................
private AlertDialog theDialog = null;
public AlertDialog getTheDialog () { return this.theDialog; }
// ...........................................................................
public ChooseLevel (Context ctx, Listener li) {
theListener = li;
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("choose a level");
builder.setSingleChoiceItems(Level.names, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int numChosen) {
dialog.dismiss();
Level theLevel = Level.valueOf(Level.names[numChosen]);
if (theListener != null) {
theListener.levelChosen(theLevel);
}
}
});
theDialog = builder.create();
} // ()
} // class
I’ve found a much easier way of displaying dialogs for choosing options:
the user code would be:
You only have to change the constants in
enum Optionsabove.All the tricks are done in the
Chooserclass, that needs no modification: