Android Design time attributes

Posted By : Piyush Choudhary | 30-Jun-2017

It is a common desire of most android developers while building the UI layout of app to preview the design of what currently is being built .

For this purpose developers usually add sample data , like android:text="Dummy Text" to see how it actually looks in the layout . 
Many times we forget to remove that sample data which creates problem . It is even more stressful task to preview when developer is designing a list .
To see the actual output data we have to run the app .

"Designtime" attributes play their role well here to fulfill this purpose and avoid any problems in case of any dummy data left unremoved during compile time .

 


What are design time attributes ?


These are those attributes which are used only to render the Android layput preview . These attributes do not have any effect on runtime of application . 

Designtime attributes are specified with any UI widget by using tools namespace in root tag of XML layout.

 

How to use designtime attributes in XML layout .

1) Declare tools namespace with root tag of your XML layout

To use the designtime attributes , first the tools namespace needs to be declared whithin root tag of the layout . It shoul be used like this :-

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    ...
    >
    ...
</RelativeLayout>

All the tools attributes gets eliminated at compile time by Android Asset Packaging Tool(AAPT) , that is why they have no effect during runtime .

2) Replace the android namspace with tools 

We can replace almost every android xml attribute with tools , for designtime . Example :-

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...
    tools:text="Dummy text, this is a long text to test design time attributes , shoudl not appear during runtime"
    />

here we have replaced android:text attribute with tools:text

Usage with Listviews and Recyclerviews 

a) Listview

We can use designtime attributes with listview to see or preview how the list looks if we have designed the list item for it . It is used like 

<ListView
    android:id="@+id/list_songs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:listitem="@layout/song_item"
    />

b) Recyclerview

Same way we can use it with recyclerview this way 

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_songs"
    android:layout_width="match_parent"
    android:layout_height="wrap_parent"
    tools:listitem="@layout/song_item"
    android:scrollbars="vertical"
    tools:scrollbars="none" />

 

Things to take in account for designtime attributes 

1)These attributes work only with Android framework attributes . It does not work with     with android custom attributes .
2)We can use them only with Android layout files. we can not use them for drawables,menus, etc.

About Author

Author Image
Piyush Choudhary

Piyush is a bright mobile app developer, he has expertise in building Native Android Applications and Core Java.His hobbies are reading tehnical blogs, playing snooker and console gaming.

Request for Proposal

Name is required

Comment is required

Sending message..