I am a beginner in android development and am having a hard time understanding the use of Inflater.I have already read this question:
What does it mean to inflate a view from an xml file?
Now consider this example:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.toast_layout_root));
Toast toast = new Toast(getApplicationContext());
toast.setView(layout);
toast.show();
From what I understood we used inflater to inflate(convert) the xml layout into a View object and then we set the view using setView(layout). But if we just had to set view of toast then why not simply use findviewbyid as follows:
Toast toast=new Toast(this);
toast.setView(findViewById(R.id.toast_layout_root));
toast.setDuration(toast.LENGTH_LONG);
toast.show();
The above code compiles but it causes the application to crash at start up.I know it is wrong to do it this way but why?
What will be the difference between the view obtained by using inflater and the view obtained using findViewById.