I’m a newbie in Android development.
I have Eclipse with ADT (sdk version: 17 , Android 4.2).
I don’t understand what is the difference between:
- DEFINING a View (via visual editor provided by ADT or directly in the XML layout file corresponding to the current activity)
and
- INSTANTIATING a View (PASSING THE CONTEXT AS PARAMETER) such as: TextView tv = new TextView(getContext()); (taken from : What is 'Context' on Android? , first asnwer)
and
- INSTANTIATING a View (WITHOUT PASSING THE CONTEXT AS PARAMETER) such as: TextView tv = new TextView();
Thank in advance for any advice.
Kind regards
To use your terminology:
When DEFINING a
Viewin XML (or the ADT editor – which just creates the XML for you), it still needs to be inflated by a layout inflater. A layout inflater will INSTANTIATE theView(s) for you. This can be done behind the scenes – such as when you callActivity.setContentView(), or directly usingView.inflate(). The inflater effectively just runs through the XML and instantiates all of the Views it contains.When INSTANTIATING a
View, you’re giving it theContextso it has a reference to resources – so it can load images, strings, dimensions etc – plus other Android related functionality (which you can probably ignore for now).You can’t INSTANTIATE a
Viewwithout the context.