background-shape

We all love to add toast in places we want to check something, check on click events, give some short messages to user they have endless use. But putting a same toast for different kind of messages such as success, error, alert can be confusing and may not be visually alarming to users.

So what is the solution? We can design custom views and then inflate them in Toast. Below are some kind of examples we will be implementing, to create our custom Toast which will look cool and visually satisfying to users.

Custom Toast

To implement above 3 Custom toasts i.e.

  1. Success Toast
  2. Waiting Toast
  3. Custom Image Icon Toast

We shall write a generic code where only background color, icon, card radius only need to change.

These names are not default we will just be using it to proceed ahead to implement them.

Step 1

Let’s create the layout for it as res/layout/custom_toast.xml.

Step 2

Now lets call it from our Application Class, so that it can be called from any activity without writing extra code.

public void showToast(Context context,String message){
    // setup toast layout
    SuccessToastLayoutBinding binding = SuccessToastLayoutBinding.inflate(LayoutInflater.from(context));
    binding.message.setText(msg);

    // setup toast
    Toast toast = new Toast(context);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(binding.getRoot());
    toast.show();
}

We have inflated the layout using viewBinding(A Jetpack Library) above.

Just now make changes to your xml file to create different and new visually cool layouts 😎.

Hope it helped. Happy Coding 😀