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?
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().
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With