I want to create a toast message with background color is white and message color is black. My toast message is:
Toast.makeText(Logpage.this, \"Please Give
static void CustomToast(Context context, String text, int duration,
@Nullable Integer backgroundColor,
@Nullable Integer textColor){
Toast t = Toast.makeText(context,text,duration);
if (backgroundColor != null)
t.getView()
.setBackgroundTintList(ColorStateList.valueOf(backgroundColor));
if (textColor != null)
((TextView)t.getView().findViewById(android.R.id.message))
.setTextColor(textColor);
t.show();
}
I was able to do it by setting the background color filter color and finding the toast resource ID and setting the text color.
Android.Graphics.Color
/// <summary> Creates and displays a toast with the given text. </summary>
public void Show(string message)
{
// Create the toast.
Toast toast = Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long);
// Set the background color.
Android.Graphics.Color c = new Android.Graphics.Color(122, 193, 66, 255);
Android.Graphics.ColorMatrixColorFilter CMF = new Android.Graphics.ColorMatrixColorFilter(new float[]
{
0,0,0,0,(float)c.R,
0,0,0,0,(float)c.G,
0,0,0,0,(float)c.B,
0,0,0,1,0
});
toast.View.Background.SetColorFilter(CMF);
// Set the text color.
toast.View.FindViewById<TextView>(Android.Resource.Id.Message).SetTextColor(new Android.Graphics.Color(0, 55, 103, 255));
// Display the toast.
toast.Show();
}
Kotlin Version:
val toast = Toast.makeText(this, getString(R.string.back_again), Toast.LENGTH_SHORT)
val view = toast.view
view.background.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN)
toast.show()
Create a layout file toast.xml as to how your toast should look as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/background_dark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a custom toast."
android:textColor="@android:color/white"
android:layout_gravity="center_vertical" />
</LinearLayout>
To show the toast in the java file put the below code:
public void showCustomAlert()
{
Context context = getApplicationContext();
// Create layout inflator object to inflate toast.xml file
LayoutInflater inflater = getLayoutInflater();
// Call toast.xml file for toast layout
View toastView = inflater.inflate(R.layout.toast, null);
Toast toast = new Toast(context);
toastView.setView(toast);
// Set layout to toast
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
Toast toast= Toast.makeText(YOUR ACTIVITY NAME ,Toast.LENGTH_SHORT);
View view =toast.getView();
view.setBackgroundColor(Color.GREEN); //any color your want
toast.show();
To Change the default Toast
Text Color and Background color Try Like this.
Toast toast = Toast.makeText(MainActivity.this, "Please Give Feedback...", Toast.LENGTH_LONG);
View view = toast.getView();
//To change the Background of Toast
view.setBackgroundColor(Color.TRANSPARENT);
TextView text = (TextView) view.findViewById(android.R.id.message);
//Shadow of the Of the Text Color
text.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
text.setTextColor(Color.BLACK);
text.setTextSize(Integer.valueOf(getResources().getString(R.string.text_size)));
toast.show();