Simplify your life as Android dev: densityconv and grabids scripts

By February 12, 2013 Mobile No Comments

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

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.

Example:

% 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

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.

Example:

% 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.

Example:

% 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. 😉

 

Leave a Reply

Contact us
×