Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Lists with RecyclerViews Creating the Item Layout

Ziyang Tan
Ziyang Tan
6,881 Points

Different between android:text and tools:text

What's the different Different between android:text and tools:text? Why use the tools:text instead of android:text?

3 Answers

Wesley Seago
Wesley Seago
10,424 Points

Per the documentation:

The tools namespace is a specially recognized namespace by the Android tools, so all the attributes you define on view elements in the tools-namespace will be automatically stripped when the application is packaged and there is no runtime overhead.

These are attributes which are used when the layout is rendered in the tool, but have no impact on the runtime. This is useful if you for example want to put sample data in your textfields for when you are editing the layout, but you don't want those attributes to affect your running app.

To use designtime attributes, first make sure you have the tools namespace defined in your layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" ...

Hope this helps!

Slava Fleer
Slava Fleer
6,086 Points

android:text --> what you would see while running the app

tools:text --> what you would see just on preview window (when you need to design layout, but won't it to see on layout in app)

Abhinav Kanoria
Abhinav Kanoria
7,730 Points

In this project, we use tools:text="Something" so that we can have some placeholder text while creating our custom layouts. Eg: We wrote tools:text="12 AM" for the timeLabel in the hourly_list_item.xml file. We do this because we want to make sure we have sufficient space to display the longest String that can appear. Just as a rough example, suppose you have a TextView called 'cityLabel' in your custom layout that will display the cities extracted from a database or from the web. Now, to make sure that you're making space for the longest possible city name you'd write something like: tools:text="Thiruvananthapuram". (That is the city with the longest name in my country India). So we can be sure that any city that will appear will have length of its name smaller than or equal to 'Thiruvananthapuram'. But we don't wish to display this city in out App. We want it in the preview only, not the actual app! I hope this helps.