I am trying to get the Click event when clicking on currently selected tab of my TabActivity.
I tried below code but when i click on one tab the other tabs are not working/clicking properly.
setupTab(new TextView(this), "Map");
setupTab(new TextView(this), "Attacks");
setupTab(new TextView(this), "Profile");
setupTab(new TextView(this), "Headquater");
int numberOfTabs = tabHost.getTabWidget().getChildCount();
for(int t=0; t<numberOfTabs; t++){
getTabWidget().getChildAt(t).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(tabHost.getCurrentTab() == 0){
Toast.makeText(TabContext, ""+"i m in on clickkkkk" ,1500).show();
getTabHost().setCurrentTab(0);
}
if(tabHost.getCurrentTab() == 1){
Toast.makeText(TabContext, ""+"i m in on clickkkkk....$#@$#$" ,1500).show();
getTabHost().setCurrentTab(1);
}
}
});
}
I needed to detect second click only for one tab, so this answer worked for me. The trick is setting the OnClick for the child and not for the TabHost itself.
getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TAG", "Clicked tab : "+getTabHost().getCurrentTabTag());
tabHost.setCurrentTab(0);
}
});
I used theczechsensation solution but with minor modifications because adding a listener to child and not to the tab host itself will cause confusion to tabhost listener to read comments in the code for better classifications
mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// re enforce selecting targeted tab to re apply main listener to default behaviour
mTabHost.setCurrentTab(0);
// display current tab tag in console
Log.i("MainActivity", "Clicked tab : "+mTabHost.getCurrentTabTag());
// identify targeted fragment
Fragment currentFragment = new "FragmentClassName"();
// execute navigation (fragment transaction)
fragmentManager.beginTransaction()
.replace(R.id.content_frame, currentFragment)
.commit();
// re enforce selecting targeted tab to re apply main listener to default behaviour
mTabHost.setCurrentTab(0);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With