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

10

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?

Bilocular answered 14/7, 2011 at 1:57 Comment(1)
I Hope this will help you. Check this Link.Kizer
S
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

Simdars answered 14/7, 2011 at 2:1 Comment(0)
C
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

Conias answered 24/2, 2012 at 15:3 Comment(3)
You can also do toast.getView().setBackgroundColor(Color.RED); to set the background color of the entire Toast area.Choosey
^- On my telephone that adds a background behind the default grey background, though.Keeton
@Choosey Nice find! However it gave my Toast corners. This kept the shape intact: toast.getView().setBackgroundTintList(ColorStateList.valueOf(Color.RED));Glandular
S
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

Simdars answered 14/7, 2011 at 2:1 Comment(0)
N
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

Northway answered 14/7, 2011 at 2:3 Comment(0)
L
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();
Louisiana answered 11/3, 2015 at 7:51 Comment(0)
Z
5

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

enter image description here

Zajac answered 30/6, 2017 at 16:0 Comment(0)
M
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();
Motoring answered 4/8, 2014 at 15:17 Comment(1)
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())Epley
S
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();
Shephard answered 8/3, 2020 at 20:9 Comment(0)
M
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()
Maryjanemaryjo answered 7/6, 2020 at 15:16 Comment(0)
S
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.

Slather answered 3/3, 2021 at 16:43 Comment(0)
P
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();
Pyrexia answered 17/4, 2021 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.