Change Tab Icon

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Change Tab Icon

Postby lnediger » Sat Aug 02, 2008 12:32 am

I have an activity which has a set of tabs on it. The tabs all have icons set when the tabs are created. How would I go about changing the icons on pre-existing tabs?
lnediger
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Aug 02, 2008 12:30 am

Top

Postby michael_g_hu » Thu Aug 07, 2008 9:27 am

when you add tab ,just use
addTab(CharSequence label, int resId, int imgId)
the last one is image for tab
michael_g_hu
Developer
Developer
 
Posts: 35
Joined: Fri Mar 21, 2008 9:46 am

Change Tab Icon and Text Dynamically

Postby artjin » Wed Oct 22, 2008 7:01 pm

Hi all!
Great forum with plenty of very useful info.
I am using Tabs that are constructed by Intent, and I tried to change tab icon and text dynamically, but could not find any code examples on how to do that.
So I am using some really clumsy way, and it would be nice to know if there is a better way of doing the same thing.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class MyTabs extends TabActivity {
  3.  
  4.        
  5.  
  6.         public static final String TAG_TAB1 = "TAB1";
  7.  
  8.         public static final String TAG_TAB2 = "TAB2";
  9.  
  10.        
  11.  
  12.         public static final int ID_TAB1 = 0;
  13.  
  14.         public static final int ID_TAB2 = 1;
  15.  
  16.        
  17.  
  18.        
  19.  
  20.         private TabHost tabHost;
  21.  
  22.  
  23.  
  24.     @Override
  25.  
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.  
  28.         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  29.  
  30.         super.onCreate(savedInstanceState);
  31.  
  32.        
  33.  
  34.         tabHost = getTabHost();
  35.  
  36.         tabHost.addTab(tabHost.newTabSpec(TAG_TAB1)
  37.  
  38.                 .setIndicator(TAG_TAB1, getResources().getDrawable(R.drawable.graphictabred))
  39.  
  40.                 .setContent(new Intent(this, MyActivity_1.class))
  41.  
  42.         );
  43.  
  44.         setText(ID_TAB1, null, new Float(10.0));
  45.  
  46.  
  47.  
  48.         tabHost.addTab(tabHost.newTabSpec(TAG_TAB2)
  49.  
  50.                 .setIndicator(TAG_TAB2, getResources().getDrawable(R.drawable.graphictabwhite))
  51.  
  52.                 .setContent(new Intent(this, MyActivity_2.class))
  53.  
  54.         );
  55.  
  56.         setText(ID_TAB2, null, new Float(10.0));
  57.  
  58.        
  59.  
  60.         tabHost.setOnTabChangedListener(new OnTabChangeListener(){
  61.  
  62.                         public void onTabChanged(String tabId) {
  63.  
  64.                                 if(tabId.equals(TAG_TAB2)){
  65.  
  66.                                         setIcon(tabHost.getCurrentTab(), R.drawable.graphictabred);
  67.  
  68.                                         setText(tabHost.getCurrentTab(), tabId + " is selected", new Float(10.0));
  69.  
  70.                                         setIcon(ID_TAB1, R.drawable.graphictabwhite);
  71.  
  72.                                         setText(ID_TAB1, TAG_TAB1, new Float(10.0));
  73.  
  74.                                 }
  75.  
  76.                                 else if(tabId.equals(TAG_TAB1)){
  77.  
  78.                                         setIcon(tabHost.getCurrentTab(), R.drawable.graphictabred);
  79.  
  80.                                         setText(tabHost.getCurrentTab(), tabId + " is selected", new Float(10.0));
  81.  
  82.                                         setIcon(ID_TAB2, R.drawable.graphictabwhite);
  83.  
  84.                                         setText(ID_TAB2, TAG_TAB2, new Float(10.0));
  85.  
  86.                                 }
  87.  
  88.                         }
  89.  
  90.         });
  91.  
  92.     }
  93.  
  94.    
  95.  
  96.     private void setIcon(int whichTab, int resourceId){
  97.  
  98.                 ArrayList <View> views = tabHost.getTabWidget().getChildAt(whichTab).getTouchables();
  99.  
  100.                 RelativeLayout relLayout = (RelativeLayout)views.get(0);
  101.  
  102.                 ImageView iv = (ImageView)relLayout.getChildAt(0);
  103.  
  104.                 iv.setImageDrawable(getResources().getDrawable(resourceId));
  105.  
  106.     }
  107.  
  108.    
  109.  
  110.     private void setText(int whichTab, String text, Float size){
  111.  
  112.                 ArrayList<View> views = tabHost.getTabWidget().getChildAt(whichTab).getTouchables();
  113.  
  114.                 RelativeLayout relLayout = (RelativeLayout)views.get(0);
  115.  
  116.                 TextView tv = (TextView)relLayout.getChildAt(1);
  117.  
  118.                 if(text != null){
  119.  
  120.                         tv.setText(text);
  121.  
  122.                 }
  123.  
  124.                 tv.setTextSize(size);
  125.  
  126.     }
  127.  
  128. }
  129.  
  130.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Thanks :-)
artjin
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 22, 2008 6:18 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 3 guests