Toast message is the most simple way of providing feedback to the user. By default, android provide gray color toast message and we can set the message and the duration of the message. If we need to create more customizable and reusable toast message, we can implement our own custom toast message with custom layout. If we use the singleton method when implementing the toast message it will easy to maintain and we can use same instance throughout the application.
his code contains the way of creating the singleton class for android toast message. If your application need to show success, warning and the danger kind of message for different use cases you can use this class.
public class ToastGenerate { private static ToastGenerate ourInstance = new ToastGenerate(); public static ToastGenerate getInstance() { return ourInstance; } private ToastGenerate() { } //pass message and message type to this method public void createToastMessage(Context context,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(context,toastLayout,toastShowMessage,message); break; case 1: //if the message type is 1 success toaster method will call createSuccessToast(context,toastLayout,toastShowMessage,message); break; case 2: createWarningToast(context, toastLayout, toastShowMessage, message); //if the message type is 2 warning toaster method will call break; default: createFailToast(context,toastLayout,toastShowMessage,message); } } //Failure toast message method private final void createFailToast(Context context,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(Context context, 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(Context context,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(Context context,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(); } }
When you need to show a toast in your active you can use this class like this. In the onCreate method of the activity i created the object of ToastGenerate class.
ToastGenerate toastGenerate; @Override protected void onCreate(Bundle savedInstanceState) { toastGenerate = ToastGenerate.getInstance(); }
Then you can show any kind of toast message by calling the createToastMessage method in the ToastGenerate class. Of you need success kind of message ,pass 3rd parameter as 1 and if you need danger kind of message pass 3rd parameter as 0.
ToastGenerate toastGenerate;
@Override
protected void onCreate(Bundle savedInstanceState) {
toastGenerate = ToastGenerate.getInstance();
toastGenerate.createToastMessage(
context, "Toast create success", 1);
}
No comments:
Post a Comment