HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Toast functions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
toastfunctionsstackoverflow

Problem

I made this method for a better Toast function. But I have doubts about it because this Toast forced me to make two methods while using two types of contexts.

Now I would like to improve it by shortening my code and choosing a better and more professional approach (overall structure, splitting onto logical units, accordance to code conventions, etc.)

public static final void iToast(final Activity context, final int resourceId)
{
   Utils.iToast(context, context.getResources().getString(resourceId));
}

public static final void iToast(final Activity context, final String message)
{
   if (context == null)
   {
      return;
   }

   context.runOnUiThread(new Runnable()
   {
      @Override
      public void run()
      {
         Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_LONG).show();
      }
   });
}

Solution

I suggest you to use the new SnackBar class. It's always important to update the code with the latest features of android, that can improve your code and solve some android issues.

Your helper method might be something like this:

public static void showToast(final View view, final String toastMessage, final String callbackMessage, final Callable callback) {

    Snackbar snackBar = Snackbar
            .make(view, toastMessage, Snackbar.LENGTH_LONG);

    if (callback != null) {
        snackBar.setAction(callbackMessage, new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                try {
                    callback.call();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    snackBar.show();
}


The Callable parameter allow you to implement a Callable Interface so you can use the Command Pattern for calling a callback method.

Callable myCallback = new Callable() {

    @Override
        public Void call() {
            // Here you can do something as try to reload data from  
            // content provider or just nothing.
            return null;
        }
    });
}


The helper's method can then be called from every fragment, activity etc:

Utilities.showToast(getView(), "NO ITEMS", "RELOAD", myCallback);

Code Snippets

public static void showToast(final View view, final String toastMessage, final String callbackMessage, final Callable<Void> callback) {

    Snackbar snackBar = Snackbar
            .make(view, toastMessage, Snackbar.LENGTH_LONG);

    if (callback != null) {
        snackBar.setAction(callbackMessage, new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                try {
                    callback.call();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    snackBar.show();
}
Callable myCallback = new Callable<Void>() {

    @Override
        public Void call() {
            // Here you can do something as try to reload data from  
            // content provider or just nothing.
            return null;
        }
    });
}
Utilities.showToast(getView(), "NO ITEMS", "RELOAD", myCallback);

Context

StackExchange Code Review Q#105235, answer score: 4

Revisions (0)

No revisions yet.