Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7766457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:25:21+00:00 2026-06-01T15:25:21+00:00

I would like to know if the following code would be a good pattern

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:25:23+00:00Added an answer on June 1, 2026 at 3:25 pm

    I’ve found a much easier way of displaying dialogs for choosing options:

    the user code would be:

    import android.app.Activity;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.widget.Toast;
    
    // -----------------------------------------------------------------------------
    // -----------------------------------------------------------------------------
    public class MainActivity 
        extends Activity 
        implements Chooser.Listener
    {
        // .....................................................................
        private Chooser levelChooser;
    
        // .....................................................................
        enum Options { one, two, tree, four};
    
        // .....................................................................
        // .....................................................................
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
    
            levelChooser = new Chooser (Options.class, "choose an option", this, this);
    
            showDialog(levelChooser.ID);
        } // ()
    
        // .....................................................................
        // .....................................................................
        @Override
        protected Dialog onCreateDialog(int dialogId) {
            Dialog aDialog = null;
    
            if (dialogId == levelChooser.ID) {
                aDialog = levelChooser.getTheDialog();
            }
            return aDialog;
        }
    
        // .....................................................................
        // .....................................................................
        public <T extends Enum<T>> void onChoiceMade(T theChoice) {
            Toast.makeText(this, "choice = " + theChoice, Toast.LENGTH_LONG).show();
        }
    } // class
    

    You only have to change the constants in enum Options above.
    All the tricks are done in the Chooser class, that needs no modification:

    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import java.lang.reflect.Method;
    
    public class Chooser {
    
        // .....................................................................
        // .....................................................................
        public static <T extends Enum<T>> String[] getNames(Class<T> c) {
            // returns the values of *any* enum in an array of String
            String[] values;
            try {
                Method m1 = c.getMethod("values");
                Object[] valuesObj;
                valuesObj = (Object[]) m1.invoke(c);
                values = new String[valuesObj.length];
                for (int i = 0; i < valuesObj.length; i++) {
                    values[i] = valuesObj[i].toString();
                }
            } catch (Exception ex) {
                values = null;
            }
            return values;
        } // ()
    
        // .....................................................................
        // .....................................................................
        public interface Listener {
            public <T extends Enum<T>> void onChoiceMade(T theChoice);
        }
    
        // .....................................................................
        // .....................................................................
        public final int ID;
        private Listener theListener;
        private AlertDialog.Builder theBuilder;
        private AlertDialog theDialog = null;
        private final String[] names;
    
        // .....................................................................
        // .....................................................................
        public final AlertDialog getTheDialog() {
            if (this.theDialog == null) {
                this.theDialog = this.theBuilder.create();
            }
            return this.theDialog;
        }
    
        // .....................................................................
        // .....................................................................
        public <T extends Enum<T>> Chooser(final Class<T> theEnum, String theTitle, Context ctx, Listener listener) {
    
            this.ID = theEnum.hashCode();
            this.names = getNames(theEnum);
            this.theListener = listener;
            this.theBuilder = new AlertDialog.Builder(ctx);
            this.theBuilder.setTitle(theTitle);
    
            this.theBuilder.setSingleChoiceItems(names, -1, new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface dialog, int electedNumber) {
                    dialog.dismiss();
                    Enum.valueOf(theEnum, names[electedNumber]);
    
                    T theOption = (T) Enum.valueOf(theEnum, names[electedNumber]);
                    if (theListener != null) {
                        theListener.onChoiceMade(theOption);
                    }
                }
            });
    
        } // ()
    } // class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if it is safe to use the following code
I have the following example code and would like to know what kind of
I would like to know if the following code is a good way to
I would like to know if I can use the following JQuery function on
I would like to know following: I added the folder Graphics into my project
I would like to know the answer for the following questions: Identify device types
I would like to know other people's opinion on the following style of writing
I have the following issue and I would like to know what exactly happens.
I've come across the following piece of JavaScript and would like to know what
I have links in the following structure and I would like to know what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.