Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining IDs within style, is it safe or a disaster?

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.

like image 997
harism Avatar asked Apr 05 '11 20:04

harism


2 Answers

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

like image 56
Sensei Avatar answered Oct 02 '22 08:10

Sensei


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.

like image 40
Varun Vishnoi Avatar answered Oct 02 '22 08:10

Varun Vishnoi