Tabs in Actionbarsherlock are always inside the actionbar? - android

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

Related

How to change position of Tab Navigation?

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.

What's the new way to use different fragments for different tabs?

I have been looking far and wide at different solutions for this problem: How do I show different things in different tabs with the ActionBar in Android?
I have found a method: Using fragments as XML then put them as the layout for the different tabs. But most code that is posted is either outdated and deprecated or very complicated.
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_main);
// Set the Action Bar to use tabs for navigation
ActionBar ab = getSupportActionBar();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Add three tabs to the Action Bar for display
ab.addTab(ab.newTab().setText("Tab 1").setTabListener(this));
ab.addTab(ab.newTab().setText("Tab 2").setTabListener(this));
ab.addTab(ab.newTab().setText("Tab 3").setTabListener(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu from menu resource (res/menu/main)
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
// Implemented from ActionBar.TabListener
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// This is called when a tab is selected.
}
// Implemented from ActionBar.TabListener
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// This is called when a previously selected tab is unselected.
}
// Implemented from ActionBar.TabListener
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// This is called when a previously selected tab is selected again.
}
This is a sample from Android providing an example for the ActionBar and its own capabilities. This would be a good start, except it's deprecated. And most either this or something else that's deprecated. So I am wondering, What's the new way to use different fragments for different tabs?
# Try to follow below steps:
Design your activity_main.xml with Toolbar, TabLayout and ViewPager
Design layout XML's your fragments that you want to show for each Tab content.
Use FragmentPagerAdapter to populate Fragments on ViewPager
Set FragmentPagerAdapter to ViewPager using ViewPager.setAdapter(FragmentPagerAdapter)
Use TabLayout.setupWithViewPager(ViewPager) to give ability to ViewPager to work with TabLayout functionalities.
Here is a complete tutorial.
# If you are using AndroidStudio, then you can easily create it using default TabbedActivity template. Follow below Steps:
Open AndroidStudio
File > New > New Project
Give a project name > Choose minimum SDK versions then press Next
Select TabbedActivity then press Next
Choose Navigation Style: "Action Bar Tabs(with ViewPager) > Finish
Hope this will help~
A good example can be found here: https://gist.github.com/talhahasanzia/84e44e831f9c5f9b14835a253f3614dd

How to display Navigation Drawer below Viewpager tabs?

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));
}

Tabs Not Styled

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));
}
}

Tabhost in the action bar of Honeycomb app?

I have an app (for Honeycomb) with a main activity that shows a sort of dashboard, with three buttons and a title. When the user clicks a button they are taken to a screen where they can enter data and do a calculation. I would like to have two approaches to the calculation in this second ('calculator') activity, and would like to implement this through having two tabs in the action bar (only when you are in this calculator activity).
I haven't used a tabhost widget or tabs ever before, so how do I go about having a tab widget in the action bar and changing the rest of the screen (everything but the action bar and system bar) when the other tab is selected?
If someone could point me towards some source code specifically for Honeycomb action bar tabs, that would be great.
Thanks for any help, and have a great day.
See Honycomb Gallery which makes use of action bar tabs.
Tabs in the action bar is a very neat feature. To make this question complete here on SO, I'll provide an example; This code goes in your Activity's onCreate
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// remove the activity title to make space for tabs
actionBar.setDisplayShowTitleEnabled(false);
// instantiate some fragments for the tabs
Fragment fragment1 = new Fragment1();
Fragment fragment2 = new Fragment2();
// add a new tab and set its title text and tab listener
actionBar.addTab(actionBar.newTab().setText(R.string.title_tab1)
.setTabListener(new MyTabListener(fragment1)));
actionBar.addTab(actionBar.newTab().setText(R.string.title_tab2)
.setTabListener(new MyTabListener(fragment2)));
You can put the MyTablListener as an inner class of your activity, It could look something like this;
class MyTabListener implements ActionBar.TabListener {
private Fragment fragment;
public MyTabListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.replace(R.id.activity_new_formula_fragment_content, fragment, null);
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}

Categories

Resources