21

I am wondering what's the difference between @+id/android:list and @+id/list. I know the last one which is a regular id assignment but the first looks different. What makes it special?

Where I saw it: I was studying on ListView, ListAdapter and things like that and the author define the ListView in layout xml file as below :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
<TextView
    android:id="@+id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/main_no_items"/>
</LinearLayout>

and also let me mention @+id/android:empty id as well.

And he also extends ListActivity class.

Here is the source of the article.

And also what's in my mind as questions are :

  1. Should we extend ListActivity? Maybe I want an Activity which also contains other Views.
  2. We use @+id/android:list just because we extend ListActivity or we can use the same convention if we extend Activity?

Thanks.

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
Tarik
  • 79,711
  • 83
  • 236
  • 349

3 Answers3

46

Resource IDs in Android are specific to a package (which is good, or else you'd have lots of conflicts if your app is dealing with several packages at the same time).

@+id/list will create a resource ID in your app (=your package) with the name "list" and give it a unique ID. In code, that would be R.id.list.

@android:id/list will use the ID "list" from the package android (which, in code, would be android.R.id.list.

EDIT: Need to add the corrections David Hedlund pointed out: The proper reference would be @android:id/list. Also, + indicates you're defining a new ID - you obviously don't need that when you're referencing something that was defined in the Android API.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • 1
    This is the correct description of what is going on, but as elevine points out, the syntax is `@android:id/list`. The `+` implies adding something to `R`, whereas omitting the plus simply implies referencing something that is already defined. – David Hedlund Dec 04 '10 at 20:26
  • Should it be `@+id/android:list` or `@id/android:list` ? – Tarik Dec 04 '10 at 20:26
  • Thanks David, I copied it from the question without thinking. – EboMike Dec 04 '10 at 20:28
  • @Brave: `@id/android:list`, because the Android API already defined `list`. – EboMike Dec 04 '10 at 20:28
  • 3
    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.*** from : http://developer.android.com/reference/java/text/Annotation.html – Tarik Dec 04 '10 at 20:29
  • The above link is outdated. Check this: http://developer.android.com/guide/topics/ui/declaring-layout.html – zeekvfu Jul 22 '14 at 16:34
11

I think the example code you posted has a typo, so it should be @android:id/list (without the +). From the ListActivity javadoc:

your own view MUST contain a ListView object with the id "@android:id/list"

@android:id/list is specific to ListActivity, so you do not need it if you are adding a ListView into any other kind of Activity. You should extend ListActivity if you want the user to do more than view the list. For example, you can override ListActivity.onListItemClick to respond to clicks on an item in the list.

Similarly, @id/android:empty (again, without the +), is a special case for ListActivity. This allows you to specify an alternative view that should be displayed when your list is empty. That View will not be displayed when the list is populated.

Eric Levine
  • 13,536
  • 5
  • 49
  • 49
0

in android,

In XML: @[package:]layout/filename

like

android:id="@+id/android:list"

This is the standard way to refer to a list view when refering to listFragment or listActivity

so filename is android:list is a reference to ListView.

navigate to res/values/ids.xml

you will find <item type="id" name="list" />

ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

Ahmed Ali
  • 671
  • 7
  • 10