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

17,897

Solution 1

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

Solution 2

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.

Share:
17,897

Related videos on Youtube

Aoyama Nanami
Author by

Aoyama Nanami

Junior Mobile Programming for android

Updated on August 28, 2022

Comments

  • Aoyama Nanami
    Aoyama Nanami over 1 year

    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();
    }
    
  • Aoyama Nanami
    Aoyama Nanami almost 11 years
    can you tell how the way to custom it?
  • Aoyama Nanami
    Aoyama Nanami almost 11 years
    I want to customize my toast without creating a custom layout by modifying the default Toast. can i?
  • Raghunandan
    Raghunandan almost 11 years
    @AoyamaNanami you can check the post above. also your code works fine i can see the toast with red background and white text. SO what is the exact problem. i can't reproduce your problem. I ran your code by copy pasting the same. it works fine. Use custom layout will give you more options of customizing.