Useful Tools Attribute for Android Development
Hello, world
Android Studio supports a variety of XML attributes in the tools namespace which enable design-time features (such as which layout to show in RecyclerView & Fragment). This is only to show features at design-time so when you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior. To use these attributes we need to add tools namespace to the root element of an XML layout. For example
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
</LinearLayout>
1. @tools:sample/*
This attribute allows you to inject placeholder data or images into your view. For example, if you want to test how your layout behaves with text, but you don’t yet have finalized UI text for your app, you can use placeholder text as follows:
<TextView
tools:text="@tools:sample/lorem"
/>
Available sample for placeholder are :
- @tools:sample/full_names
- @tools:sample/first_names,
- @tools:sample/last_names,
- @tools:sample/cities,
- @tools:sample/us_zipcodes,
- @tools:sample/us_phones,
- @tools:sample/lorem,
- @tools:sample/date/day_of_week,
- @tools:sample/date/ddmmyy,
- @tools:sample/date/mmddyy,
- @tools:sample/date/hhmm,
- @tools:sample/avatars,
- @tools:sample/backgrounds/scenic
2. tools:layout & tools:itemCount
tools:layout attribute declares which layout you want the layout preview to draw inside the Fragment or RecyclerView.
tools:itemCount specifies the number of items the layout editor should render in the Preview window for RecyclerView.
<androidx.recyclerview.widget.RecyclerView
tools:listitem="@layout/item_card"
tools:itemCount="5"
/>
<fragment
tools:layout="@layout/activity_data_binding"
/>
3. tools:listitem / tools:listheader / tools:listfooter
These attributes specify which layout to show in the layout preview for a list’s items, header, and footer. Any data fields in the layout are filled with numeric contents such as “Item 1” so that the list items are not repetitive.
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:listitem="@layout/sample_list_item"
tools:listheader="@layout/sample_list_header"
tools:listfooter="@layout/sample_list_footer" />
4. tools:menu
This attribute specifies which menu the layout preview should show in the app bar. The value can be one or more menu IDs, separated by commas (This attribute specifies which menu the layout preview should show in the app bar. The value can be one or more menu IDs, separated by commas.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:menu="menu1,menu2" />
Thanks for reading..