10
  • I always had ambiguity on why we need to use inflater in android, Why are they used in ListView for custom layouts (like below)?
  • What is an Inflater ?
  • What is the advantage of using Inflater ?

public class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;


public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_mobile, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
    textView.setText(values[position]);

Thanks,

Joel
  • 4,732
  • 9
  • 39
  • 54

3 Answers3

24

What is an Inflater ?

To summarize what the LayoutInflater Documentation says... A LayoutInflater is one of the Android System Services that is responsible for taking your XML files that define a layout, and converting them into View objects. The OS then uses these view objects to draw the screen.

I always had ambiguity on why we need to use inflater in android, Why are they used in android ListView for a custom layout ?

Typically, you don't ever need to directly use a LayoutInflater. Android does most of the layout inflation for you when you call setContentView() in the onCreate() method of your activity. So you, as the programmer, are responsible for making sure the views are inflated. Now you want to inflate views in the context of a ListView. The Adapter class can do the inflation for you if you do not want to customize each item. But if you want to customize the views shown in a list, you will have to manually inflate each view with the LayoutInflater, since there is no other existing method you can use.

What is the advantage of using Inflater ?

There is no advantage to using it. You are required to use a LayoutInflater in some shape or form to inflate your static XML layouts.

Alternatively, you could create views dynamically with java code. However, you would need to call methods to set each property for the view by hand. In my opinion, it is easier to use the XML/inflation process. In addition, Android pre-processes your XML files at build time, so this results in a faster execution time.

Joel
  • 4,732
  • 9
  • 39
  • 54
  • "There is no advantage. You are required to inflate your views by some means" that statement is obviously wrong, you could always create views in code – Emanuel Moecklin Aug 02 '13 at 17:43
  • The question "What is the advantage of using Inflater ?" translates for me to "What is the advantage of using Inflater vs. creating the views in code?". Your answer "There is no advantage to using it [...]" would be wrong if it's answering the "expanded" question. – Emanuel Moecklin Aug 02 '13 at 17:52
  • That is your interpretation. It seems to me the OP doesn't know what an inflater is.. So he/she could be wondering if there are alternatives for xml layout rendering. After all, that is all his code example is doing. – Joel Aug 02 '13 at 17:57
  • Joel .... Your answers clarified my questions .... is there any alternatives for xml layout rendering apart from from using inflator ? –  Aug 02 '13 at 18:02
  • 1
    @sky You can also use java to create view objects for android, but I would not recommend it. See my edit. – Joel Aug 02 '13 at 18:06
3

I always had ambiguity on why wee need to use inflater in android, Why are they used in android ListView for a custom layout ?
They are used to create the Views for each row.

What is an Inflater ?
A system service that creates a View out of an XML layout.

In the below code i am trying to understand why Inflator is used ?
The inflater is used to create the Views for the rows.

What is the advantage of using Inflater ?
Compared to what? How do you want to create the Views out of the XML layout?

SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • So Creating views for the rows for a Listview is only possible using Inflator ? –  Aug 02 '13 at 17:41
  • 1
    Either with an inflater from an XML layout, or programmatically with `new View()`. In most cases you probably want to use an XML layout. – SimonSays Aug 02 '13 at 18:54
0

Put simply, an inflater allows you to create a View from a resource layout file so that you do not need to create everything programmatically.

In your example, you inflate the layout R.layout.list_mobile. This lets you access all of the views within it. For example, you then call:

TextView textView = (TextView) rowView.findViewById(R.id.label);

By calling rowView.findViewById() you are able to access views that were created within that layout. Often for ListViews you will have a row XML file that you then inflate and put your data into the views.

telkins
  • 10,440
  • 8
  • 52
  • 79