3

In my project, I needed to set always-constant IDs to Views. I mean IDs that are constant between different app builds. After some investigation I found that it can be achieved by using values/public.xml and any id declared in that file would not change on future builds. Now the problem is that I can't define an id of a view in some layout file. This is my layout.xml containing an ImageView with an Id which should be added to public.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/menu_item_selector" >

<ImageView
    android:id="@+id/index_row_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="@dimen/padding_small" />

<ImageView
    android:id="@+id/index_row_search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@drawable/index_row_search" />

<TextView
    android:id="@+id/index_row_caption"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@id/index_row_icon"
    android:layout_toRightOf="@id/index_row_search"
    android:gravity="right"
    android:textAppearance="?android:attr/textAppearanceLarge" />

and this is public.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <public type="string" name="no_internet" id="0xAA0a0001" />
        <public type="id" name="index_row_search" id="0xAA0b0015" />
</resources>

eclipse shows an error on the line where I have added id of "index_row_search" and says:

error: Public symbol id/index_row_search declared here is not defined!

but as you can see in above layout file, I have an ImageView with that id. It's wondering that the string id definition one line above has no error!

So, how should I define Id of a View in public.xml?

yrajabi
  • 637
  • 1
  • 9
  • 28
  • 3
    "I needed to set always-constant IDs to Views. I mean IDs that are constant between different app builds" -- why? – CommonsWare Sep 08 '12 at 22:09
  • Agreed with CommonsWare on that. Also, have you cleaned/rebuilt your project? – Cat Sep 08 '12 at 22:11
  • @CommonsWare because I want later be able to manipulate some layouts, without recompiling, and you know, rebuilding resources by aapt may change some id's. – yrajabi Sep 08 '12 at 22:14
  • 1
    @CommonsWare think of this, as a project that designer likes to change some layouts a bit without any need to recompile and having source. So I should provide him a classes.dex file that uses always-constant id's which won't change on rebuilding. – yrajabi Sep 08 '12 at 22:18
  • @Eric my question is linked to this one: http://stackoverflow.com/questions/9348614/what-is-the-use-of-the-res-values-public-xml-file-on-android – yrajabi Sep 08 '12 at 22:22
  • ...okay, that's fine. I asked if you had cleaned and rebuilt your project, though. – Cat Sep 08 '12 at 22:35
  • @Eric yes, I have cleaned and rebuild many times, but no success. I'm wondering because string definition works but id does not. – yrajabi Sep 08 '12 at 22:39
  • Is it perhaps because the `ImageView`'s ID is `@+id/index_row_search`? Shouldn't it be `@id/index_row_search`? – Cat Sep 08 '12 at 22:43
  • Is the string no_internet present in strings.xml? – nandeesh Sep 09 '12 at 04:10
  • @Eric No, I tested both. – yrajabi Sep 09 '12 at 07:17
  • @nandeesh Yes, the string is defined and has no problem, but id has problem however it is defined, too. – yrajabi Sep 09 '12 at 07:17

1 Answers1

10

Create a new xml ids.xml in values with all the id values which are to be constant.

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

I think its better if you copy the resource ids to public.xml from the R.java, the first time to avoid errors like entry index is larger than available symbols and also to keep the type of resource to be consistent.

Community
  • 1
  • 1
nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • Thanks man, you are my hero :) It seems to work properly. I was completely ignorant of ids.xml which you pointed to. first I declared in ids.xml and then in public.xml and it goes without error and seems the id would stay constant between builds. Hooray! – yrajabi Sep 09 '12 at 09:20
  • why am i getting different ID values by using this way @yrajabi – zhang xuefeng Aug 18 '15 at 07:31