0

I'm trying to develop an android app and looking at various tutorials, it seems people are using setContentView as well as LayoutInflater at many places. What is the difference between these two and when should i use LayoutInflater instead of setContentView?

user1159517
  • 5,390
  • 8
  • 30
  • 47

2 Answers2

3

I think setContentView will set the specified layout as the layout of the Activity your are running, while LayoutInflater can be used in situations where you need to use a layout but not necessarily use that layout as the layout for the Activity itself.

For example you may want to inflate a custom layout for the listview's items, which has nothing to do with the layout of the Activity. In this case you will use a LayoutInflater.

Juan
  • 5,525
  • 2
  • 15
  • 26
0

setContentView is for Activities, and LayoutInflater is used in Fragments.

jyoonPro
  • 1,661
  • 1
  • 16
  • 41
  • Sorry but what is the difference between Fragments and activities? – user1159517 May 08 '14 at 23:01
  • Activities are considered standalone, and have their own layout; that's why you set a content view. On the other hand, fragments cannot stand alone and they need an activity to view them. So they have their layouts, but you cannot set to view the content. But fragments are made to be 'reusable'. Read [this thread](http://stackoverflow.com/questions/10478233/android-need-some-clarifications-of-fragments-vs-activities-and-views) for more information. – jyoonPro May 08 '14 at 23:07
  • false, check above answer – Suhaib Mar 26 '17 at 06:02