Android - Geek went Freak!

Android

@android:id vs @id

@id

This refers to the ids defined in the application, either in layouts itself using @+id/<id-nam> or in ids.xml. These ids are exposed to Java through R.id.

@android:id

This namespace contains builtin ids from android itself. These ids are exposed to Java through android.R.id.

Ids in Android layout

The layouts are designed in an XML backed layout editor. When the project is built, android tools compile the XML layouts and other resources into corresponding Java code that can later be inflated.

Each view in the layout must have an unique Id to be able to reference them. Here are some scenarios where giving an unique Id to a view in Layout is required:

  1. To cross reference one view from another view in the layout

  2. To query for the view and get a reference to it from Java code

Id namespace

In android, the Ids have no namespacing. Ids from all layouts are clumped into the same R.id namespace.

android.R.id namespace

android.R.id is namespace through which android exposes the internal Ids. These Ids are defined by/in android itself.

Define and Id in Layout

<TextView
    android:id="@+id/txtCounter"
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

Note: The + following @ means that android should create an Id with the name txtCounter if it doesn’t exist already. If such Id exists, the existing Id is used.

Define Ids in ids.xml

One can manually define the required Ids in ids.xml using item tag with type="id". The name property defines the name of the defined id.

<item name="txtCounter" type="id"/>

The Id can be later used in layouts and Java.

<TextView
    android:id="@id/txtCounter"
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
findViewById(R.id.txtCounter);