0

I'm creating custom toast class called MyToast:

public class MyToast extends Toast {

    public static final int RED = 0;
    public static final int BLUE = 1;

    public MyToast(Context context, String data, int color) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.my_toast, null);
        setView(layout);

        TextView tvToast = (TextView) layout.findViewById(R.id.tvToast);
        tvToast.setText(data);

        if(color == RED) {
            tvToast.setBackgroundResource(R.drawable.red_background);
            tvToast.setTextColor(context.getResources().getColor(R.color.red));
        } else {
            tvToast.setBackgroundResource(R.drawable.blue_background);
            tvToast.setTextColor(Color.WHITE);
        }
    }
}

my_toast.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/tvToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

Using MyToast in MainActivity:

new MyToast(context, getResources().getString(
                            R.string.uspesna_najava), MyToast.BLUE).show();

I'm getting this warning:

Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)

on this part of code:

View layout = inflater.inflate(R.layout.my_toast, null);

This code is working good, but I want to get rid of that warning. I know that I need to inflate like this, to avoid this warning:

View layout = inflater.inflate(R.layout.my_toast, parent, false);

But where can I find parent?

In official site Android Developers, they inflating view like this:

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

I tried like this, but I'm getting error:

The method findViewById(int) is undefined for the type MyToast

KiKo
  • 1,668
  • 7
  • 27
  • 56
  • 1
    [How to Customize Toast In Android](http://stackoverflow.com/questions/16909476/how-to-customize-toast-in-android) – Top Cat Dec 09 '14 at 16:43

1 Answers1

0

Of course, the method findViewById is a method of Activity.

So the sample code is in the Activity class that just calling findViewById will cause no problem.

Hence your code is in MyToast class, you should call the method for an instance of Activity:

((Activity) context).findViewById(R.id.toast_layout_root)
hata
  • 11,633
  • 6
  • 46
  • 69
  • 1
    I am having a similar issue. But the context I pass is from a Service rather than an Activity. Is there another way to get the `findViewById` method? – Aakaash Jois Sep 25 '16 at 07:11