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
So far so good. This means that before the
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
The second attribute allows you to specify a
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
.To avoid these errors it's a good habit to place the@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EditText testField = (EditText) findViewById(R.id.test); //To early!! Will return nullsetContentView(R.layout.main);testField.setText("Some Text"); //NullPointerExceptionbutton.setText("Some other text"); //No problem}
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
:To actually create the view object you use the inflate() method of the LayoutInflater. In the most straightforward form this method has the signature:LayoutInflater inflater = (LayoutInflater)context.getSystemService(LayoutInflater inflater = LayoutInflater.from(context)
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.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.EditText fooField = (EditText) findViewById(R.id.foo); //To early!! Will return nullLinearLayout myRootlayout = new LinearLayout();EditText barField = (EditText) findViewById(R.id.bar); //Now we have an object
Comments
Post a Comment