Monday, February 25, 2013

Android defining ID'S at res/values.xml


Android allows that you define ID of user interface components dynamically in the layout files, via the @+id/your_id notation.

To control your IDs you can also create a file called ids.xml in your /res/values folder and define all IDs in this file.
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <item name="button1" type="id"/>

</resources> 
This allow you to use the ID directly in your layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="27dp"
        android:text="Button" />

</RelativeLayout> 

No comments:

Post a Comment