Home » Android » java – How to customize background, background color and text color for Toast in android

java – How to customize background, background color and text color for Toast in android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to customize my toast without creating a custom layout by modifying the default Toast. I want red color for toast’s background, and white color for toast’s text color and I want to make my toast’s background bigger that default toast. when I run my application, there’s nothing change from my toast, it still show in default toast.

This is how I customize my toast:

if (seriesSelection == null) {
    Toast toast = Toast.makeText(getApplicationContext(), "tidak ada chart yang dipilih", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 50, 50);
    toast.getView().setPadding(10, 10, 10, 10);
    toast.getView().setBackgroundColor(Color.RED);
    TextView text = (TextView) toast.getView().findViewById(android.R.id.message);
    text.setTextColor(Color.WHITE);
    text.setTextSize(14);
} else {
    Toast toast=  Toast.makeText(
            getApplicationContext(),
            "Nilai " + listData.get(seriesSelection.getPointIndex()).getInuNilai()+
            "  tanggal " + listData.get(seriesSelection.getPointIndex()).getTanggal(), 
            Toast.LENGTH_SHORT); 
    toast.setGravity(Gravity.CENTER, 50, 50);
    toast.getView().setPadding(10, 10, 10, 10);
    toast.getView().setBackgroundColor(Color.RED);
    text.setTextColor(Color.WHITE);
    text.setTextSize(14);
    toast.show();
}
How to&Answers:

You can have a custom view inflate a custom view and use toast.setView(layout).

Example:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
                               (ViewGroup) findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("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();

And your xml

<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="8dp"
              android:background="#DAAA"
              >
    <ImageView android:src="@drawable/droid"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginRight="8dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              />
</LinearLayout>

More info @

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

Ran your if and else part of the code (separately) it shows toast with red background and white text color. I don’t see any problem. But if you need to customize you can use a custom layout and inflate the layout and set the view to the toast.

Edit:

Your textview

  TextView text = (TextView) toast.getView().findViewById(android.R.id.message);

is initialized in the if part and in else part textview is not initialized.

Initialize textview outside if and else code.

Check this library called crouton which you might find usefull

https://github.com/keyboardsurfer/Crouton

Answer:

Toast has a setView() method.

You can customize a Toast to show any view.

I’d say instead of trying to edit the view inside the Toast, you just create a View and pop it in yourself.

Answer:

I have very simple and easy code for customize a Toast accordingly, you can change background of toast and text color also.

 Toast toast = Toast.makeText(MainActivity.this, "Added successfully", Toast.LENGTH_LONG);
    View view = toast.getView();
    view.setPadding(20, 20, 20, 20);
    view.setBackgroundResource(R.color.GREEN);
    view.setTextColor(Color.RED);
    toast.show();