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

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


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.