tags:

views:

125

answers:

1

Hi all,

Suppose I have a simple layout xml like the following:

button.xml:

<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Are there any differences in the following calls? and which one should i use?

button = (Button) getLayoutInflater().inflate(R.layout.button, null);

and

View v = getLayoutInflater().inflate(R.layout.button, null);
button = (Button) v.findViewById(R.id.button01);
A: 

The first option is cleaner and slightly more efficient.

Your layout inflater will return a Button. With the first option, you gain access to the Button directly. With the second option, you cast the button down to a View and then look for the view with a given ID, which is an extra useless comparison, since the view with the ID you're looking for in the hierarchy happens to be the button itself. So in the second option, v == button.

Roman Nurik