67

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 Feedback...", 3000).show();

I wanted to create it in another method not in onCreate().

peterh
  • 11,875
  • 18
  • 85
  • 108
Saranya
  • 733
  • 1
  • 6
  • 7

16 Answers16

120

You can create the custom toast message like below :

Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.your_custom_layout, null);
toast.setView(view);
toast.show();

One textview you can put inside the layout file and give the background and textcolor as you want.

Also you can do the following which won't need the extra custom layout file :

Toast toast = Toast.makeText(context, R.string.string_message_id, Toast.LENGTH_LONG);
View view = toast.getView();
view.setBackgroundResource(R.drawable.custom_background);
TextView text = (TextView) view.findViewById(android.R.id.message);
/*Here you can do anything with above textview like text.setTextColor(Color.parseColor("#000000"));*/
text.setTextColor(Color.parseColor("#000000"));
toast.show();
Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • 4
    Thank you, this should have been the accepted answer. – ClassA Oct 04 '17 at 05:41
  • this is the best answer – Raj Bedi Jun 01 '18 at 10:43
  • 1
    Custom toast views are deprecated. Toasts constructed with Toast(Context) that haven't called setView(View) with a non-null view will return null – Aydinozkan Feb 21 '21 at 14:40
  • @Aydinozkan But in my answer, we have called setView(view) with non-null view, right ? – Android Killer Mar 07 '21 at 18:21
  • @AndroidKiller you are right, my mistake. – Aydinozkan Mar 08 '21 at 10:26
  • 2
    Google deprecated it from Android 11, now we can only use raw Toast! – Akshay Ashok May 01 '21 at 09:00
  • Great solution that does not need a layout file. I having a little trouble changing the text color – Cleaton Pais Jul 08 '21 at 15:22
  • No xml solution for the settings, like with a Dialog Box? – Steven Sep 17 '21 at 12:53
  • try { Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG); View view = toast.getView(); view.setBackgroundResource(R.drawable.bk_toast_gradient_round); TextView textView = view.findViewById(android.R.id.message); textView.setTextColor(Color.WHITE); toast.show(); }catch (Exception e){ e.printStackTrace(); } java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundResource(int)' on a null object reference – Whitebird May 16 '22 at 09:08
87

Change Toast Colours without any additional Layouts, 2018

This is a very easy way I've found of changing the colour of the actual image background of the Toast as well as the text colour, it doesn't require any additional layouts or any XML changes:

Toast toast = Toast.makeText(context, message, duration);
View view = toast.getView();

//Gets the actual oval background of the Toast then sets the colour filter
view.getBackground().setColorFilter(YOUR_BACKGROUND_COLOUR, PorterDuff.Mode.SRC_IN);

//Gets the TextView from the Toast so it can be editted
TextView text = view.findViewById(android.R.id.message);
text.setTextColor(YOUR_TEXT_COLOUR);

toast.show();
Matthew Weilding
  • 1,240
  • 1
  • 9
  • 15
42

Heads Up, Updates to toasts in Android 11

Custom toasts from the background are blocked, Android 11 protects users by deprecating custom toast views. For security reasons and to maintain a good user experience, the system blocks toasts that contain custom views if those toasts are sent from the background by an app that targets Android 11.

addCallback() method added in Android R If you want to be notified when a toast (text or custom) appears or disappears.

The most important text in toast API changes that for apps that target Android 11 the getView() method returns null when you access it, So, ensure to protect your apps from FATAL EXCEPTION, you know what I mean :)


You can customize android native toast by using following code

/**
 * ShowToast
 */
public class ShowToast {
    public ShowToast(Context context, String info) {
        Toast toast = Toast.makeText(context, Html.fromHtml("<font color='#e3f2fd' ><b>" + info + "</b></font>"), Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.show();
    }
}
 

If you want to change the background you have to use custom layout in toast

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
13

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();
Yugesh
  • 4,030
  • 9
  • 57
  • 97
  • @Saranya Whats the error You got or the Toast Colors are not changing. – Yugesh Jul 02 '15 at 06:20
  • 1
    Custom toast views are deprecated. Toasts constructed with Toast(Context) that haven't called setView(View) with a non-null view will return null – Aydinozkan Feb 21 '21 at 14:41
  • What do you write if you want to use the color and text size resources in colors.xml and dimensions.xml respectively? – Steven Sep 17 '21 at 12:50
12

use this way

Toast toast = Toast.makeText(MainActivity.this, R.string.toastMessage, Toast.LENGTH_LONG);   
toast.getView().setBackgroundColor(Color.parseColor("#F6AE2D"));
toast.show();
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
7

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();         
    }
Akanksha Hegde
  • 1,738
  • 11
  • 14
5
Toast toast=   Toast.makeText(YOUR ACTIVITY NAME ,Toast.LENGTH_SHORT);
View view =toast.getView();
view.setBackgroundColor(Color.GREEN); //any color your want 
toast.show();
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
satej sarker
  • 310
  • 3
  • 6
5

Change default toast message color and background color in JAVA. You can change toast message color and background color this way..

        Toast toast=Toast.makeText(MainActivity.this,"Signin button is clicked.",Toast.LENGTH_SHORT);
        View view =toast.getView();
        view.setBackgroundColor(Color.GREEN);
        TextView toastMessage = (TextView) toast.getView().findViewById(android.R.id.message);
        toastMessage.setTextColor(Color.RED);
        toast.show();

Just change toast text color this way..

        Toast toast = Toast.makeText(getApplicationContext(), "Signup button is clicked.",Toast.LENGTH_SHORT);

        TextView toastMessage=(TextView) toast.getView().findViewById(android.R.id.message);
        toastMessage.setTextColor(Color.BLUE);
        toast.show();
  • After changing the background color and text color, toast shape changed to rectangle. Why is this happening ?? Why is the default ellipse shape of toast is not retained after changing colors ?? – K Pradeep Kumar Reddy Apr 09 '20 at 19:52
5

To keep round corners

val toast = Toast.makeText(context, "Text", Toast.LENGTH_SHORT)
toast.view.background.setTintList(ContextCompat.getColorStateList(context, android.R.color.darker_gray))
toast.show()
vmtrue
  • 1,724
  • 18
  • 36
4

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

Kotlin Version:

val toast:Toast = Toast.makeText(this, "Please enter your name !", Toast.LENGTH_SHORT)
val view = toast.view
view?.background?.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN)
toast.show()
Amit Baderia
  • 4,454
  • 3
  • 27
  • 19
1
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();
    }
G Dias
  • 89
  • 1
  • 3
0

Adding to @AndroidKiller's answer, you can also set the gravity and a custom TextView among other things like so:

Toast toast = Toast.makeText(context, context.getResources().getString(resID), Toast.LENGTH_LONG);
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );        
View toastView = li.inflate(R.layout.toast_hint_layout, null);
TextView text = (TextView) toastView.findViewById(R.id.hint_text_tv);
text.setText(resID);
toast.setView(toastView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toastView.setBackgroundResource(R.drawable.toast_9_patch);          
toast.show();

Note, your background drawable should be a nine-patch PNG

You can even put an ImageView, and multiple TextViews in with XML like this:

<LinearLayout android:id="@+id/layout_root"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
    <ImageView
        android:layout_width="32dp"
        android:layout_height="43dp"
        android:src="@drawable/lightbulb"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/hint_text_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ccc"
            android:textSize="14dp"
            />

        <TextView
            android:id="@+id/hint_text_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="(disable hints in preferences)"
            android:textColor="#555"
            android:textSize="11dp"
            />
    </LinearLayout>
</LinearLayout>
pjco
  • 3,826
  • 25
  • 25
0

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();
    }
Xiokraze
  • 353
  • 4
  • 14
0

Short java code that keeps the round shape:

Toast toast = Toast.makeText(context, "message", Toast.LENGTH_SHORT);
toast.getView().setBackgroundTintList(ColorStateList.valueOf(Color.RED));
toast.show();
einUsername
  • 1,569
  • 2
  • 15
  • 26
0

Well here you can update the existing view of the toast class because setting new background would remove rounded border and all you want is changing background color and text color without changing toast background decoration

        Toast toast = Toast.makeText(getActivity(), "Copied", Toast.LENGTH_LONG);
        TextView textView = toast.getView().findViewById(android.R.id.message);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(25);
        toast.getView().getBackground().clearColorFilter();
        toast.getView().getBackground().setColorFilter(Color.BLACK, PorterDuff.Mode.DARKEN);
        toast.show();
adham khaled
  • 37
  • 2
  • 6
  • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations. – cursorrux Sep 19 '21 at 10:05