Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to set the colour of a Toast's text

I am displaying a toast message as the result of an if statement using the following code:

Toast.makeText(getBaseContext(), "Please Enter Price", Toast.LENGTH_SHORT).show(); 

It is displayed as white text on a white background, as such it can not be read! My question is, how can I change the colour of the toast's text?

like image 461
super Avatar asked Jul 14 '11 01:07

super


People also ask

Can we change the color of toast message in Android?

If you need to change the background of the Toast then you need to use getView(). setBackground() function. If You need to change the color of the view created then you need to use getView(). getBackground().

How do you change the color of a toaster?

We need to create a drawable XML file to achieve this, we will create toast_drawable. xml and store it in the any of the drawable folders under res. We will add a solid tag to set the background color.


1 Answers

You can achieve this very easily, without creating a custom layout by modifying the default Toast :

Toast toast = Toast.makeText(this, resId, Toast.LENGTH_SHORT); TextView v = (TextView) toast.getView().findViewById(android.R.id.message); v.setTextColor(Color.RED); toast.show(); 

You can find the layout used by the default toast view in the Android SDK :

$ANDROID-SDK$/platforms/android-8/data/res/layout/transient_notification.xml

like image 103
XGouchet Avatar answered Sep 24 '22 03:09

XGouchet