• By Ben Van Daele
  • February 12, 2013

Simplify your life as Android dev: densityconv and grabids scripts

I’m publishing two scripts that may be of use to Android developers. Both of them can be found in our android-utils repo on GitHub. Ideally, we’ll be pushing a bunch of useful tools daily*.


densityconv (Density Converter) is simple script that takes a density (ldpi, mdpi, hdpi, xhdpi) and a value (in dip) and converts it to all other densities.


% densityconv x 480
l 180.00
m 240.00
h 360.00
x 480.00

Really simple, but helps you avoid a lot of boring calculations.

You need to install the argparse Python module to run the script.


grabids (Grab IDs) can parse an Android resource XML file, get all ID definitions from it, and print out Java code that does a findViewById for the IDs. It aims to take away some of the tedium of writing boilerplate code when creating Android layouts.


% grabids res/layout/my_list_item.xml
ImageView avatar = (ImageView) findViewById(R.id.avatar);
TextView name = (TextView) findViewById(R.id.name);
TextView tagline = (TextView) findViewById(R.id.tagline);
TextView joinDate= (TextView) findViewById(R.id.join_date);

You can optionally use --split-fields (-f), which will split the declaration and the assignment of the variables, so you can make fields out of your views.

The --view (-v) switch allows you to specify a view, on which the findViewById calls will be made.


% grabids -f -v myLayout res/layout/my_list_item.xml

private ImageView avatar;
private TextView name;
private TextView tagline;
private TextView joinDate;

avatar = (ImageView) myLayout.findViewById(R.id.avatar);
name = (TextView) myLayout.findViewById(R.id.name);
tagline = (TextView) myLayout.findViewById(R.id.tagline);
joinDate= (TextView) myLayout.findViewById(R.id.join_date);

The script needs argparse and ElementTree.

Android development has its fair share of annoyances. Luckily the good outweighs the bad, and the community has come up with a lot of smart shortcuts. I hope my contributions to that effort are of some use to you. Enjoy!

* On second thought, better check back monthly. Or at least post-Barcelona. 😉


Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates.

You have Successfully Subscribed!