Why do Android docs have a different identifier syntax?

This Android documentation page defines the identifier of an element 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 happening until I saw this second example. In the first case, you need the + character so that the identifier 'label' is added to the R file, right? In the second case, the EditText identifier will not be added to the R file because it does not contain the +?

In addition, the second example does not include the android namespace in id. Does or does not have an Android namespace, does this identifier affect the R file?

Thanks for any clarification.

+4
source share
4 answers

You are right in your initial assessment. It is 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!" /> 

There is no android: namespace, so this is actually not an android xml tag. The first is an example of how to add this view identifier to the R file so that you can access it in your code. Honestly, I'm not sure that the purpose of the identifier in the second example is *, but I know that the android would not know what to do with it. The first is the correct syntax.

* This is just an assumption, but I bet it was a typo that someone did not notice or did not want to correct, because they were trying to illustrate something else.

+3
source

This format is without android: namespace

  id="text" 

is an earlier version of the Android SDK.

+4
source

A plus sign (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are many other ID resources that are offered by the Android platform. When you reference the Android resource identifier, you do not need a plus sign, but it should add the android package namespace, for example:

android: id = "@ android: id / empty"

Adapted from Ad Layout | Android Developers in the ID section.

However, in your second example there is no @android:id/ provided before id text , therefore, to be rude honest, I have never seen this notation before and am wondering if this could be a typo on the side of the author.

+1
source

The second example is incorrect. The attribute is always android: id, and the value must be either @ + id / myId (to create a new identifier called "myId") or @ id / myId (to use an already defined identifier called "myId".) Using @android : id / theId allows you to use identifiers defined by the Android platform.

+1
source

Source: https://habr.com/ru/post/1306296/


All Articles