-1

This method makes my project crowded because i have many method like this. these may only differs on name of method and to the layout that that it views. My question is how can I lesser this kind of event?

void showLevel1Quest1() {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(context.getText(R.string.question));
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.add_question1, null);
    builder.setView(view);
    builder.setCancelable(true);
    final AlertDialog finishDialog = builder.create();
    View closeButton = view.findViewById(R.id.close);
    closeButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View clicked) {

            if (clicked.getId() == R.id.close) {
                finishDialog.dismiss();
            }
        }
    });
    finishDialog.show();
}
user3698267
  • 65
  • 1
  • 1
  • 7

2 Answers2

4

You can place a generic method in a separate class and just pass in the id of the layout you wish to use, such as:

/**
 * makeAlertBox
 * 
 * Populates an Android OS alert dialog with the passed params.
 * Only for quick messages that require no imput, other than to
 * dismiss the dialog
 * 
 * @param context - The application context
 * @param title - The dialog's title
 * @param message - The dialog's message
 * @param positiveText - The OK buttons text
 */
public static void makeAlertBox(Context context, String title, String message, 
        String positiveText, int layoutId)
{
    try
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layoutId, null);
        new AlertDialog.Builder(context)
        .setView(view)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton(positiveText, new DialogInterface.OnClickListener() 
        {               
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                dialog.cancel();
            }
        })
        .setCancelable(false)
        .show();
    }
    catch(Exception ex)
    {
        Log.e("UTIL", "Caught exception while attempting to create alertdialog");
        ex.printStackTrace();
    }
}

In my code my abstract class is called Utility So I'd call:

Utility.makeAlertBox(getApplicationContext(), "Title", "Message", "Okay!", someLayoutId);

edit You can obviously get rid of the extra parameters that you don't need. I mearly copied and pasted from my workspace, as an example.

edit 2 If you plan on using this code anywhere other than an Activity, you're going to need a reference to the Context/Application. Your best bet is to use a Singleton class, that inherits from the Application class, like so:

public class myApplication extends Application
{
    private static myApplication instance;

    @Override
    public void onCreate()
    {
        instance = this;
        super.onCreate();
    }

    /**
     * getInstance
     * @return Returns the instance of this myApplication
     */
     public static myApplication getInstance()
     {
         if(instance != null)    
            return instance;

         return new myApplication();
     }
}

Then when you need to access the context you can do: myApplication.getInstance() Or myApplication.getInstance().getApplicationContext()

You'll also need to update your Manifest to ensure the application is picked up:

<application
    android:name="com.YOURPACKAGE.myApplication"
    <!-- everything else. Such as Activites etc...-->
</application

Hope this helps.

laminatefish
  • 5,197
  • 5
  • 38
  • 70
  • so you mean sir I will create a new class? – user3698267 Jan 15 '15 at 15:56
  • You don't have to. But in the long run it would be better to refactor into it's own object, especially if it's only a dialog and not really needed to be inside an activity. Make a new abstract class and add this method (change it how you see fit) and then just call it as I've shown above, replacing Utility, with your own class name. – laminatefish Jan 15 '15 at 16:00
  • No sir. from a view class – user3698267 Jan 15 '15 at 16:27
  • Okay, in that case you'll need some kind of Singleton reference to the entire application (if you haven't got one already). See the answer here: http://stackoverflow.com/questions/13824293/android-getapplicationcontext-wont-work-in-view-subclass – laminatefish Jan 15 '15 at 16:31
  • See my last edit. Create that class and call it from within your View sub object as I've shown. :) – laminatefish Jan 15 '15 at 17:03
1

Here you are:

void showQuest(int layoutResId) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(context.getText(R.string.question));
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layoutResId, null);
        builder.setView(view);
        builder.setCancelable(true);
        final AlertDialog finishDialog = builder.create();
        View closeButton = view.findViewById(R.id.close);
        closeButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View clicked) {

                if (clicked.getId() == R.id.close) {
                    finishDialog.dismiss();
                }
            }
        });
        finishDialog.show();
    }

P.S. OOP rocks :)

localhost
  • 5,568
  • 1
  • 33
  • 53