Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android custom toasts

Tags:

android

toast

I was able to make custom toast using this code

    LayoutInflater inflater = getLayoutInflater();

    View layout = inflater.inflate(R.layout.custom_toast_layout, (ViewGroup)findViewById(R.id.custom_toast));

    TextView text = (TextView) layout.findViewById(R.id.toast_tv);
    text.setText("Hello! This is a custom toast!");

    Toast toast = new Toast(getApplicationContext());       
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();

However, since I do not understand the purpose of LayoutInflater, I modified the code to this...

Toast toast = new Toast(getApplicationContext());
    toast.setView(findViewById(R.id.custom_toast));
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.show();

And I get RuntimeException saying "setView must have been called"..

  • Why can't I just assign the view to toast without using LayoutInflater?

  • What is the purpose of LayoutInflater in general so that I can apply this experience to other custom views?

Edit: I am using these codes in onListItemClick() interface method.. after the content is set..

like image 834
BLOB Avatar asked Dec 30 '12 08:12

BLOB


1 Answers

Your question have your answer, every custom view should inflate first, that's the reason you got an error with your modified code.

like image 143
Soumyadip Das Avatar answered Sep 29 '22 09:09

Soumyadip Das