Development issue/problem:
I display the toast message as a result of the if declaration with the following code :
Toast.makeText(getBaseContext(), Enter the Price, Toast.LENGTH_SHORT).show() ;
It appears as a white text on a white background, so unreadable! My question is: how can I change the colour of the toast text?
How can I solve this problem?
Solution 1:
You can make your own kind of toast according to your needs. See Making a custom toast at http://developer.android.com/guide/topics/ui/notifiers/toasts.html.
Solution 2:
You can do this very easily without creating your own layout by using the standard Toast:
ToastToast = Toast.makeText(dit, resId, Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.setTextColor(Color.RED);
toast.show() ;
The default presentation of the Toast view is in the Android SDK :
$ANDROID-SDK$/platform/android-8/data/res/lay-out/transient_notification.xml
Solution 3:
You can make your own toast
–
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 your 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:
The easiest way to change the background colour of the toast and the background colour of the toast text:
View;
TextView text;
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,0);
view.setBackgroundResource(R.color.white);
toast.show() ;
Solution No 5:
You can also use SpannableString. It can also color parts of a rope.
SpannableString spanableString = 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 No 6:
Try the Toasty Library. It is very easy to use – https://github.com/GrenderG/Toasty
Solution No 7:
You can try this if you don’t want to use custom libraries.
Toast=Toast.makeText(getApplicationContext(),This is extended toast,Toast.LENGTH_LONG);
view=toast.getView();
view textview1=(TextView)view.findViewById(android.R.id.message) ;
view1.setTextColor(Color.YELLOW) ;
view.setBackgroundResource(R.color.colorPrimary) ;
toast.show() ;
Solution No 8:
Here is an example in Kotlin that shows how to change the background colour of the toast and the colour of the text:
val toast = Toast.makeText(context, text, Toast.LENGTH_SHORT)toast.view.background.setColorFilter(ContextCompatible.getColor(context,R.color.white), PorterDuff.Mode.SRC_IN)fall textView = toast.view.findViewById(android.R.id.message) as TextViewtextView.setTextColor(ContextCompat.getColor(context, R.color.black))toast.show())
Good luck!
Related Tags:
android toast default background color,toast background,how to change toast text size in android,android toast style,custom toast in android,toast gravity,toast getview deprecated,android toast font color,android toast bold text,android toast theme,how to set toast size in android