android – Defining IDs within style, is it safe or a disaster?-ThrowExceptions

Exception or error:

Following question has kept me puzzled for a while and I thought maybe asking about this does no harm. I have the following layout.xml and style.xml files;

res/layout/layout.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="fill_parent" >
    <LinearLayout
        style="@style/headerContainer" />
    <LinearLayout
        style="@style/footerContainer" />
    <ScrollView
        style="@style/contentContainer" />    
</RelativeLayout>

res/values/style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="container">
    <item name="android:layout_width">fill_parent</item>
    </style>
    <style name="headerContainer" parent="container">
        <item name="android:layout_height">40dp</item>
        <item name="android:layout_alignParentTop">true</item>
        <item name="android:background">#80FF0000</item>
        <item name="android:id">@+id/header</item>
    </style>
    <style name="footerContainer" parent="container">
        <item name="android:layout_height">50dp</item>
        <item name="android:layout_alignParentBottom">true</item>
        <item name="android:background">#8000FF00</item>
        <item name="android:id">@+id/footer</item>
    </style>
    <style name="contentContainer" parent="container">
        <item name="android:layout_height">60dp</item>
        <item name="android:layout_below">@id/header</item>
        <item name="android:layout_above">@id/footer</item>
        <item name="android:background">#800000FF</item>
    </style>
</resources>

Now, the question is, is there a danger of overlapping IDs as I’m introducing them in style.xml? Funny thing is that this approach works, on the emulator I’m using at least, but the created IDs are not being added to the R class. And I’m a bit confused how they are defined once my layout is inflated.

How to solve:

Don’t use @+id/... in styles.
@+id/... can only be used in layouts.
Otherwise you can get Error executing apt: return code 139 during build.

Use @id/... and generate ids with help resource file if needed:
res/values/ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="header" />
    <item type="id" name="footer" />
</resources>

http://developer.android.com/guide/topics/resources/more-resources.html#Id

###

Its not safe my friend. You should use different id for different files. Here the emulator will not act problem. It will understand because for every xml file there is unique code defined R.java file automatically. So emulator will understand from there very easily. But if you need to improve or edit the code for any up gradation surely you will get confused which id is belonging to which xml file’s layout or wedged. So provide unique id to every widget of layout. It will be helpful if you provide the id including some tag of respective file name.

Example: If file name is filldetails.xml then you can use id=@+fd_name

It will be helpful to know the flow of application.

###

I am doing it this way and have had good luck:

Layout
res/layout/main.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="fill_parent"
    >
    <include layout="@layout/action_bar"/>
</RelativeLayout>

Common:
res/layout/action_bar.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/action_bar_container"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/action_bar_height"
    android:layout_alignParentTop="true"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    >

    <TextView
        android:id="@+id/action_bar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="20dip"
        android:textStyle="bold"
        />

</RelativeLayout>

Leave a Reply

Your email address will not be published. Required fields are marked *