Reputation: 1205
I build app that provide custom add and remove tabs from TABFragment. now i want to give id of every tab which i created custom. and get the id of where i am exactly. I also used tab.getPosition
but it give me where am i exactly. For example i created five page. now when i am 4th tab and then i want to 5th tab id 5th page progress under background. Please give me best suggestion and any regards.Thank you
my Add button code.
btNewtab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
actionBar.addTab(actionBar.newTab().setText("New Tabs")
.setTabListener(HomeActivity.this)); //Adding new tab
COUNT_TAB+=1; //Add tab for counting
mAdapter.notifyDataSetChanged();
}
});
Upvotes: 0
Views: 893
Reputation: 1340
You can identify the individual tabs by Tag easily. just set the tag for each tab and perform action on your basis. i.e
COUNT_TAB+=1;//put this line before so that you can use it as tag
actionBar.addTab(actionBar.newTab().setText("New Tabs")
.setTabListener(HomeActivity.this).setTag(COUNT_TAB));
//If you want to set the selected tab initially different from default then set true or false to indicate which tab should be selected. you can change the position of the added tab also by setting the position. Below is the code i.e.
actionBar.addTab(actionBar.newTab().setText("TAB1").setTag("tab1")
.setTabListener(this),0,false);
actionBar.addTab(actionBar.newTab().setText("TAB2").setTag("tab2")
.setTabListener(this),1,true);
actionBar.addTab(actionBar.newTab().setText("TAB3").setTag("tab3")
.setTabListener(this),2,false);
//To handle the click event of the tabs you have to override the onTabSelected() method i have put some sample code hope it will help you.
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
if (tab.getTag().equals("tab1")) {
// When the given tab is selected, show the tab contents in the
// container view.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt("myvalues", 1);
fragment.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment).commit();
} else if (tab.getTag().equals("tab2")) {
Toast.makeText(this, "two clicked", Toast.LENGTH_LONG).show();
} else if (tab.getTag().equals("tab3")) {
Toast.makeText(this, "three clicked", Toast.LENGTH_LONG).show();
}
}
Upvotes: 1