54

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?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
super
  • 4,139
  • 4
  • 23
  • 20
  • I Hope [this](http://linkflows.blogspot.in/2014/08/creating-custom-toast-using-xml.html) will help you. [Check this Link.](http://linkflows.blogspot.in/2014/08/creating-custom-toast-using-xml.html) –  Aug 06 '14 at 10:46

10 Answers10

129

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

XGouchet
  • 10,002
  • 10
  • 48
  • 83
  • 63
    You can also do `toast.getView().setBackgroundColor(Color.RED);` to set the background color of the entire Toast area. – Chris Mar 25 '13 at 19:51
  • 2
    ^- On my telephone that adds a background behind the default grey background, though. – Stephan Bijzitter Mar 02 '17 at 19:42
  • @Chris Nice find! However it gave my Toast corners. This kept the shape intact: toast.getView().setBackgroundTintList(ColorStateList.valueOf(Color.RED)); – einUsername Dec 14 '20 at 07:47
22

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

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Mandel
  • 2,968
  • 2
  • 22
  • 19
16

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

BrainCrash
  • 12,992
  • 3
  • 32
  • 38
9

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();
Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
Akshay Shinde
  • 945
  • 10
  • 23
5

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

enter image description here

Anatolii Shuba
  • 4,614
  • 1
  • 16
  • 17
4

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();
VMAtm
  • 27,943
  • 17
  • 79
  • 125
Claus Holst
  • 901
  • 1
  • 8
  • 13
  • above single value **getColor(int)** method is deprecated so better to use double value method like **getColor(int,theme)**.EX:: **getColor(android.R.color.holo_red_light,getTheme())** – brahmy adigopula Aug 18 '16 at 12:24
2

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();
Mohamed Sajjadh
  • 139
  • 2
  • 11
0

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()
Anubhav
  • 1,984
  • 22
  • 17
0

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.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
0

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();