Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between setContentView and LayoutInflater?

I am creating a tabs list with several fragments. I have noticed that, in the main activity, I used setContentView to get the layout xml and use findViewById to get the corresponding UI element config.

setContentView(R.layout.fragment_tabs); mTabHost = (TabHost)findViewById(android.R.id.tabhost); mTabHost.setup(); mTabManager = new TabManager(this, mTabHost, android.R.id.tabcontent); 

However, in the different fragment class, I have to use the inflater instead.

View v = inflater.inflate(R.layout.webview, container, false); WebView myBrowser=(WebView)v.findViewById(R.id.mybrowser); 

And both function are used to get the layout xml to create an object, why is there a difference? Is the first one use during onCreate, and the second one during onCreateView? In what situation I should choose either of them?

like image 598
user782104 Avatar asked Jul 23 '13 10:07

user782104


People also ask

What is SetContentView () used for?

SetContentView is used to fill the window with the UI provided from layout file incase of setContentView(R. layout. somae_file). Here layoutfile is inflated to view and added to the Activity context(Window).

What is a LayoutInflater?

LayoutInflater is a class used to instantiate layout XML file into its corresponding view objects which can be used in Java programs. In simple terms, there are two ways to create UI in android. One is a static way and another is dynamic or programmatically.

How do you make a LayoutInflater?

Create a copy of the existing LayoutInflater object, with the copy pointing to a different Context than the original. This is used by ContextThemeWrapper to create a new LayoutInflater to go along with the new Context theme. Context : The new Context to associate with the new LayoutInflater.

When should I call SetContentView?

No it is not necessary to call setContentView() method. It is call to show the UI but in your case you just want to finish the activity without showing the UI, So it is fine.


1 Answers

setContentView is an Activity method only. Each Activity is provided with a FrameLayout with id "@+id/content" (i.e. the content view). Whatever view you specify in setContentView will be the view for that Activity. Note that you can also pass an instance of a view to this method, e.g. setContentView(new WebView(this)); The version of the method that you are using will inflate the view for you behind the scenes.

Fragments, on the other hand, have a lifecycle method called onCreateView which returns a view (if it has one). The most common way to do this is to inflate a view in XML and return it in this method. In this case you need to inflate it yourself though. Fragments don't have a setContentView method

Activities and views both have a method called findViewById(). The activity version will search for a view with the given id inside of it's content view (therefore, internally, it will call contentView.findViewById()). This means that the contentView needs to be set before it becomes usable. Like setContentView, fragments don't have a method for findViewById (which makes sense, because there is no content view). Simply use getView().findViewById() instead for the same behaviour.

LayoutInflater.inflate just inflates and returns a view (you can use this anywhere). You still need to set that view as the content view within an Activity

like image 141
Bradley Campbell Avatar answered Sep 22 '22 23:09

Bradley Campbell