Create own singleton class for toast massages

suggest change

Here is how to create your own singleton class for toast messages, If your application need to show success, warning and the danger messages for different use cases you can use this class after you have modified it to your own specifications.

public class ToastGenerate {
    private static ToastGenerate ourInstance;

    public ToastGenerate (Context context) {
        this.context = context;
                                            }
    public static ToastGenerate getInstance(Context context) {
        if (ourInstance == null)
            ourInstance = new ToastGenerate(context);
        return ourInstance;
    }

   //pass message and message type to this method
    public void createToastMessage(String message,int type){
   
//inflate the custom layout 
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout toastLayout = (LinearLayout) layoutInflater.inflate(R.layout.layout_custome_toast,null);
        TextView toastShowMessage = (TextView) toastLayout.findViewById(R.id.textCustomToastTopic);

        switch (type){
            case 0:
                //if the message type is 0 fail toaster method will call
                createFailToast(toastLayout,toastShowMessage,message);
                break;
            case 1:
                //if the message type is 1 success toaster method will call
                createSuccessToast(toastLayout,toastShowMessage,message);
                break;

            case 2:
                createWarningToast( toastLayout, toastShowMessage, message);
                //if the message type is 2 warning toaster method will call
                break;
            default:
                createFailToast(toastLayout,toastShowMessage,message);

        }
    }

//Failure toast message method
    private final void createFailToast(LinearLayout toastLayout,TextView toastMessage,String message){
        toastLayout.setBackgroundColor(context.getResources().getColor(R.color.button_alert_normal));
        toastMessage.setText(message);
        toastMessage.setTextColor(context.getResources().getColor(R.color.white));
        showToast(context,toastLayout);
    }

    //warning toast message method
    private final void createWarningToast( LinearLayout toastLayout, TextView toastMessage, String message) {
        toastLayout.setBackgroundColor(context.getResources().getColor(R.color.warning_toast));
        toastMessage.setText(message);
        toastMessage.setTextColor(context.getResources().getColor(R.color.white));
        showToast(context, toastLayout);
    }
//success toast message method
    private final void createSuccessToast(LinearLayout toastLayout,TextView toastMessage,String message){
        toastLayout.setBackgroundColor(context.getResources().getColor(R.color.success_toast));

        toastMessage.setText(message);
        toastMessage.setTextColor(context.getResources().getColor(R.color.white));
        showToast(context,toastLayout);
    }

    private void showToast(View view){
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.TOP,0,0); // show message in the top of the device
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Singleton Class for Toast Message:
* Create own singleton class for toast massages

Table Of Contents
2 Gradle
5 Intent
17 Service
19 WebView
31 SQLite
35 Glide
37 Dialog
38 ACRA
44 Handler
53 Toast
63 Menu
65 Picasso
70 Volley
71 Widgets
78 Realm
90 Spinner
95 OkHttp
108 TextView
109 ListView
111 Loader
118 Xposed
119 Security
121 ImageView
123 Doze Mode
130 Drawables
131 Colors
134 Fresco
139 AdMob
145 Keyboard
146 Button
150 EditText
155 Vk SDK
163 ExoPlayer
169 XMPP
175 OpenCV
177 Threads
184 ORMLite
186 TabLayout
190 LruCache
192 Zip files
194 Fastlane
199 FileIO
202 Moshi
210 VideoView
216 Paint
218 ProGuard
226 CleverTap
228 ADB shell
229 Ping ICMP
230 AIDL
234 Context
240 JCodec
242 Okio
249 FuseView
254 Looper
260 Singleton Class for Toast Message
261 Fastjson
263 Jackson
267 Smartcard