How to handle TabHost TabChanged event in Mono Android?

Being new to Android development I struggled with this one for some time and hopefully this post will save some poor soul the agony I went through.

After I added a TabHost widget to my Layout I assumed I could attach an event handler to either the TabSpec component or the TabHost itself and listen to these events in the activity. But this does not work. Googling and trolling stackoverflow threw up old references which no longer were valid for 4.0.1 version of the SDK I am targeting.

However the technique is very simple. 

The TabHost has a method named “SetOnTabChangedListener” which takes in an instance of class implementing the IOnTabChangeListener interface. 

So all you got to do is implement IOnTabChangeListener in your Activity and set the listener on the TabHost like so

TabHost.SetOnTabChangedListener(this)

Your activity class needs to implement only the OnTabChanged method and you are golden. This method is called when any tab is clicked and you may do whatever pleases you in the method. The tabId is passed in as a parameter. This is the same string you used when setting yo individual tabs.

spec = TabHost.NewTabSpec(“home”);

There you have it. Enjoy.

Advertisements

About Ramesh Sringeri

Yet another chaos monkey. View all posts by Ramesh Sringeri

One response to “How to handle TabHost TabChanged event in Mono Android?

  • Thomas

    You asked a question at StackOverflow about referencing fields by number, e.g., Select “1” from MyTable rather than using field names. My solution would be to create a view where the fields are renamed by number. Of course this requires that the fields names come from a table known in advance. Suppose all you have is a csv string of fields names. This could be to create a string of dynamic t-sql that creates the view that renames the fields as numbers.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: