Search code examples
androidandroid-tabhost

My tabhost is not working


I am working with the tabhost with 2 tabs i am unable nothing in the tab activity,

This is the main class where i implemented the tab host

tabHost = getTabHost();
//*   TabHost Implementations*//*
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();

TabSpec tab1 = tabHost.newTabSpec("FirstTab");
TabSpec tab2 = tabHost.newTabSpec("SecondTab");

// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Friends");
Intent tab1intent = new Intent().setClass(this, Friends_list_add.class);
tab1intent.putExtra("userid", userid);
System.out.println("tabHost------" + tab1intent);
tab1.setContent(tab1intent);

tab2.setIndicator("Invite Friends");
Intent tab2intent = new Intent().setClass(this, Friends_Invite_list.class);
tab2intent.putExtra("response", response);
System.out.println("tab2intent------" + tab2intent);
tab2.setContent(tab2intent);


//** Add the tabs  to the TabHost to display. *//*
tabHost.addTab(tab1);
tabHost.addTab(tab2);

tab = 1;
//set Windows tab as default (zero based)
tabHost.setCurrentTab(tab);

Through intent I am switching to another activity in that activity in onCreate

I have to view a layout as follows textview but it's not displaying anything

no_friends = (TextView) findViewById (R.id.nodata_found_friends);    

I have referred all the website but still didn't get anything, Please help me, Thanks in advance


Solution

  • Try this way to make tabhost i m sure its work.

    public class MainActivity extends TabActivity implements OnTabChangeListener,
    		OnClickListener {
    
    	private ImageView iv_Home;
    	private ImageView iv_Chat;
    	private ImageView iv_Post;
    	private ImageView iv_Alert;
    	private ImageView iv_More;
    	private TabHost tabHost;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main_activity);
    		Casting();
    	}
    
    	private void Casting() {
    		// TODO Auto-generated method stub
    		iv_Home = (ImageView) findViewById(R.id.iv_Home);
    		iv_Chat = (ImageView) findViewById(R.id.iv_Chat);
    		iv_Post = (ImageView) findViewById(R.id.iv_Post);
    		iv_Alert = (ImageView) findViewById(R.id.iv_Alert);
    		iv_More = (ImageView) findViewById(R.id.iv_More);
    
    		iv_Home.setOnClickListener(this);
    		iv_Chat.setOnClickListener(this);
    		iv_Post.setOnClickListener(this);
    		iv_Alert.setOnClickListener(this);
    		iv_More.setOnClickListener(this);
    
    		tabHost = getTabHost();
    		tabHost.setup();
    
    		TabHost.TabSpec tabSpec;
    
    		tabSpec = tabHost.newTabSpec("Tab1").setIndicator("")
    				.setContent(new Intent(this, CategoryActivity.class));
    		tabHost.addTab(tabSpec);
    
    		tabSpec = tabHost.newTabSpec("Tab2").setIndicator("")
    				.setContent(new Intent().setClass(this, ChatActivity.class));
    		tabHost.addTab(tabSpec);
    
    		tabSpec = tabHost
    				.newTabSpec("Tab3")
    				.setIndicator("")
    				.setContent(
    						new Intent().setClass(this, LoginActivity.class));
    		tabHost.addTab(tabSpec);
    
    		tabSpec = tabHost.newTabSpec("Tab4").setIndicator("")
    				.setContent(new Intent().setClass(this, AlertActivity.class));
    		tabHost.addTab(tabSpec);
    
    		tabSpec = tabHost.newTabSpec("Tab5").setIndicator("")
    				.setContent(new Intent().setClass(this, MoreActivity.class));
    		tabHost.addTab(tabSpec);
    
    		tabHost.setOnTabChangedListener(this);
    		tabHost.setCurrentTab(0);
    		setSelectedTabColor();
    
    	}
    
    	@SuppressWarnings("unused")
    	@Override
    	public void onTabChanged(String tabId) {
    		// TODO Auto-generated method stub
    		int pos = this.tabHost.getCurrentTab();
    		// this.mViewPager.setCurrentItem(pos);
    		setSelectedTabColor();
    	}
    
    	// private static void AddTab(MainActivity activity, TabHost tabHost,
    	// TabHost.TabSpec tabSpec) {
    	// tabSpec.setContent(new CategoryTabFactory(activity));
    	// tabHost.addTab(tabSpec);
    	// }
    
    	private void setSelectedTabColor() {
    		// TODO Auto-generated method stub
    		switch (tabHost.getCurrentTab()) {
    		case 0:
    			Global.setActionbar(this, getActionBar(),
    					getString(R.string.app_name));
    			iv_Home.setImageResource(R.drawable.btn_home_hover);
    			iv_Chat.setImageResource(R.drawable.btn_chat);
    			iv_Post.setImageResource(R.drawable.btn_post);
    			iv_Alert.setImageResource(R.drawable.btn_alert);
    			iv_More.setImageResource(R.drawable.btn_more);
    			break;
    		case 1:
    			Global.setActionbar(this, getActionBar(), getString(R.string.chat));
    			iv_Home.setImageResource(R.drawable.btn_home);
    			iv_Chat.setImageResource(R.drawable.btn_chat_hover);
    			iv_Post.setImageResource(R.drawable.btn_post);
    			iv_Alert.setImageResource(R.drawable.btn_alert);
    			iv_More.setImageResource(R.drawable.btn_more);
    			break;
    		case 2:
    			Global.setActionbar(this, getActionBar(),
    					getString(R.string.post_product));
    			iv_Home.setImageResource(R.drawable.btn_home);
    			iv_Chat.setImageResource(R.drawable.btn_chat);
    			iv_Post.setImageResource(R.drawable.btn_post_hover);
    			iv_Alert.setImageResource(R.drawable.btn_alert);
    			iv_More.setImageResource(R.drawable.btn_more);
    			break;
    		case 3:
    			Global.setActionbar(this, getActionBar(), getString(R.string.alert));
    			iv_Home.setImageResource(R.drawable.btn_home);
    			iv_Chat.setImageResource(R.drawable.btn_chat);
    			iv_Post.setImageResource(R.drawable.btn_post);
    			iv_Alert.setImageResource(R.drawable.btn_alert_hover);
    			iv_More.setImageResource(R.drawable.btn_more);
    			break;
    		case 4:
    			Global.setActionbar(this, getActionBar(), getString(R.string.more));
    			iv_Home.setImageResource(R.drawable.btn_home);
    			iv_Chat.setImageResource(R.drawable.btn_chat);
    			iv_Post.setImageResource(R.drawable.btn_post);
    			iv_Alert.setImageResource(R.drawable.btn_alert);
    			iv_More.setImageResource(R.drawable.btn_more_hover);
    			break;
    		default:
    			break;
    		}
    	}
    
    	@Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		if (v == iv_Home) {
    			tabHost.setCurrentTab(0);
    		} else if (v == iv_Chat) {
    			tabHost.setCurrentTab(1);
    		} else if (v == iv_Post) {
    			tabHost.setCurrentTab(2);
    		} else if (v == iv_Alert) {
    			tabHost.setCurrentTab(3);
    		} else if (v == iv_More) {
    			tabHost.setCurrentTab(4);
    		}
    		setSelectedTabColor();
    	}
    }