As a new-bee I am again in trouble understanding some basics of inflate().
here is my xml file -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/linearlayout"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/textview" />
</LinearLayout>
below is little basic code -
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ly = (LinearLayout)findViewById(R.id.linearlayout);
Log.i("System.out ","linear layout = " + ly);
View view=getLayoutInflater().inflate(R.layout.activity_main,null);
LinearLayout ly1 = (LinearLayout)findViewById(R.id.linearlayout);
Log.i("System.out ","linear layout = " + view);
Log.i("System.out ","linear layout = " + ly1);
}
and the output:
05-10 14:30:33.446: I/System.out(26806): linear layout = android.widget.LinearLayout@41e28848
05-10 14:30:33.446: I/System.out(26806): linear layout = android.widget.LinearLayout@41e29740
05-10 14:30:33.446: I/System.out(26806): linear layout = android.widget.LinearLayout@41e28848
What I understand from the 1st and 3rd line of output that once we call setContentView()
it does inflating and hence the view object will be in memory after call to this method. Therefore on calling findViewById()
, it return same object of linearlayout View both time in code block. (ly isequalto ly1)
But, why the address of LinearLayout object in 2nd line of output different,
linear layout = android.widget.LinearLayout@41e29740
?
code responsible for this is -
View view=getLayoutInflater().inflate(R.layout.activity_main,null);
I thought this will return root View, which in this case is LinearLayout.
If R.layout.activity_main
is already inflated and there is no change in Layout(neither addition or removal of any View/ViewGroup), then why address of object(view & ly1) does not match?
I tried this -
View view=getLayoutInflater().inflate(R.layout.activity_main,null);
setContentView(view);
LinearLayout ly1 = (LinearLayout)findViewById(R.id.linearlayout);
Log.i("System.out ","linear layout = " + view);
Log.i("System.out ","linear layout = " + ly1);
and got this -
I/System.out(2603): linear layout = android.widget.LinearLayout@41e09e10
I/System.out(2603): linear layout = android.widget.LinearLayout@41e09e10
why is ly1 and view object represent same address in this case ?