Android Tools attributes: listItem & sample data rocks!

Thibault de Lambilly
AndroidPub
Published in
4 min readNov 4, 2017

Do you know layout tools attributes ? You know the one we import sometimes without really don’t know what for.

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

I use to only use the tools:text one with a plain text in it. While I am a little more brave, I would but the label in a string.xml file, but that’s all.

I have discovered much more watching the Def Fest Nantes 2017 cast, thanks to Nicolas Roard conf speaking about ConstraintLayout. (When I think that it was 200 meters from my work, and I had to watch it on Youtube :’( — life is a b…. haha

Show me the real life !

So 2 things stoke me:

1 — the simple use of tools:listitem that allows you to replace the randoms lines of a “List Item” or a “Recycler View” line in the Android Studio layout editor.

By just adding this line on my Recycler View in my layout like this:

tools:listitem="@layout/item_subject_row"
list item turning to real

My layout shows not random line anymore, but the real item I have built for the actual application, directly shows inside the editor.

Isn’t that as cool as easy ? but much more to come

2 — Nicolas Roard spoke about Sample Data as well.

Since Android Studio 3.0, you can use predefine data offered by Android Studio or create a specific folder of “Sample data” where you will put fake data to be displayed directly in your layout, right directly in the layout editor.

Predefine data

Android studio 3.0 brought a number of predefine data through the tools attributes, to visualized easily your layout structure.

inside a tools:text attribute, just use @tools/data/ options, eg:

tools:text="@tools:sample/last_names"

Android studio is offering you a list that includes:

last_names, first_names, full_names, cities, avatars, backgrounds/scnenic, date/ddmmyy, date/day_of_week, date/ddmm, date/hhmmss, date/mmddyy, lorem, lorem/ramdom, us_phones, us_zipcodes

At last found the documentation page for the tools attributes!!

My own sample data

To create your fake/sample data folder, just right click on the “app” folder then “new > Sample Data directory”

create sample data folder

It will create a “sampledata” folder where you can put your data, inside simple files.

Inside your folder, add plain text file where you put raw data, one by line (can be colors such as #ff33aa or just text). Then in your layout you access in your tools:text attributes by referencing @sample/nameOfTheFileGiven

tools:text="@sample/my-subjects"

But even better! you can use Json object to show more complex data

In my “sampledata” folder, I have created a file called subects.json where I have put these data in it.

{
"data" :[
{"idweb": -1, "sourceId" : 1, "name" : "AIRCRAFT ACCIDENT ..."},
{"idweb": 1, "sourceId" : 1, "name" : "ANNEX 1 ..."},
{"idweb": 2, "sourceId" : 1, "name" : "ANNEX 11 ..."},
{"idweb": 3, "sourceId" : 1, "name" : "ANNEX 12 ..."}
]
}

The tricky thing that stuck me for a moment, it that you cannot start your Json file by a table. It must be a Json object at the root. And must compile your project to see your new/updated data

Then you access your data such as this:

tools:text="@sample/subjects.json/data/name"

Android studio is really helpful and usually, shows you directly all the options it knows from your files as soon as your type @sample/ (don’t forget to make the project first).

I pronounce you husband and wife

If you combine tools:text and sample data (together with tools:listitem in my case) you can show without launching your app how your screen as it would be.

So my screen is now became:

using sample data and tools:text

What a time saver to build and “debug” your screen

--

--

AndroidPub
AndroidPub

Published in AndroidPub

The (retired) Pub(lication) for Android & Tech, focused on Development

Thibault de Lambilly
Thibault de Lambilly

Written by Thibault de Lambilly

Mobile enthusiast, Kotlin lover | full stack by pleasure