How can I change default toast message color and background color in android?

后端 未结 15 770
执念已碎
执念已碎 2020-12-23 17:12

I want to create a toast message with background color is white and message color is black. My toast message is:

Toast.makeText(Logpage.this, \"Please Give          


        
相关标签:
15条回答
  • 2020-12-23 17:42

    Heads Up, Updates to toasts in Android 11

    Custom toasts from the background are blocked, Android 11 protects users by deprecating custom toast views. For security reasons and to maintain a good user experience, the system blocks toasts that contain custom views if those toasts are sent from the background by an app that targets Android 11.

    addCallback() method added in Android R If you want to be notified when a toast (text or custom) appears or disappears.

    The most important text in toast API changes that for apps that target Android 11 the getView() method returns null when you access it, So, ensure to protect your apps from FATAL EXCEPTION, you know what I mean :)


    You can customize android native toast by using following code

    /**
     * ShowToast
     */
    public class ShowToast {
        public ShowToast(Context context, String info) {
            Toast toast = Toast.makeText(context, Html.fromHtml("<font color='#e3f2fd' ><b>" + info + "</b></font>"), Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 0, 0);
            toast.show();
        }
    }
    

    If you want to change the background you have to use custom layout in toast

    0 讨论(0)
  • 2020-12-23 17:44

    To keep round corners

    val toast = Toast.makeText(context, "Text", Toast.LENGTH_SHORT)
    toast.view.background.setTintList(ContextCompat.getColorStateList(context, android.R.color.darker_gray))
    toast.show()
    
    0 讨论(0)
  • 2020-12-23 17:44

    Short java code that keeps the round shape:

    Toast toast = Toast.makeText(context, "message", Toast.LENGTH_SHORT);
    toast.getView().setBackgroundTintList(ColorStateList.valueOf(Color.RED));
    toast.show();
    
    0 讨论(0)
  • 2020-12-23 17:46

    You can create the custom toast message like below :

    Toast toast = new Toast(context);
    toast.setDuration(Toast.LENGTH_LONG);
    
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.your_custom_layout, null);
    toast.setView(view);
    toast.show();
    

    One textview you can put inside the layout file and give the background and textcolor as you want.

    Also you can do the following which won't need the extra custom layout file :

    Toast toast = Toast.makeText(context, R.string.string_message_id, Toast.LENGTH_LONG);
    View view = toast.getView();
    view.setBackgroundResource(R.drawable.custom_background);
    TextView text = (TextView) view.findViewById(android.R.id.message);
    /*Here you can do anything with above textview like text.setTextColor(Color.parseColor("#000000"));*/
    toast.show();
    
    0 讨论(0)
  • 2020-12-23 17:46

    Kotlin Version:

    val toast:Toast = Toast.makeText(this, "Please enter your name !", Toast.LENGTH_SHORT)
    val view = toast.view
    view?.background?.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN)
    toast.show()
    
    0 讨论(0)
  • 2020-12-23 17:48

    Change default toast message color and background color in JAVA. You can change toast message color and background color this way..

            Toast toast=Toast.makeText(MainActivity.this,"Signin button is clicked.",Toast.LENGTH_SHORT);
            View view =toast.getView();
            view.setBackgroundColor(Color.GREEN);
            TextView toastMessage = (TextView) toast.getView().findViewById(android.R.id.message);
            toastMessage.setTextColor(Color.RED);
            toast.show();
    

    Just change toast text color this way..

            Toast toast = Toast.makeText(getApplicationContext(), "Signup button is clicked.",Toast.LENGTH_SHORT);
    
            TextView toastMessage=(TextView) toast.getView().findViewById(android.R.id.message);
            toastMessage.setTextColor(Color.BLUE);
            toast.show();
    
    0 讨论(0)
提交回复
热议问题