How to Use Tabhost.Ontabchangelistener in Android

How to use TabHost.OnTabChangeListener in android?

why it would be my pleasure to help you good sir:

myTabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
if(TAB_1_TAG.equals(tabId)) {
//destroy earth
}
if(TAB_2_TAG.equals(tabId)) {
//destroy mars
}
}});

Where TAB_1_TAG is the tag provided to the newTabSpec method when creating the tab.

How to use OnTabChangeListener?

You should implement OnTabChangeListener to the TabActivity class rather than the contents of the Tab.

In your TabActivity implement OnTabChangeListener

then set the listener for the TabHost mTabHost.setOnTabChangedListener(this);

@Override
public void onTabChanged(String tabId) {
Log.i("selected tab ", tabId);

}

UPDATE

public class HelloTabWidget extends TabActivity implements OnTabChangeListener{`

private TabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
mTabHost = getTabHost();

intent = new Intent().setClass(this, BarActivity.class);
spec = tabHost.newTabSpec("Name").setIndicator("Name",res.getDrawable(R.drawable.ic_tab_name)).setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, CityActivity.class);
spec = tabHost.newTabSpec("city").setIndicator("City",res.getDrawable(R.drawable.ic_tab_city)).setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, MapsActivity.class);
spec = tabHost.newTabSpec("Map").setIndicator("Map",res.getDrawable(R.drawable.ic_tab_map)).setContent(intent);
tabHost.addTab(spec);

tabHost.setCurrentTab(2);
mTabHost.setOnTabChangedListener(this);
}

public void onTabChanged(String tabId) {
Toast.makeText(getApplicationContext(), "Selected Tab "+tabId, Toast.LENGTH_LONG).show();
Log.i("selected tab index", "Current index - "+ mTabHost.getCurrentTab());
}}

OnTabChangeListener Null Pointer

You should have gone with the StackOverflow example: How do I change the background of an Android tab widget? :-)

@Override  
public void onTabChanged(String tabId) {
setTabColor(getTabHost());
}

private void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
}
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}

The most likely reason for your NullPointer is your TabHost, check that this is being instantiated, I assume you do it within onCreate.

If you are extending TabActivity you can just call getTabHost(); from your Activity context.

I cannot change the fragment by clicking on tab using TabHost in android

After a long struggle found answer.......
want to add view pager and tab to separate list with fragment.... answer is
separate class

  public class TabInformation 
{
public String tag;
private Class<?> clss;
private Bundle args;
private Fragment fragment;

TabInformation(String tag, Class<?> clazz, Bundle args)
{
this.tag = tag;
this.clss = clazz;
this.args = args;
}

}

Tab Initialization in MainActivity

 private void TabInitialize(Bundle args) 
{
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInformation tabInfo = null;
MainActivity.AddTab(this,mTabHost, mTabHost.newTabSpec("Tab1").setIndicator("Andorid"), ( tabInfo = new TabInformation("Tab1", AndroidFragment.class, args)));
mTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, mTabHost, mTabHost.newTabSpec("Tab2").setIndicator("Apple"), ( tabInfo = new TabInformation("Tab2", AppleFragment.class, args)));
mTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, mTabHost, mTabHost.newTabSpec("Tab3").setIndicator("Microsoft"), ( tabInfo = new TabInformation("Tab3", MicrosoftFragment.class, args)));
mTabInfo.put(tabInfo.tag, tabInfo);
setSelectedTabColor();
}

View Pager Initialization in MainActivity

 private void ViewPagerInitialize()
{
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, AndroidFragment.class.getName()));
fragments.add(Fragment.instantiate(this, AppleFragment.class.getName()));
fragments.add(Fragment.instantiate(this, MicrosoftFragment.class.getName()));
mPageradapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
mViewPager = (ViewPager)super.findViewById(R.id.pager);
mViewPager.setAdapter(mPageradapter);
mViewPager.setOnPageChangeListener(this);
}

private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInformation tabInfo)
{
tabSpec.setContent(new FragmentControl(activity));
tabHost.addTab(tabSpec);
}

Android - TabHost / TabWidget

As you mentioned you should extend from ActivityGroup , but if you don't want a deprecated Class so you can Use Fragments and FragmentManager : Fragments



Related Topics



Leave a reply



Submit