It never ends!

Been ultra super busy the past few weeks. Also learning a lot of new things. And renewing my battle with orientation change, AsyncTasks and dialog boxes.

So, I have been thinking of what to document here next. Here are my choices:
- Loading HTML pages stored in the SD card in a WebView
- Managing your SharedPreferences
- Using a ViewStub

I had a long list in my mind yesterday, but I managed to not write it down. facepalm

~1 min read

stealth ninja mode on

Over the past couple of weeks, this blog has been getting unusually high traffic. Which means I get more than one hit per week.

So if it isn’t too much to ask, can I please know how you got here, and what you were looking for. And please please let me know if I said anything wrong, or if you know of a better/easier/more optimized way to do the stuff I’m trying to talk about. Thanks! :)

~1 min read

That damn seekbar thumb

If you have ever needed to use a SeekBar, you definitely would have noticed how hard it is to move the slider (aka thumb) when it is set to the minimum or maximum value. The slider tends to be cut in half, and fitting your finger into it to press it becomes a test of patience.<div></div>
See how small the slider becomes when it reaches the far ends of the SeekBar? Crazy!

Luckily, I found a way (just today!) to move the slider just a little tiny bit to make it easier to press. Apparently, there is a method called setThumbOffset() that allows us to nudge the slider by a number of pixels.

It’s pretty easy to use, aside from the fact that it accepts pixels and not dip measurements. Anyway, here’s how to do it:<pre class="brush:java">int pixels = convertDipToPixels(8f);
SeekBar mySeekBar = (SeekBar) findViewById(;
mySeekBar.setThumbOffset(pixels);</pre>I convert dip measurements to pixels to better manage the growing number of resolutions of screen sizes present. Here’s the code to do that:<pre class="brush:java">private int convertDipToPixels(float dip) {
DisplayMetrics metrics = new DisplayMetrics();
float density = metrics.density;
return (int)(dip * density);
}</pre>Aaaaaaaand this is now how our slider looks:Applause! Confetti! Applause!

~1 min read

Enabling/disabling menu items on the fly

In one of my applications, I want to disable some menu entries if the database is not valid or is not present at all. To do that, I make use of the onPrepareOptionsMenu() API.

Let’s say the user is on the Quiz activity. When the user presses the MENU button, the Quiz item should not be there anymore; and until the application validates the presence of a database, I want the Books item to be unclickable.

So I inflate my menu as usual from the XML file:<pre class="brush:java">@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(, menu);

return true;

private void removeSomeItems(Menu menu){
MenuItem books = menu.findItem(;
books.setEnabled(false); // I want this item to be available eventually

menu.removeItem(; // I want this item to be unavailable for this activity
You have to remember though, that this method is called only ONCE, the very first time the user opens the menu.

The activity does its business as usual. When I am done checking if the database is there already, I want the user to be able to use the Books item. I have a boolean field that I set when the database is validated, and so now I can enable the menu.<pre class="brush:java">@Override
public boolean onPrepareOptionsMenu (Menu menu){

MenuItem books = menu.findItem(;

return true;

You can change the contents of the menu as often as you want using onPrepareOptionsMenu() because that method is called each and every time the user presses the MENU button.

And that’s it!

1 min read

Just wondering

You know those postscripts that Google engineers have on their posts in forums? I wonder if they have a sort of “standards body” that came up with it. They all sound like this:<blockquote>Note: please don’t send private questions to me, as I don’t have time to provide private support. All such questions should be posted on public
forums, where I and others can see and answer them </blockquote>

I wonder why I wonder about these things.

~1 min read

Quick tip: Quick Formatting of Android XML Files

One of the most useful tools in Android’s Eclipse plug-in is the Layout Editor. It is easy to experiment with layouts using the drag-and-drop enabled editor without having to worry about the correct syntax or if you are using the correct attribute name.

After creating your layout this way, however, you can end up with a messy XML file. I say “messy” in the sense that elements can run on in one veeeeeeeeeeeery long line, and if you are about to edit the XML file manually, this can be a nightmare.

I used to format the XML file by hand, putting in line breaks and correcting indentation. But one edit using the graphical layout editor and it’s messed up again. And then I had a light bulb moment. Eclipse allows auto-formatting of code, but what about XML files? AHA!

It turns out that auto-formatting works for XML files too! Simply select all the contents of the XML file (CTRL-A) and then press the ultra magical shortcut CTRL-I and your XML file is clean and orderly as can be! YAY!

~1 min read

What happened to my layout editor?

There you are, happily creating your layout files in the Eclipse plug-in’s layout editor. Dragging and dropping is a breeze. But then one day, you open a layout XML file and boom! No UI! All you see is the XML tree with all the nodes and attributes. What happened?

This happened to me and I was in a panic for a few seconds. Why do things like these have to happen to me? I tried opening a layout file from another project in the same workspace, and it has the UI! What happened?

It probably has something to do with the interpreter you used to open the XML file, a voice in my head said. So I tried right-clicking on the XML file, and lo and behold, I found it. I may have accidentally clicked on some file in one of my mad-clicking moments and changed the setting.

So anyway, to bring back the Layout UI Editor, right click on an XML file > Choose Open With > Android Layout Editor.

I would say that everything is handy dandy, but apparently, the engineers at Google decided that we developers need a little less help and removed the very useful up and down arrow keys in the Outline View when editing XML layouts. Why do they hurt us like this?


1 min read

What grammar?

My OC side was alarmed when suddenly, my Problems view in Eclipse was filled with warnings on my XML files. Each of my XML files had a warning with it, and that little yellow exclamation mark on the side:<div>No grammar constraints (DTD or XML schema) detected for the document</div><div>
</div><div>So how do you get rid of it? Go to Window > Preferences > XML > XML Files > Validation then set Indicate when no grammar is specified to Ignore. Click on Apply.</div><div>
</div><div>Clean up your project (Project > Clean). </div><div>
</div><div>If the problem doesn’t go away, you may need to re-validate the XML files. Right click on the file then choose Validate from the popup menu. You can also right click on the folder (such as your res folder) and validate from there.</div>

~1 min read

TextView and MaxLines

I have a TextView (who doesn’t?) and I want to adjust its height automatically, depending on the length of the text it will contain. Should be easy. It was, but it took me a couple of minutes to figure it out.<div>
</div><div>So I want my TextView to be by default one line tall, but be able to expand up to two lines. My initial set up was to set lines=1 and maxLines=2, but it was making the TextView always two lines. Not what I wanted! I went through the documentation again, read each word carefully, and then:
<pre class="brush:xml"><TextView android:id=”@+id/title”
android:text=”This is the text” /></pre>So it turned out that you have to set both minLines and maxLines. TADA!</div>

~1 min read

Missing hierarchyviewer in SDK 7

If you have SDK version 7, you are most probably missing the hierarchyviewer from your /tools folder. To check your SDK version, launch the SDK manager UI from your installation path, usually C:\android-sdk-windows, then click About.
To run hierarchyviewer, you need to manually create the </code>hierarchyviewer.bat</code> file and add it to your <install_path>/tools directory. The text of the batch file can be copied from here.

And so, you can now run hierarchyviewer as you would if the SDK release isn’t effed up. Don’t know how to run it? Follow these steps:
a. In Windows, open up a terminal by running cmd.
b. Navigate to your SDK’s install path. Since I installed mine in C:\, I would have to type in cd C:\android-sdk-windows\tools
c. Type in hierarchyviewer at the prompt.

~1 min read