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