Android: how to set Toast text color

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

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

It appears as white text on a white background, so it cannot be read! My question is: how to change the color of the toast text?

+46
android android-layout toast
Jul 14 2018-11-11T00:
source share
6 answers

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

+20
Jul 14 '11 at 2:01
source share
โ€” -

You can achieve this very easily without creating a custom layout, changing 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 default layout for the toast in the Android SDK:

$ ANDROID-SDK $ / platform / Android-8 / data / RES / layout / transient_notification.xml

+111
Feb 24 '12 at 15:03
source share

You might 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

+15
Jul 14 2018-11-11T00:
source share

The easiest way to change the background color of the toast and the background color of the toast:

 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(); 
+6
Mar 11 '15 at 7:51
source share

You can also use SpannableString . It can also color parts of a 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(); 
+5
Aug 04 '14 at 15:17
source share

Try using the Toasty library. It is very easy to use - https://github.com/GrenderG/Toasty

enter image description here

+1
Jun 30 '17 at 16:00
source share



All Articles