tools:title=”RemoteSampleData”

- 2 mins

What I’ve learned from my previous article is that the best way to gain new knowledge about a certain topic is to write about it. When I first wrote the article, I had no idea that you can create custom sampledata for your Android project. (If you don’t have a clue what sampledata is, please read tools:title=”LayoutPreview”) And then someone sent me this.

It sounded like a really cool feature, so I made the list of my favourite characters from Lord of the Rings, used it as sampledata, and got this glorious layout preview.

LotR

Later on, I wanted to include it in my other projects, but the only option that I had was to copy/paste the sampledata from one project to another, and that didn’t make any sense. Then I did what every (lazy) developer would do, asked a question on StackOverflow.

After 10 views and no answer in sight, I’ve decided to sacrifice a bit of my reputation and start a bounty. And it worked. I got my answer, which I will share with you.

To use sampledata between multiple projects you first have to host it and then have a Gradle script which will download it into your project’s sampledata folder. This is the code which does that:

The first script will delete sampledata once you do clean build, and the second one will create sampledata folder in case it doesn’t exist, and then download sample files into it. It works like a charm and from now on you can go crazy with your layout previews. I know that from now on I will never again use @tools:sample/lorem/random.

Developers developers

This code can be found in GitHub repo.

I’ve also added some sample data, and will keep adding more. If you notice that something is missing or could be improved, feel free to contribute. This could be a great opportunity for novice developers to make their first contribution 😉.

Thanks for reading! If you enjoyed this story, please share it! Also, feel free to leave a comment 💬 below.

Vladimir Jovanovic

Vladimir Jovanovic

Android Developer

comments powered by Disqus
rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora