androidandroid-layoutxml-parsingandroid-viewgroupandroid-identifiers

Effect of muliple @+id vs single @+id in compilation of same layout file


First Way:

<Button
    android:id="@+id/btnLogin"
    style="@style/btnStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnFacebook"
    android:layout_alignRight="@+id/btnFacebook"
    android:layout_below="@id/etPassword"
    android:layout_centerInParent="true"
    android:onClick="goHome"
    android:text="Login"/>

<Button
    android:id="@+id/btnFacebook"
    style="@style/btnFacebook"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textOR"
    android:layout_marginTop="10dp"
    android:onClick="facebookSignUp"
    android:text="     Signup with Facebook     "/>


<Button
    android:id="@+id/btnGooglePlus"
    style="@style/btnGoogle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnFacebook"
    android:layout_alignRight="@+id/btnFacebook"
    android:layout_below="@id/btnFacebook"/>

Second Way:

<Button
    android:id="@+id/btnLogin"
    style="@style/btnStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnFacebook"
    android:layout_alignRight="@id/btnFacebook"
    android:layout_below="@id/etPassword"
    android:layout_centerInParent="true"
    android:onClick="goHome"
    android:text="Login"/>

<Button
    android:id="@id/btnFacebook"
    style="@style/btnFacebook"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textOR"
    android:layout_marginTop="10dp"
    android:onClick="facebookSignUp"
    android:text="     Signup with Facebook     "/>


<Button
    android:id="@+id/btnGooglePlus"
    style="@style/btnGoogle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/btnFacebook"
    android:layout_alignRight="@id/btnFacebook"
    android:layout_below="@id/btnFacebook"/>

Third Way:

<Button
    android:id="@+id/btnLogin"
    style="@style/btnStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/btnFacebook"
    android:layout_alignRight="@id/btnFacebook"
    android:layout_below="@id/etPassword"
    android:layout_centerInParent="true"
    android:onClick="goHome"
    android:text="Login"/>

<Button
    android:id="@+id/btnFacebook"
    style="@style/btnFacebook"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textOR"
    android:layout_marginTop="10dp"
    android:onClick="facebookSignUp"
    android:text="     Signup with Facebook     "/>


<Button
    android:id="@+id/btnGooglePlus"
    style="@style/btnGoogle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/btnFacebook"
    android:layout_alignRight="@id/btnFacebook"
    android:layout_below="@id/btnFacebook"/>

Two questions:

  1. Output of all three ways of initialising

btnFacebook

is same. How?

  1. Since output is same, means XML Parser compilation result is same. So how exactly does initialising of resources is done and how are they attached to ViewTree?

Any official documentation with explanation is appreciated. Thanks In Advance :)


Solution

  • from here

    order would not make any difference.

    As though view will be same but third way won't work because btnFacebook is not being declared before btnLogin. So compiling error will be shown. Third way will work after slight change, see going that link. For more info