toast.getView() and toast.setView() is deprecated

Viewed 1906

I want to show custom toast to users. But toast.getView() and toast.setView() is deprecated in android studio.

Here is my code:

Toast toast = Toast.makeText(context, "Show Toast", Toast.LENGTH_LONG);
View view = toast.getView();
view.setBackgroundResource(R.drawable.toast_background);
TextView text = view.findViewById(android.R.id.message);
text.setTextColor(Color.WHITE);
text.setPadding(15,0,15,0);
toast.show();



Now, how to customize toast in android studio(java)?

1 Answers

Create New Class

    public class WhiteCustomToast {
    public static void Make(Context context, String message) {
        Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
            View view = toast.getView();
            view.setBackgroundResource(R.drawable.white_cutom_bg);
            TextView text = (TextView) view.findViewById(R.id.message);
            //text.setTextColor(context.getResources().getColor(R.color.white));
            text.setTextColor(Color.parseColor("#FFFFFFFF"));
            text.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            text.setTypeface(Utilities.Ubuntu_Regular);
            text.setTextSize(14f);
        }
        toast.show();
    }
}
        
        

And Used in Main Class or Every Where

WhiteCustomToast.Make(context, "Checking");
Related