Android: "#+id/" and "#+id/" - android

According to this thread I need to use #+id/ for the first time to make resource be created.
But what if I forget that this resource was previously created and create it again with #+id/? I have some input and set nextFocusDown for the element that is still not declared.
<EditText
...
android:nextFocusDown="#+id/myinput2"/>
200 lines below I create this element with #+id because I forgot that it's already declared.
<EditText
...
android:id="#+id/myinput2"/>
It works like this but can it cause an issue?

Some quick definitions:
#+id/foo means "use the id foo, and create it if it doesn't exist"
#id/foo means "use the id foo" (which will be an error if the id foo doesn't exist)
Previously, there were reasons to prefer #id over #+id (the system could tell you if you tried to reference a view via an id that didn't exist), but now the system is smart enough that even writing android:layout_below="#+id/idthatdoesntexistanywhere" will be tagged as an error:
So just always use #+id.

It works like this but can it cause an issue?
No, it can't. Always use #+id/. By now, the Android build environment is smart enought to figure it out.

If you put two elements with the same id in the same layout file, it generates an IDE error (red underline) and you won't be able to compile.

Related

Why does the designer use "#+id" instead of "#id" for constraints?

I've always understood #+id to indicate the creation of a new ID (generally used with android:id), and #id to reference it elsewhere, as explained at length in a popular question here and in the official documentation:
The at-symbol (#) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace...
That this is not enforced is a bit odd; you can just use #+id everywhere, multiple times with the same id, and it does not seem to be an error or problem (unless you use the same id with multiple android:id declarations).
When using the graphical designer in Android Studio with constraint layout (it could in fact be with everything, I have not checked), whenever a constraint is added it will declare them like this:
app:layout_constraintTop_toBottomOf="#+id/rlistfrag"
Where rlistfrag is assigned to another element with android:id in the same file -- if it didn't exist already, the designed could not have created the constraint, so there can be no contextual ambiguity.1 According to the docs this amounts to twice declaring "a new resource name that must be created and added to our resources".
It seems the semantics here are not, by omission at least, exactly as described in the docs. Why does the designer do this and what are those omitted semantics?
Or could there? What all this implies to me is that elements may be processed in any order so the point is whenever an id is first encountered it will be created, even if it is not associated with an existing element.
There's a bit in the doc guide on layout resources that confirms the id will only be set the first time:
The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the R.java class, if it doesn't already exist.
Which need not mean the elements aren't processed in order.
It is applied in all layout components. whenever you want to mention "id" you have to use "#+id"

Why is it necessary to use #+id instead of #id?

I know the difference between #+id and #id (see this accepted answer). However, I always get the feeling that I'm doing the job of the AAPT compiler when I write the '+' in #+id.
Is there a reason the resource compiler cannot infer by itself if an identifier must be created or just reused? An underlying hashtable structure would do the job: every resource with the same id go into the same bucket, and if the key does not exist, just create it.
Probably the compiler would not be able to differentiate between a 'right' and a 'wrong' id. If it finds a new id (i.e. one that is not in the underlying hashtable), it would always assume it to be a right, new id. It would not be able to differentiate between an actual new id and a mistyped id.
I could imagine it is to help the programmer.
If you did not need the #+id construction then all #id references/constructions would be valid, then it would be difficult to track down an error, as the compiler would not fail on incorrect references (as it would simply construct the typo id).
Put differently, all id reference errors would have to be discovered at runtime.
Edit:
Just noticed the similar answer by Piovezan, regarding your comment:
Maybe, but the result is that many devs use #+id everywhere, since there is no error if the id is already defined, and everything works just fine. That means the compiler tests if the id already exist, but not if it does not exist, that's crazy
Then those developers are misusing the #+id construction imo.
It is still much better to have the option to distinguish between #+id and #id, since (for those who does not misuse the #+id) the compiler has a chance of giving a compile time error on wrong references.
Edit2
And to address the comment:
That's the link I gave in the first sentence. It explains the difference but does not answer why the '+' cannot be automatically infered by AAPT
I believe it can, it just does not due to the argumentation above (i believe).
with #+id you are adding id to R.java which allows you to reference it from Java classes while with #id you are not. You can use #id only if specific id is already created and you are referencing it in another view e.g. android:layout_below="#id/some_id"

Resource scope for #+id/ declarations?

When working through the tutorial to build my first Android application I reached a section where it states the #+id/ prefix not only references a resource that is defined in the gen/R.java file, but that the + sign also indicates its first encounter with it so it will create it. Consider this code snippet:
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
After reading through the side-bar in the first link, related to resources, and the article it linked to named Providing Resources (at a somewhat cursory level), I'm unable to get a very clear statement from the documentation on the scope of the resource with the #+id/ prefix. I understand that you can have a resource with the same name scoped inside each prefix:
Note: This string resource has the same name as the element ID: edit_message. However, references to resources are always scoped by the resource type (such as id or string), so using the same name does not cause collisions.
but what I'm driving at is this. Based on the documentation it appears that I can't have two controls resourced as edit_message in two different activities because there would be a conflict.
My concern here is that I'd presumably have to prefix my id attributes with the Activity name to keep them unique so I can access these controls from code.
Am I correct in my statement and assumptions here?
You can place that exact block of XML in another layout file and it will work just fine. #+id generates a new id if it is not defined yet. When your app gets packaged, the packaging tool will create the id once and all the others will correctly refer to that id.
As long as you don't end up with two UI components in the same layout with the same id, everything's ok.

what does the "#+android:id/title" mean?

In normal, we should use #+id/ to define an id and use #id to reference an id. Today I found #+android:id/title in apps/settings/res/layout/preferenc_progress.xml.
How to understand it and how to use it?
It is used for resources that are shipped with the SDK.
You can take a look at them by browsing to
[PATH TO ANDROID SDK]/platforms/android-[VERSION]/data/res
By using the android in android.R.whatever you just specify the R file to look up. For more information you should read Accessing Platform Resources.
That belongs to the app preferences activity screen definition.
title and summary are standard Android fields of a TextView preference item.
I think it does the same thing. It's just a more formal way of saying it by specifying where the namespace is.
I've never met this way of giving id, but in theory this means adding new id title to android package. So you'll be able to use it in your code like android.R.id.title. But I'm not sure resource compiler will really create any id in android package. I think it can be used only with predefined ids. But I'll give you more precise answer later, when I'll be able to check it.
EDIT: I've checked it and found some differences. Firstly, if you define Android's id using #+android:id/some_id, which is already present in SDK, this id will not be defined in your R.java file. If it's not present in SDK, it will be defined in R.java, but with different kind of value. Secondly, if you'll try to convert id from its string representation to int value, Resources.getIdentifier() method will return 0 in case of #+android:id format.
This means it will create an id in your resource file.

Difference between "#id/" and "#+id/" in Android

What is the diffirence between the #id/ and #+id/?
In #+id/ the plus symbol + instructs to create a new resource name and add in to the R.java file but what about #id/? From the documentation of ID: when referencing an Android resource ID, you do not need the plus symbol, but must add the android package namespace, like so:
android:id="#android:id/list"
But in the image below Eclipse doesn't suggest any kind of #android:id/.
Are #id/ and #android:id/ the same?
you refer to Android resources , which are already defined in Android system, with #android:id/.. while to access resources that you have defined/created in your project, you use #id/..
More Info
As per your clarifications in the chat, you said you have a problem like this :
If we use android:id="#id/layout_item_id" it doesn't work. Instead #+id/ works so what's the difference here? And that was my original question.
Well, it depends on the context, when you're using the XML attribute of android:id, then you're specifying a new id, and are instructing the parser (or call it the builder) to create a new entry in R.java, thus you have to include a + sign.
While in the other case, like android:layout_below="#id/myTextView" , you're referring to an id that has already been created, so parser links this to the already created id in R.java.
More Info Again
As you said in your chat, note that android:layout_below="#id/myTextView" won't recognize an element with id myTextViewif it is written after the element you're using it in.
the + sign is a short cut to add the id to your list of resource ids. Otherwise you need to have them in a xml file like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="my_logo" type="id"/>
</resources>
In Short
android:id="#+id/my_button"
+id Plus sign tells android to add or create a new id in Resources.
while
android:layout_below="#id/my_button"
it just help to refer the already generated id..
Its very simple:
"#+..." - create new
"#..." - link on existing
Source: https://developer.android.com/guide/topics/resources/layout-resource.html#idvalue
The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's R.java file that refers to the EditText element. With the resource ID declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts. See the sidebox for more information about resource objects.
From: https://developer.android.com/training/basics/firstapp/building-ui.html
From the Developer Guide:
android:id="#+id/my_button"
The at-symbol (#) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:
android:id="#android:id/empty"
There's a bug with Eclipse where sometimes if you just created a new #+id/.., it won't be added immediately to the R.java file, even after clean-building the project. The solution is to restart Eclipse.
This I think should be solved as soon as possible, because it may (and from experience, will) confuse some developers into thinking that there's something wrong with their syntax, and try to debug it even if there's really nothing to debug.
Android uses some files called resources where values are stored for the XML files.
Now when you use #id/ for an XML object, It is trying to refer to an id which is already registered in the values files. On the other hand, when you use #+id/ it registers a new id in the values files as implied by the '+' symbol.
Hope this helps :).
#id/ and #android:id/ is not the same.
#id/ referencing ID in your application, #android:id/ referencing an item in Android platform.
Eclipse is wrong.
Difference between #+id and #id is:
#+id is used to create an id for a view in R.java file.
#id is used to refer the id created for the view in R.java file.
We use #+id with android:id="", but what if the id is not created and we are referring it before getting created(Forward Referencing).
In that case, we have use #+id to create id and while defining the view we have to refer it.
Please refer the below code:
<RelativeLayout>
<TextView
android:id="#+id/dates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/spinner" />
<Spinner
android:id="#id/spinner"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="#id/dates"
android:layout_alignParentRight="true" />
</RelativeLayout>
In the above code,id for Spinner #+id/spinner is created in other view and while defining the spinner we are referring the id created above.
So, we have to create the id if we are using the view before the view has been created.
If the view item performs the same operation, you can use the #+id for each entry in any layout because during the compilation of multiple #+id/foo the R.java file only creates one enumeration. So for example, if I have a save button on each page that performs the same operation, I use android:id="#+id/button_save" in each layout. The R.java file only has one entry for the button_save.
Difference between “#+id/” and “#id/” in Android
The first one is used for to create the ID of the particular ui component and the another one is used for to refer the particular component

Categories

Resources