Layout resource inflate in Android

One thing that often confuses developers new to the Android platform is the handling of layout resources. The xml files describing the layouts are magically transfered into a more efficient binary format behind the scenes and hidden away - leaving the developer with a static reference to the resources via the R.java file.
By using the setContentView(int layoutResId) method of the Activity class your layout will be displayed on the screen. Behind the scenes the Android platform is creating all the view objects contained in your layout xml file provided to the setContentView(int layoutResId) method. This process of creating view objects out of layout resources is referred to as layout inflation.
So far so good. This means that before the setContentView(int layoutResId) method is called the findViewById(int resId) method will return null for any view references in the layout, causing some potential NullpointerExceptions.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
EditText testField = (EditText) findViewById(R.id.test); //To early!! Will return null
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.buttontest);
 
testField.setText("Some Text"); //NullPointerException
button.setText("Some other text"); //No problem
}
To avoid these errors it's a good habit to place the setContentView(int layoutResId) method call at the very top of the onCreate() method.
In some cases you will have to do the layout inflation by yourself, i.e. when you want to set a custom view to a Dialog or a Toast. To inflate a view you use the LayoutInflater class. There's a number of different ways to get a handle to a LayoutInflater:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(context)
To actually create the view object you use the inflate() method of the LayoutInflater. In the most straightforward form this method has the signature:
public View inflate(int resource, ViewGroup root)
Note that the resource is referring to a layout xml file resource, NOT the id of the view object that you want to inflate. First inflate the layout resource and then get a handle to the view object by using the findViewById(int resId) method.
The second attribute allows you to specify a ViewGroup object to be the root view of the inflated object(s) specified by your layout xml file. This means that the inflated view object(s) are attached as child views on the specified root ViewGroup object. Setting this value to null will simply return the inflated view object(s) without attaching them to a root view.
EditText fooField = (EditText) findViewById(R.id.foo); //To early!! Will return null
LinearLayout myRootlayout = new LinearLayout();
View itemView = inflater.inflate(R.layout.layout_details, myRootlayout); //from layout_details.xml
EditText barField = (EditText) findViewById(R.id.bar); //Now we have an object
In this example the inflate method returns the mRootlayout object with the view objects contained in the layout_details.xml file attached as child views. So itemView and myRoot is actually referring to the same physical object.

Comments

Popular posts from this blog

How to draw an overlay on a SurfaceView used by Camera on Android?

Android TCP Connection Chat application

Create EditText with dropdown in android