what is ids.xml used for?

Android

Android Problem Overview


Just a quick question, what is the ids.xml used for when developing an Android app? I saw an example on the android resources webpage which contained:

<resources>
  <item name="snack" type="id"/>
</resources>

What would this be used for?

Android Solutions


Solution 1 - Android

id.xml is generally used to declare the id's that you use for the views in the layouts.

you could use something like

<TextView android:id="@id/snack">

for your given xml.

Solution 2 - Android

ids.xml has the following advantage: all ids were declared, so compiler can recognize them. If something like this:

<TextView
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBelow="@id/text2"
    android:text="...."/>
<TextView
    android:id="@+id/text2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="...."/>

Can result in compiling error because text2 was refered before declared

Solution 3 - Android

When creating views dynamically, predefining id's in ids.xml gives the posibility to reference a newly created view. After you use the setId(id) method you can access the view as if it had been defined in XML. This blog post has a nice example.

Solution 4 - Android

Another application for id.xml is in respect to layouts and library projects. Let's say you specify a generic list of options in a library (dialog) layout

<CheckedTextView android:id="@+id/checked_option_one"...
<CheckedTextView android:id="@+id/checked_option_two"...
...

and handle these views in a generic (dialog) fragment

optionOneCheck = (CheckedTextView)rootView.findViewById(R.id.checked_option_one);
optionTwoCheck = (CheckedTextView)rootView.findViewById(R.id.checked_option_two);

If you remove any of the view declarations from a copy of the layout in a main project, you will get a "no such field" error exception at runtime.

The compiler doesn't complain, but at runtime the id isn't actually there/known.

Declaring the ids in id.xml and using

<CheckedTextView android:id="@id/checked_option_one"...
...

avoids the runtime error

Solution 5 - Android

ids.xml is much more powerful. You can predefine values like

<item name="fragment_my_feature" type="layout"/>
<item name="my_project_red" type="color"/>

not only id. Actually you can use more resource types: https://developer.android.com/guide/topics/resources/more-resources

It's extremely helpful to predefine some layouts, colors etc. in root module for multi-module app. You can keep actual layouts, colors, etc. in specific app module which has root module as dependency.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsamView Question on Stackoverflow
Solution 1 - AndroidYashwanth KumarView Answer on Stackoverflow
Solution 2 - AndroidNgo Phuong LeView Answer on Stackoverflow
Solution 3 - AndroidJose_GDView Answer on Stackoverflow
Solution 4 - AndroidmirView Answer on Stackoverflow
Solution 5 - AndroidPrzemoView Answer on Stackoverflow