1

I have a tab bar that shows 5 tabs. I want the tab bar to appear on every activity in my app. The only activity's the tab bar appears on is the 5 activities in the tabs. I am using TabActivity, which I know is deprecated, but is there any way I can make it work everywhere except those 5 activities? I need a solution rather quickly and that won't really affect my other code that much.

Here is the Tab Bar Activity

package com.appname;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;

import com.readystatesoftware.viewbadger.BadgeView;

@SuppressWarnings("deprecation")
public class TabController extends TabActivity {  
   /** Called when the activity is first created. */ 
   TabHost tabHost; 
   SharedPreferences prefs;
   public static BadgeView badgeTab;
       @Override  
       public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);    

            tabHost = getTabHost();
            TabHost.TabSpec spec;  
            Intent intent;              

        intent = new Intent().setClass(this, Item1.class);  
        spec = tabHost.newTabSpec("Item 1").setIndicator("Item 1",  getResources().getDrawable(R.drawable.item1))  
                  .setContent(intent); 
        tabHost.addTab(spec);               

        intent = new Intent().setClass(this, Item2.class);  
        spec = tabHost.newTabSpec("Item 2").setIndicator("Item 2", getResources().getDrawable(R.drawable.item2))  
                  .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Item3.class);  
        spec = tabHost.newTabSpec("Item 3").setIndicator("Item 3", getResources().getDrawable(R.drawable.item3))  
                  .setContent(intent);     
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Item4.class);  
        spec = tabHost.newTabSpec("Item 4").setIndicator("Item 4", getResources().getDrawable(R.drawable.item4))  
                  .setContent(intent);              
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Item5.class);  
        spec = tabHost.newTabSpec("Item 5").setIndicator("Item 5", getResources().getDrawable(R.drawable.item5))  
                  .setContent(intent);      
        tabHost.addTab(spec);                  

        //Set background images
       for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){                      

        tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabbackground); //unselected
       }
       tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.selectedtab); // selected

        tabHost.setOnTabChangedListener(new OnTabChangeListener(){
        public void onTabChanged(String tabId) {
            for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            {
                tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabbackground); //unselected
            }
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.selectedtab); // selected
        }
    });

    TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);               
    badgeTab = new BadgeView(this, tabs, 2);
    badgeTab.setBadgePosition(2);
    if(NotificationService.messageCount <= 0){
        badgeTab.hide();
    }
    else{                                                                                                               
        badgeTab.setText(String.valueOf(NotificationService.messageCount));
        badgeTab.show();
    }

    Intent tabIntent = this.getIntent();
    if (tabIntent != null && tabIntent.getExtras() != null) {
        if(tabIntent.getExtras().containsKey("ID_KEY")){
            int id_Key = this.getIntent().getExtras().getInt("ID_KEY");

            if (id_Key > 0) {
                tabHost.setCurrentTab(2);
            }
        }
    }           

}

}

BigT
  • 1,413
  • 5
  • 24
  • 52
  • you can make a custom linear layout with 5 five button in it and control the click events .i can post the code if you want – Harsh Dev Chandel Sep 26 '12 at 17:33
  • If you can post the code that would be great. But wouldn't that mean the buttons wouldn't really look like a tab bar? – BigT Sep 26 '12 at 17:44

0 Answers0