Update indicator of tab after creation.

Quickly share your Android Code Snippets here...

Update indicator of tab after creation.

Postby RickB » Sat Jan 01, 2011 2:07 pm

When a user updates his/her Locale while running your application, all new
resources will be based upon the new Locale, but ones in use will not,
for example the indicators of your TabHost. Having a TabHost with a
different language as for example the new content in the tab’s activity
looks bad.

Since the Android SDK doesn’t provide a way to change the indicator (title) of a tab
after creation, this is what I came up with myself:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /**
  2.          * Updates the indicator (title) of a tab.
  3.          *
  4.          * @param index The index of the tab in your tabhost.
  5.          * @param indicator The new indicator (title) for the tab.
  6.          * @return Indicator updated with success.
  7.          */
  8.         public boolean setTabIndicator(int index, String indicator) {
  9.                 // The TabWidget of your TabHost.
  10.                 TabWidget widget = this.getTabHost().getTabWidget();
  11.  
  12.                 // If the index is out of our widget's bounds, return false
  13.                 // to indicate no tabs were updated.
  14.                 if (index < 0 || index >= widget.getTabCount())
  15.                         return false;
  16.  
  17.                 // A tab in your widget is a RelativeLayout.
  18.                 RelativeLayout tabView = (RelativeLayout) widget.getChildTabViewAt(index);
  19.  
  20.                 // The RelativeLayout has 2 views. child-0 is an ImageView, which
  21.                 // represents the tab's icon. child-1 is a TextView, which represents
  22.                 // your tab's indicator (title).
  23.                 TextView tabTitle = (TextView) tabView.getChildAt(1);
  24.  
  25.                 // Set the text of the TextView.
  26.                 tabTitle.setText(indicator);
  27.                 return true;
  28.         }
  29.  
  30.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
RickB
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Aug 27, 2010 4:54 pm
Location: Netherlands

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests