Navigation drawer contains 4 items and each item contains 4 actionbar tabs ,while switching between menu items means if i click on menu item 2 then number of actionbar items changing to 8 and if i click on menu item3 then number of actionbar tabs changing to 12 ,how o stop this repeation of tabs.here is my code
public class TopicsFragment extends Fragment {
public TopicsFragment() {
}
// Declare Tab Variable
ActionBar.Tab AllTopics, NewContent, StaffPicks, Popular, Recommended;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
Fragment fragmentTab3 = new FragmentTab3();
Fragment fragmentTab4 = new FragmentTab2();
Fragment fragmentTab5 = new FragmentTab1();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup tabs,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_topics, tabs, false);
ActionBar actionBar = ((ActionBarActivity) getActivity())
.getSupportActionBar();
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
AllTopics = actionBar.newTab().setIcon(R.drawable.tab1);
NewContent = actionBar.newTab().setIcon(R.drawable.tab1);
StaffPicks = actionBar.newTab().setIcon(R.drawable.tab1);
Popular = actionBar.newTab().setIcon(R.drawable.tab1);
Recommended = actionBar.newTab().setIcon(R.drawable.tab1);
// Set Tab Listeners
AllTopics.setTabListener(new TabListener(fragmentTab1));
NewContent.setTabListener(new TabListener(fragmentTab2));
StaffPicks.setTabListener(new TabListener(fragmentTab3));
Popular.setTabListener(new TabListener(fragmentTab2));
Recommended.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(AllTopics);
actionBar.addTab(NewContent);
actionBar.addTab(StaffPicks);
actionBar.addTab(Popular);
actionBar.addTab(Recommended);
return rootView;
}
}
That problem is that you are adding tabs constantly when you go back in your TopicsFragment which will call onCreateView again and executing the adding of tabs to the actionbar thus adding another set of tabs to the current tabs.
solution:
you need to check first if the number of tabs is zero in your actionbar.
sample:
actionBar.removeAllTabs();
if(actionBar.getTabCount() == 0)
{
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
AllTopics = actionBar.newTab().setIcon(R.drawable.tab1);
NewContent = actionBar.newTab().setIcon(R.drawable.tab1);
StaffPicks = actionBar.newTab().setIcon(R.drawable.tab1);
Popular = actionBar.newTab().setIcon(R.drawable.tab1);
Recommended = actionBar.newTab().setIcon(R.drawable.tab1);
// Set Tab Listeners
AllTopics.setTabListener(new TabListener(fragmentTab1));
NewContent.setTabListener(new TabListener(fragmentTab2));
StaffPicks.setTabListener(new TabListener(fragmentTab3));
Popular.setTabListener(new TabListener(fragmentTab2));
Recommended.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(AllTopics);
actionBar.addTab(NewContent);
actionBar.addTab(StaffPicks);
actionBar.addTab(Popular);
actionBar.addTab(Recommended);
}
Related
I have create the sample activity using frame layout and fragment with tabs. However, when I switch to other activity/fragment and than comes back to same activity/fragment, it always create duplicate entry or view for tabs. for example, I have tab1 and tab2, when I view activity for first time it display two tabs, but when i switch to other activity and comes back to tabs activity it display four tabs, 'tab1, tab2, tab1, tab2'.
This is my code
public View onCreateView(LayoutInflater Inflater, ViewGroup Container,Bundle savedInstanceState) {
if(savedInstanceState==null) {
rootView = Inflater.inflate(R.layout.loanapplicationview, Container, false);
actionBar = getActivity().getActionBar();
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(true);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(true);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
}
return rootView;
}
}
You should check if the tabs exist before adding them.
if (actionBar.getTabCount() == 0) {
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
}
ActionBar().removeAllTabs() will remove all the tabs attached to your ActionBar.
So before addinf new tabs clear the previous ones with this method
actionBar.removeAllTabs();
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
I have a FragmentActivity and some fragment in my app.
i need to have Swip ActionBar-Tabs Just inside one fragment , but when i use ActionBar and ViewPager in my project to make Swip ActionBar Tab , make ActionBar in all of fragment No just in one,
hove i can make ActionBar just inside one fragment that not shown in other fragment?
actionabar = getActionBar();
actionabar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewpager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
actionabar.setSelectedNavigationItem(position);
}
};
/** Setting the pageChange listner to the viewPager */
viewpager.setOnPageChangeListener(pageChangeListener);
/** Creating an instance of FragmentPagerAdapter */
MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);
viewpager.setAdapter(fragmentPagerAdapter);
actionabar.setDisplayShowTitleEnabled(true);
Tab tab = actionabar.newTab().setText("My Friends").setTabListener(tabListener);
actionabar.addTab(tab);
tab = actionabar.newTab().setText("Android Version").setTabListener(tabListener);
actionabar.addTab(tab);
tab = actionabar.newTab().setText("Android Phones").setTabListener(tabListener);
actionabar.addTab(tab);
Use TabHost (http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html) instead of ActionBar navigation mode.
I have a class that is SherlockFragmentActivity and inside it two SherlockFragment.
When I enter the class it automatically puts me inside the first tab (Tab1 "FragmentTab1"), and I can go to the other tab (Tab2 "FrgmentTab2") by clicking on the tab up. How can I disable a tab?
I want to disable Tab2(bbb) from Tab1(aaa).
my main class code:
public class MainClass extends SherlockFragmentActivity {
ActionBar.Tab Tab1,Tab2;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mm);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar actionBar = getSupportActionBar();
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("aaa");
Tab2 = actionBar.newTab().setText("bbb");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
}
My tabs codes (both classes have same clean-empty code right now)
public class FragmentTab1 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab2, container, false);
return rootView;
}
}
For further explanation please ask.
For removing tab from tabbar, you can use
getActivity().getSupportActionBar().removeTabAt(index)
If you want to disable touch events on that particular tab, you need to create custom view for this Tab a disable focus on that view.
EDIT
Sory, forgot to mention. If you are using API version>=11, you can use
getActivity().getActionBar()
If you are using support lib for compatability, e.g. ActionBarSherlock or appCompat you need to cast it first, in your case
((SherlockFragmentActivity) getActivity()).getSupportActionBar()
![When Tap on Tab 1][1]
![When Tap on Tab 2][2]
when I select action bar tab the title action bar above should be changed, but I can set only one title action bar now. I have tired those code below. But i could not find any solution. can anybody help me???
package com.ActionBar.Tab;
public class MainActivity extends Activity {
// Declare Tab variables
ActionBar.Tab tab1, tab2, tab3;
TextView tab1TV, tab2TV, tab3TV;
Fragment Fragment1 = new Fragment1();
Fragment Fragment2 = new Fragent2();
Fragment Fragment3 = new Fragment3();
View view1,view2,view3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__main);
RelativeLayout custom = new RelativeLayout(getApplicationContext());
ActionBar.LayoutParams params = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
custom.setLayoutParams(params);
custom.setGravity(Gravity.CENTER);
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tab1TV = (TextView) findViewById(R.id.tab1Tv);
tab2TV = (TextView) findViewById(R.id.tab2Tv);
tab3TV = (TextView) findViewById(R.id.tab3Tv);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// set Tab name
tab1 = actionBar.newTab().setCustomView(tab1TV);
tab2 = actionBar.newTab().setCustomView(tab2TV);
tab3 = actionBar.newTab().setCustomView(tab3TV);
// set tab listener
tab1.setTabListener(new TabListener(tab1Fragment));
tab2.setTabListener(new TabListener(tab2Fragment));
tab3.setTabListener(new TabListener(tab3Fragment));
// Add tabsto the action bar
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
int a = R.layout.layout__tab1_action_bar;
view1 = inflater.inflate(a,null);
int b=R.layout.layout_tab2_action_bar;
view2 = inflater.inflate(b,null);
int c= R.layout.layout_tab3_action_bar;
view3 = inflater.inflate(c,null);
actionBar.setCustomView(view1);
/*Here the title action bar will change with view1,view2,view3
with tab selection but I can only set one action bar*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have solved this problem. I have used the layout of action bar title into the fragments onResume() method.
I have 4 tabs in the ActionBar (tab 0, tab 1, tab 2, tab 3). Each tab has its own fragment.
At some point in the application life I need to disable tabs 1,2 and 3.
Since there is no method to disable tabs in android, I use this code on onTabSelected():
switch( tab.getPosition() ) {
case 0:
fragment = new MyFragment();
fragmentTransaction.add(R.id.fragmentContent, fragment, "0");
break;
case 1:
case 2:
case 3:
final ActionBar actionBar = getActionBar();
actionBar.selectTab(actionBar.getTabAt(0));
break;
}
this to force the first tab to be always selected.
When tabs 1,2 or 3 are selected the fragment for tab 0 is selected alright, I see its layout displayed, but the lightblue line underlining the tab is not under tab 0.
Try changing
case 3:
final ActionBar actionBar = getActionBar();
actionBar.selectTab(actionBar.getTabAt(0));
break;
for this
case 3:
final ActionBar actionBar = getActionBar();
actionBar.setSelectedNavigationItem(0);
break;