How to add id to public.xml?

14,298

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.

Share:
14,298
yrajabi
Author by

yrajabi

Co-founder at Rahpou, Android developer and enthusiast.

Updated on June 30, 2022

Comments

  • yrajabi
    yrajabi almost 2 years

    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
    yrajabi over 11 years
    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 <item type="id" name="index_row_search" /> in ids.xml and then <public type="id" name="index_row_search" id="0x7f060000" /> in public.xml and it goes without error and seems the id would stay constant between builds. Hooray!
  • zhang xuefeng
    zhang xuefeng over 8 years
    why am i getting different ID values by using this way @yrajabi