0

I am creating a custom toast but it is not working at all.

This is my Code:-

private void showCustomToast(String showToast) {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout,
                (ViewGroup) findViewById(R.id.toast_layout_root));
        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText(showToast);
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(inflater);
        toast.show();
    }

2 Answers2

2

You have to pass layout object in the setView() method not the inflater.

1

Problem in your setView section. Pass View Object .

Don't

toast.setView(inflater);

Do

 toast.setView(layout);

Finally

 private void showCustomToast(String showToast) {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout,
                (ViewGroup) findViewById(R.id.toast_layout_root));
        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText(showToast);
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout); // Not inflater
        toast.show();
    }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198