Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is there different id syntax in the Android docs?

Tags:

android

This page in the Android documentation defines an element id as follows:

<TextView android:id="@+id/label" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:text="Type here:" />

However this page defines it as:

<EditText id="text"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:textColor="@color/opaque_red"
    android:text="Hello, World!" />

I thought I had a decent understanding of what was going on until I saw this second example. In the first case, you need the + character so that id 'label' is added to the R file, correct? In the second case, would the EditText's id not be added to the R file because it does not contain the + character?

Also, the second example does not include the android namespace on the id. Does having or not having the Android namespace affect whether that id will be added to the R file?

Thanks for any clarification.

like image 780
D.C. Avatar asked Jan 22 '23 07:01

D.C.


2 Answers

This format without the android: namespace

 id="text"

is from an earlier version of the Android SDK.

like image 101
Mark B Avatar answered Jan 24 '23 21:01

Mark B


You are correct in your initial assessment. It's worth noting that the second id tag

<EditText id="text"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:textColor="@color/opaque_red"
    android:text="Hello, World!" />

Is missing the android: namespace so it actually isn't an android xml tag. The first one is an example of how to add that view's id to the R file so you can access it in your code. To be honest, I'm not sure what the purpose of the id in the second example is*, but I know that android wouldn't know what to do with it. The first one is the correct syntax.

*This is just speculation, but I'm willing to bet it was a typo somebody didn't notice or didn't care to fix because they were trying to illustrate something else.

like image 27
Chris Thompson Avatar answered Jan 24 '23 20:01

Chris Thompson