I want to make 2 tabs both contain fragments and have swipe functionality. I have got everything working however the tabs are not styled.
I want to use the holo light theme and have defined this in my manifest like so...
<application android:icon="#drawable/icon" android:label="#string/app_name" android:theme="#android:style/Theme.Holo.Light">
What is the problem?
Take a look at this example : https://developer.android.com/training/implementing-navigation/lateral.html
As for customizing you can use the style generator download the file and copy it into your project its an easy way to create the basics.
http://jgilfelt.github.io/android-actionbarstylegenerator/
#Override
public void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();
...
// Specify that tabs should be displayed in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
}
Related
I currently have Tab Based app and the tabs are on bottom. Is there any way to put them up?
Add Tabs To Action Bar
To create tabs using ActionBar, you need to enable
NAVIGATION_MODE_TABS, then create several instances of ActionBar.Tab
and supply an implementation of the ActionBar.TabListener interface
for each one. For example, in your activity's onCreate() method, you
can use code similar to this:
#Override
public void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();
// Specify that tabs should be displayed in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(actionBar.newTab().setText("Tab " + (i + 1)).setTabListener(tabListener));
}
}
How you handle the ActionBar.TabListener callbacks to change tabs
depends on how you've constructed your content. But if you're using
fragments for each tab with ViewPager as shown above, the following
section shows how to switch between pages when the user selects a tab
and also update the selected tab when the user swipes between pages.
From Docs.
Im trying to Combine drawer navigation and tab navigation together. I have successfully combined the 2. When i launched the app for first time, everything works fine. However, after I click on the drawer item and back to the tabs fragments, there are too many tabs added. I tried to limit the number and it did not work. Also, when I open the other fragments in the drawer item, the tabs still remains.
here's some pictures to show
when I first open the app
after I open the drawer
When I back to the first fragment again
Here's my code for the tabs fragment's on createview
private ActionBar actionBar;
private String[] tabs={ "1", "2", "3","4" };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_home, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getChildFragmentManager());
mViewPager = (ViewPager) v.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
getActivity().getActionBar().setSelectedNavigationItem(position);
}
});
actionBar=getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(tabListener));
}
return v;
}
I think its the
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(tabListener));
part that makes this happened , can anyone help me how to fix it?
You must call actionBar.removeAllTabs() before adding tabs...because you didn't call actionBar.removeAllTabs() they cummulated (4+4)...
P.S. Please don't use actionbar and navigation modes anymore..Action bar navigation modes are deprecated in Android L
Replace actionBar with toolbar, and replace tabs with the PagerTabStrip or something else...
The onCreateView() method will be called when a frament is attached to an Activity and its view must be created, then the code to add tabs to ActionBar will be called more than once, and this cause your problem.
Since ActionBar is belongs to your Activity, not your Fragment, you'd better add tabs to it in your Activity, move the following code to your Activity should help:
private ActionBar actionBar;
private String[] tabs={ "1", "2", "3","4" };
actionBar=getActivity().getActionBar(); // remove getActivity().
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
// This line need to be changed, you can make a public method in
// your fragment which set the current item of ViewPager, and
// call the method in the Activity.
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(tabListener));
}
My Navigation Drawer works just fine!!
But i want to modify it such as to display drawer below viewpager tabs.
I have used Navigation Drawer by adding it into my activity and then accessed it but this time the scenario is different and want to display drawer below tabs.
Can i add Navigation Drawer in fragment layout and access it from fragment class or if its not possible / recommended then how can i achieve the same.
Please refer attached screenshot for problem understanding.
Thanks in advance.
You have two way to achieve this.
1. Method One, the sustainable
Use support.v7.Toolbar in your Layout and placing correctly your NavigationDrawer will feet your need. Moreover, it's more durable.
2. Method two: the old one
Use ActionBar Tabs see http://developer.android.com/training/implementing-navigation/lateral.html#tabs
The main part is
final ActionBar actionBar = getActionBar();
...
// Specify that tabs should be displayed in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
I have into an obstacle when doing my Android application. My issue is that I cannot create an onTabListener for my tabs. I have three tabs with different names. The problem is that they all have the same indicator. In my code I ran this:
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
int n = getActionBar().getTabCount();
Toast.makeText(getApplicationContext(), "You have selected: " + n, Toast.LENGTH_LONG).show();
}
This makes a toast saying the value of the tab (for example: 0, 1, 2, 3), but all of the tabs have the same indicator. I cannot make an onTabListener if I have all my tabs have the same indicator. Here is my full code:
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
int n = getActionBar().getTabCount();
Toast.makeText(getApplicationContext(), "You have selected: " + n, Toast.LENGTH_LONG).show();
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText(tabTitle[i])
.setTabListener(tabListener));
}
What is the issue? Is there any way I can create an onTabListener using the tab's title? Any help to this problem is greatly appreciated.
This code is doing exactly what you are telling it to, by setting
int n = getActionBar().getTabCount();
You are only ever going to print the current tab count. I'm not sure why it has been printing different values for you, perhaps during those tests you tried using different tab counts. Either way, replace the line
int n = getActionBar().getTabCount();
With
String n = tab.getText().toString();
That will change n to the name of the selected tab, so it gets properly appended to your toast message on the following line.
I was running the demo from Actionbersherlock samples and noticed the tabs were always inside the actionbar as in the picture. How do I fix this?
thnx.
public class TabNavigation extends SherlockActivity implements ActionBar.TabListener {
private TextView mSelected;
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_navigation);
mSelected = (TextView)findViewById(R.id.text);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
mSelected.setText("Selected: " + tab.getText());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}
}
Code From here:
https://github.com/JakeWharton/ActionBarSherlock/blob/master/actionbarsherlock-samples/demos/src/com/actionbarsherlock/sample/demos/TabNavigation.java
Per the Action Bar Tabs guide:
the system adapts the action bar tabs for different screen sizes—placing them in the main action bar when the screen is sufficiently wide, or in a separate bar (known as the "stacked action bar") when the screen is too narrow
As ActionBarSherlock mimics the platform behavior, tabs will appear in the Action Bar if there is enough space. You cannot force the stacked action bar pattern as per other answers.
actionbarsherlock has different modes use the correct one
I use this:
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
and the tabs are below the bar.
the different modes should be listed in the samples and documentation