Tab Bar Java

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

package com.example.

tabbar;
import
import
import
import
import
import
import

android.os.Bundle;
android.app.Activity;
android.app.TabActivity;
android.content.Intent;
android.view.Menu;
android.widget.TabHost;
android.widget.TabHost.OnTabChangeListener;

public class Tabbar extends TabActivity implements OnTabChangeListener {


TabHost tabh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbar);
tabh=getTabHost();
//set tab change listener called when tab changed
tabh.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent i;
//tab1
i= new Intent(Tabbar.this, MainActivity.class);
spec=tabh.newTabSpec("food").setIndicator("").setContent(i);
tabh.addTab(spec);
//tab2
i= new Intent(Tabbar.this, Game.class);
spec=tabh.newTabSpec("Game").setIndicator("").setContent(i);
tabh.addTab(spec);
//tab3
i= new Intent(Tabbar.this, Support.class);
spec=tabh.newTabSpec("Support").setIndicator("").setContent(i);
tabh.addTab(spec);
//set images to tab
tabh.getTabWidget().getChildAt(1).setBackgroundResource(R.drawab
le.ic);
tabh.getTabWidget().getChildAt(2).setBackgroundResource(R.drawab
le.icc);
tabh.getTabWidget().getChildAt(0).setBackgroundResource(R.drawab
le.icccc);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.tabbar, menu);
return true;
}
@Override
public void onTabChanged(String arg0) {

// called when tab changed and set image according to current t


ab set its image
for(int i=0; i<tabh.getTabWidget().getChildCount();i++)
{
if(i==0)
tabh.getTabWidget().getChildAt(i).setBackgroundR
esource(R.drawable.icccc);
else if(i==1)
tabh.getTabWidget().getChildAt(i).setBackgroundResource(
R.drawable.ic);
else if(i==2)
tabh.getTabWidget().getChildAt(i).setBackgroundResource(
R.drawable.icc);
}
//current tab highlight
/*
if(tabh.getCurrentTab() ==0)
tabh.getTabWidget().getChildAt(tabh.getCurrentTab()).setBackgrou
ndResource(R.drawable.ic);
else if(tabh.getCurrentTab() ==0)
tabh.getTabWidget().getChildAt(tabh.getCurrentTab()).setBackgrou
ndResource(R.drawable.icc);
else if(tabh.getCurrentTab() ==0)
tabh.getTabWidget().getChildAt(tabh.getCurrentTab()).setBackgrou
ndResource(R.drawable.icccc);
*/}
}

You might also like