Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
}
like image 961
Aoyama Nanami Avatar asked Jul 23 '13 03:07

Aoyama Nanami


1 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

like image 84
Raghunandan Avatar answered Oct 06 '22 18:10

Raghunandan