0

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 :)

Rushi M Thakker
  • 679
  • 2
  • 15
  • 34

1 Answers1

1

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

Community
  • 1
  • 1
Exigente05
  • 2,161
  • 3
  • 22
  • 42
  • Yes you are right third way gives compilation error. I have gone through that link but what I don't understand is how first way works. Because when I do something in java file like R.id. autocomplete suggestion gives me more than one >btnFacebook suggestion which means there is more than one reference created as btnFacebook. How will it compile and resolve at runtime. Won't it occupy more unnecessary memory? Thanks In Advance :) – Rushi M Thakker Aug 12 '16 at 05:41
  • I have checked your first way xml in android studio 2.1 but can't see multiple btnFacebook. – Exigente05 Aug 12 '16 at 06:05
  • Even if you use same id in different layout it might not show multiple option but ID should be inside current view. Ref:- http://stackoverflow.com/questions/17825356/how-does-multiple-component-with-same-id-work-in-android – Exigente05 Aug 12 '16 at 06:17
  • http://imgur.com/uNNTzS3 this is what I am talking about. One id points to one at top in layout_alignLeft and other in android:id in button. here I was only testing with 2 @+id for producing image to give screenshot to you – Rushi M Thakker Aug 12 '16 at 06:32
  • You can check your R.java file where you can see only one int value available for btnFacebook. So, it seems that only one id has been generated and not unnecessary memory occupation. – Exigente05 Aug 12 '16 at 08:10
  • You are right but that is where my question lies, if only 1 is generated why 2 declarations are considered and second how exactly will the parser omit/ignore second declaration. If no one will answer soon I will upvote your answer (y) – Rushi M Thakker Aug 12 '16 at 08:29
  • I guess id is identified by "name". If one name exists then search for id with that name. For more clarification- we write @+id/name and it supposes add if not exists. Then it checks inflated view have that id or not which I am calling. – Exigente05 Aug 12 '16 at 09:39