In this post

In this post, we are going to talk about tools attributes. At LetsNurture, we all droiders always prefer to use tools attributes while designing layouts for the android applications. It allows us to view particular thing like text, image, list item preview, etc. while designing layouts and so it is time saving thing.

While developing apps with Android Studio, we need to see the actual output of the XML layouts by running app every time after some changes in layout :(Most of all the android developers puts some demo data (like lorem ipsum) to see the output of any view/layout, but as you know those data are demo data and for the demo purpose only!

Before going ahead, let me share couple of issues that we face while developing android applications:

Some Issues

Let’s understand with real time problem. We take one TextView which display current username at runtime from API response.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"   
    android:id=”@+id/tv_user_name”
    android:text="@string/test_foo_user_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Two possibilities are there while running app:

  1. First is after getting successfully API response and you will be able to see current username in TextView.
  2. Second is in some case(like Network issue) you are failed to get API response. Now you will see your test_foo_user_name! Obviously it’s not expected output to see!

There are other same cases we need to take care of while developing apps.

“How nice if I can be able to set text to my TextView only for the preview while designing layouts”.

Sharing another example to explain above points:
You are developing a product app having 50+ layouts and out of which there are 20+ different listview/recyclerview screens having different row item layouts. Here in this case either you can see the output by running application and checking the output into the device. Or there is another alternate way is to use tools attribute which would allow you to see the output while designing layouts or making changes into the layouts.

Sounds good! Yes you can.

Look at the below example layout, one is without using tools attribute and another is using tools attribute.

ListView with and without tools:listitem
ListView with and without tools:listitem

Why tools attributes

Now the tools attributes comes into the picture! While creating layouts for the application, you might observed an extra namespace getting declared in the root layout.

xmlns:tools="http://schemas.android.com.tools"

There are tools attributes available and each of them provide different functionality. Some of those actually allow us to view screen designs while designing layouts using layout editor in Android studio, like rendering view (recyclerview items) and some of those provide different functionality then designing layouts like suppressing certain Lint warnings. To use tools attribute, we need to include tools namespace in top level layout in xml layout file.

xmlns:tools="http://schemas.android.com.tools"

For example:

<FrameLayout 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" >
    ....

Let’s discuss Tools Attributes:

Let’s categorize attributes through the usage and it’s functionalities like:

  • Lint
  • Android studio layout editor
  • gradle

1. Starting with Lint attributes:

Using this attribute, you can configure lint checking in Java and Xml files.

Note: While configuring this in XML, don’t forget to add tools namespace in your xml layout:

namespace

xmlns:tools="http://schemas.android.com/tools" 

Below are the tools attributes which we can use for modifying lint functionality:

  • tools:ignore

    While adding tools:ignore attributes specific section is ignore by lint checking.
    Ex. Lint check is also disabled for the child element.

    <LinearLayout
    ...
        xmlns:tools="http://schemas.android.com/tools"
        tools:ignore="UnusedResources" >
        
        <TextView
            android:text="@string/str_foo_string" />
    
    </LinearLayout>
  • tools:targetApi

    This attribute is like the @TargetApi annotation in Java classes: you declare, via the parameter, what API level you have addressed in your code

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)

    It allow you to specify an API level, either as an integer or as code name, that this element is known to be running on.

     <GridLayout tools:targetApi="ICE_CREAM_SANDWICH" >
     
  • tools:context

    Set on the root element in a layout XML file, and records which activity the layout is associated with
    For example:

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
    	    …
        tools:context=".MainActivity" ... >
    
  • tools:locale

    This indicate which language is used in resource file. Strings.xml can have tools:locale attribute in root element:

    <resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">
    
  • If you want to disable more than one warning, list down the warnings to disable in a comma-separated string. For example: tools:ignore="LintWarning,secondLintWarning."
  • And if you want to disable all the Lint warnings in the XML element, use the all keyword: tools:ignore="all"

2. Let’s discuss DesignTime Layout attributes:

  • tools:text

    While designing layout, set the text using tools:text instead of android:text to view the output. tools:text allows you to view the demo text in TextView while designing layouts.

    For example:

    <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools
        ...
        ...>
        
        <TextView
            android:id="{textView Id}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/this_string_for_runtime"
            tools:text="But this long string is for preview only using tools attributes" />
    
     </RelativeLayout>
    

    Suppose if you have a multiline textview then you would be able to see your layout output for the TextView that how it actually fits in the layout with long text!

  • tools:layout

    While designing layouts, you can use tools:layout=”@layout/your_fragment_layout” to view your fragment layout at design time.

    For example:

    <fragment 
        android:name="com.example.app.fragment.ItemListFragment"                 
        tools:layout="@android:layout/list_content" />
    
  • tools:listitem / listheader / listfooter

    These attributes can be used on (or other AdapterView children like GridView, ExpandableListView, etc) to specify which layouts to use for the list items, as well as list headers and list footers at design time. The tool will fill in dummy data to show a list with some representative contents.

    For example:

    <ListView
       android:id="@+id/lvOrders"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:listitem="@layout/row_item_orders" />
    
  • tools:showIn

    Attribute set on the root element of a layout that included by another layout. This allows you to point to one of the layouts which includes this layout, and at design time this included layout will be rendered with the outer layout surrounding it

    Ex: set this attributes to row_item_order (see above ListView example) layout to get idea how your listitem show in listview.

    <?xml version="1.0" encoding="utf-8"?>
    <ListView   
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"     
      tools:showIn="@layout/list_view_layout" />
    

Let’s look at the examples of using above tools attributes:

1: ListView example for tools:listitem

ListView with and without tools:listitem
ListView with and without tools:listitem

2: ListView row item with tools:showIn

Add this attribute line in your listview or recyclerview to preview row item layout: tools:showIn="@layout/your_listview_layout"

3. Gradle tools attributes:

  • tools:shrinkMode

    While using resource shrinking to automatically strip out unused resources, you can specify whether the Gradle plugin should play. The default is to play it safe; this is shrinkMode="safe" . To instead have the Gradle plugin only consider explicitly referenced resources, use shrinkMode="strict"

    Ex.

    <?xml version="1.0" encoding="utf-8"?>
    <resources 
       xmlns:tools="http://schemas.android.com/tools"
       tools:shrinkMode="safe" /> 
    
  • tools:keep

    When using resource shrinking to automatically strip out unused resources, and when using shrinkMode="strict", you can specify specific resources to keep. The value is a comma separated list of resource references (and a globbing pattern is allowed.)

    For example:

    <?xml version="1.0" encoding="utf-8"?>
    <resources 
       xmlns:tools="http://schemas.android.com/tools"
       tools :keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*" />
    

Tip:

If you want to use tools:ignore to silence Lint warnings, you may have to Google to figure out the exact text value for your exact warning.

Wrapping Up

And that’s it! In this post we discussed about tools attributes which helps in increasing the efficiency of android developer’s daily work and build high quality apps using android studio. If you want to learn more tools attributes, see Tools Attributes tech doc.

Specially thanks to our Technical Lead Paresh Mayani for introducing tools attributes and encourage me to write this blog.

Thanks for reading this article. I hope you have found this article helpful!. Meanwhile, looking forward to hear back suggestions and inputs on this blog!

References:

Pranay Patel

Software Engineer at @LetsNurture || #AndroidDev #twitter || @SOreadytohelp #github #java #database #security #ethicalhacking || #TEA

Want to work with us? We're hiring!
  • Vinay Maneti

    really cool stuff.