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

AndroidAndroid LayoutToast

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

You can create a custom Toast view to suit your requirements. See the section titled "Creating a Custom Toast View" at http://developer.android.com/guide/topics/ui/notifiers/toasts.html

Solution 3 - Android

You may want to create a custom Toast

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/toast_layout_root"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="10dp"
          android:background="#DAAA"
          >
<ImageView android:id="@+id/image"
           android:layout_width="wrap_content"
           android:layout_height="fill_parent"
           android:layout_marginRight="10dp"
           />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:textColor="#FFF"
          />
</LinearLayout>

-

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
                           (ViewGroup) findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

Source

Solution 4 - Android

The simplest way to change the background color of a toast and the background color of a toast's text:

View view;
TextView text;
Toast toast;
toast.makeText(this, resId, Toast.LENGTH_SHORT);
view = toast.getView();
text = (TextView) view.findViewById(android.R.id.message);
text.setTextColor(getResources().getColor(R.color.black));
text.setShadowLayer(0,0,0,0);
view.setBackgroundResource(R.color.white);
toast.show();

Solution 5 - Android

Try to use Toasty library. It really easy to use - https://github.com/GrenderG/Toasty

enter image description here

Solution 6 - Android

You can also use SpannableString. It can also color parts of the string.

SpannableString spannableString = new SpannableString("This is red text");
spannableString.setSpan(
                            new ForegroundColorSpan(getResources().getColor(android.R.color.holo_red_light)),
                            0,
                            spannableString.length(),
                            0);
Toast.makeText(this, spannableString, Toast.LENGTH_SHORT).show();

Solution 7 - Android

You can try this if you don't wish to use any custom libraries

 Toast toast=Toast.makeText(getApplicationContext(),"This is advanced toast",Toast.LENGTH_LONG);
        View view=toast.getView();
        TextView  view1=(TextView)view.findViewById(android.R.id.message);
        view1.setTextColor(Color.YELLOW);
        view.setBackgroundResource(R.color.colorPrimary);
        toast.show();

Solution 8 - Android

Here is an example in Kotlin, showing how you can change the background color of a toast and it's text color :

val toast = Toast.makeText(context, text, Toast.LENGTH_SHORT)
toast.view.background.setColorFilter(ContextCompat.getColor(context, 
R.color.white), PorterDuff.Mode.SRC_IN)
val textView = toast.view.findViewById(android.R.id.message) as TextView
textView.setTextColor(ContextCompat.getColor(context, R.color.black))
toast.show()

Solution 9 - Android

The solution with setting a custom view on Toast is deprecated for API 30 and forward.

Documentation says

> apps > * targeting API level {@link Build.VERSION_CODES#R} or higher that are in the background > * will not have custom toast views displayed.

The alternative is

Toast.makeText(applicationContext,
                HtmlCompat.fromHtml("<font color='red'>custom toast message</font>", HtmlCompat.FROM_HTML_MODE_LEGACY),
                Toast.LENGTH_LONG).show()

Html color tag can also be <font color='#ff6347'>

For every modification that has to do with the text displayed the above solution would be enough. You can for example make the text bold by inserting <b>my text</b> or you maybe want to change the font-familywith <font font-family='...'> my text </font> For all those changes that solution will be enough.

If you want to modify the container though with properties like background-color the only alternative is to use Snackbar. View can not be modified for Toast anymore.

Solution 10 - Android

https://developer.android.com/guide/topics/ui/notifiers/toasts?hl=es-419#java

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsuperView Question on Stackoverflow
Solution 1 - AndroidXGouchetView Answer on Stackoverflow
Solution 2 - AndroidMandelView Answer on Stackoverflow
Solution 3 - AndroidBrainCrashView Answer on Stackoverflow
Solution 4 - AndroidAkshay ShindeView Answer on Stackoverflow
Solution 5 - AndroidAnatolii ShubaView Answer on Stackoverflow
Solution 6 - AndroidClaus HolstView Answer on Stackoverflow
Solution 7 - AndroidMohamed SajjadhView Answer on Stackoverflow
Solution 8 - AndroidAnubhavView Answer on Stackoverflow
Solution 9 - AndroidPanagiotis BougioukosView Answer on Stackoverflow
Solution 10 - AndroidLeonardo GuevaraView Answer on Stackoverflow