ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

What is the difference between @+id/ and @id/?

Tam H. Doan
ProAndroidDev
Published in
4 min readFeb 3, 2020

TL;DR: The plus sign in “@+id/” tells Android build tools that you are declaring a new resource. And if it does not exist, add to R.java file.

But basically, you don’t have to think, just use “@+id/” anywhere you want!

In Android, when dealing with XML layouts, we come across the ID attribute in the View or Layout tags. From the beginning, we learn that it will assign or refer the id of this View/Layout. However, you might have noticed that they are different variations:

  • android:id="@android:id/myView"
  • android:id=”@+id/myView”
  • android:id="@id/myView"

So in what cases are they used?

Android resources syntax

Prefix “@android:” and Android Resources

Firstly, you should know @android: is a prefix that refer to the predefined properties, layout,… in Android Resources.

Android Resources are resources that commonly needed in Android projects, so Google has predefined them for us to use on our project. Not only @android:id/ but also other prefixes like @android:color/, @android:drawable/, @android:layout/,…

For example:

  • When creating an ArrayAdapter for ListView, we used the parameter android.R.layout.simple_list_item_1.
  • Use @android:color/transparent to indicate transparent color.

These resources are frequently used by a lot of developers and thanks to Google, all we need is to reference when needed.

Project resources

When working with your own resources that you have created, you need to use @+id/ and @id/.

Basically, you don’t have to think, just use “@+id/” anywhere you want.

However, I will tell you how it works under the hood. If simplicity is what you like, you can skip the sections below.

ID resources

ID resource is a simple resource defined in XML that provides a unique identifier for other project resources. An ID resource does not always reference an actual resource item, it is simply a unique ID that you can attach to other project resources or use as a unique integer in your app.

Android R.java file

R.java is an auto-generated file by aapt (Android Asset Packaging Tool) that contains ID resources for all the resources inside the res/ directory. This file will be generated when the app is building.

A standard Android R.java file. Source: Programmering

So what is the difference between “@+id/” and “@id/”?

As you can see, they are just a character in different — the plus sign (“+”).

The plus sign tells the Android build tools that you are declaring a new resource.

For example, when you specify an ID to an XML resource using the plus sign — in the format android:id="@+id/myView"—it means that a new ID resource with the name “myView” needs to be created, and if it does not exist, create a unique integer for it and add to R.java. Then in Java code, you can refer to this entry by View view = findViewById(R.id.myView).

On the other hand, when using “@id/”, you are referring to an existing resource (already in R.java).

For example, in XML layout:

  • When you want to define an ID for a new View. You have to add the plus sign like: android:id=@+id/myView .
  • When you want to reference the ID of another View that is predefined like android:layout_below=“@id/myTextView”, then just use @id/ , the parser will automatically understand and link to that entry in R.java.

Are there any exceptions?

Yes, if you write a function anywhere in Java file, you can call it, regardless of the order. The difference is that in XML the layout will parse from top-down, so the code below will fail to compile because it can’t find an existing R.id.check.

In this case, we have two solutions.

Firstly, bring the Button tag with id/start under Button with id/check. But, this approach can only be used for certain types of layout.

When using ConstraintLayout or RelativeLayout, changing the order in the XML layout still keeps the actual order displayed on UI but with LinearLayout that is not the case. Moreover, code like this can be more error-prone by hindering the readability and maintenance of the XML layouts.

So what if I need LinearLayout, but still want it to be easy to read?

Moving on to the second approach, we see here the id/check appears more than 1 time. So remember the rules: the first time you use @+id/ to add a new resource, then from there you can refer to it anywhere with @id/.

And everything works fine, as usual, the XML layout is also easy to read.

Conclusion

If simplicity is what you like, just use @+id/ anywhere you want. But it is always good to learn something new, right? The choice is yours!

Happy coding~

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Tam H. Doan

Software Engineer, also a Writer and Runner

Responses (1)

Write a response