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:
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.
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
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
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.
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.