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..
Your question have your answer, every custom view should inflate first, that's the reason you got an error with your modified code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With