difference between @id and @android:id

<?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="@android:id/list" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_weight="1" />
    <Spinner android:id="@+id/section_spinner"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_margin="5dp" />
</LinearLayout>

what is the difference between @android:id and @id in this case?


Solution 1:

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.

@+id/section_spinner means you are creating an id named section_spinner in the namespace of your application. You can refer to it using @id/section_spinner .

@android:id/list means you are referring to an list defined in the android namespace.

The '+' means to create the symbol if it doesn't already exist. You don't need it (and shouldn't use it) when referencing android: symbols, because those are already defined for you by the platform and you can't make your own in that namespace anyway.

Solution 2:

You need to use @+id when you are defining your own Id to a view, which in this case is section_spinner. And @android:id is used when you need to set an Id of a view to android's pre-defined Id in framework. for e.g when using ListActivity, TabWidget/FrameLayout in TabHost and etc.

Solution 3:

id your own id, Android id is default id present in android platform res/values/ids.xml had some ids as below for API 10

res/values/ids.xml

these all are default ids from android

<resources>
  <item type="id" name="background" />
  <item type="id" name="checkbox" />
  <item type="id" name="content" />
  <item type="id" name="empty" />
  <item type="id" name="hint" />
  <item type="id" name="icon" />
  <item type="id" name="icon1" />
  <item type="id" name="icon2" />
  <item type="id" name="input" />
  <item type="id" name="left_icon" />
  <item type="id" name="list" />
  <item type="id" name="menu" />
  <item type="id" name="message" />
  <item type="id" name="primary" />
  <item type="id" name="progress" />
  <item type="id" name="right_icon" />
  <item type="id" name="summary" />
  <item type="id" name="selectedIcon" />
  <item type="id" name="tabcontent" />
  <item type="id" name="tabhost" />
  <item type="id" name="tabs" />
  <item type="id" name="text1" />
  <item type="id" name="text2" />
  <item type="id" name="title" />
  <item type="id" name="title_container" />
  <item type="id" name="toggle" />
  <item type="id" name="secondaryProgress" />
  <item type="id" name="lock_screen" />
  <item type="id" name="edit" />
  <item type="id" name="widget_frame" />
  <item type="id" name="button1" />
  <item type="id" name="button2" />
  <item type="id" name="button3" />
  <item type="id" name="extractArea" />
  <item type="id" name="candidatesArea" />
  <item type="id" name="inputArea" />
  <item type="id" name="inputExtractEditText" />
  <item type="id" name="selectAll" />
  <item type="id" name="cut" />
  <item type="id" name="copy" />
  <item type="id" name="paste" />
  <item type="id" name="copyUrl" />
  <item type="id" name="switchInputMethod" />
  <item type="id" name="keyboardView" />
  <item type="id" name="closeButton" />
  <item type="id" name="startSelectingText" />
  <item type="id" name="stopSelectingText" />
  <item type="id" name="addToDictionary" />
  <item type="id" name="accountPreferences" />
  <item type="id" name="smallIcon" />
  <item type="id" name="custom" />

Solution 4:

@id 

You refers to you own defined id in your project.

and

@android:id

You refers to ids defined by android framework