Skip to content Skip to sidebar Skip to footer

How To Centralize A Custom Toast Creation

in my application i use a custom toast in almost all the activity. To create the custom toast i have the following method : private void getCustomToast(String message) { Layou

Solution 1:

You should make a custom abstract Activity that contains the toast method, and then extend that for your application's activities:

publicabstractclassToastActivityextendsActivity {

    protectedvoidgetCustomToast(String message)
    {
        LayoutInflaterli= getLayoutInflater();
        Viewtoastlayout= li.inflate(
                R.layout.toast_error, 
                (ViewGroup) findViewById(R.id.toast_layout));

        TextViewtext= (TextView) toastlayout.findViewById(R.id.toast_text);
        text.setText(message);

        Toasttoast=newToast(this);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(toastlayout);
        toast.show();
    }

}

Solution 2:

I want to say you can create a [static] class which will accept a Context object. Then use:

Toasttoast= Toast.makeText(context, text, duration).show();

So in your static class:

publicstaticclassUtility
{
    publicstaticvoidtoast(Context context, String msg)
         {
              Toast toast = Toast.makeText(context, msg, Toast.DURATION_LONG).show();

         }

}

Or something like that

Post a Comment for "How To Centralize A Custom Toast Creation"