Taking a look at the API diff report for the Android "L" preview, I see that all methods related to navigation modes in the ActionBar class (such as setNavigationMode(), addTab(), selectTab(), &c). are now deprecated.
The documentation explains:
Action bar navigation modes are deprecated and not supported by inline
toolbar action bars. Consider using other common navigation patterns
instead.
What is the supposed replacement?
Also, is "inline toolbar action bars" a new concept? I don't think I've heard of it before.
The new Android Design Support Library adds TabLayout, providing a tab implementation that matches the material design guidelines for tabs. A complete walkthrough of how to implement Tabs and ViewPager can be found in this video
Now deprecated: The PagerTabStrip is part of the support library (and has been for some time) and serves as a direct replacement. If you prefer the newer Google Play style tabs, you can use the PagerSlidingTabStrip library or modify either of the Google provided examples SlidingTabsBasic or SlidingTabsColors as explained in this Dev Bytes video.
Now that the Android 5.0 docs are available, we have the official documentation for the Toolbar widget:
A standard toolbar for use within application content.
A Toolbar is a generalization of action bars for use within
application layouts. While an action bar is traditionally part of an
Activity's opaque window decor controlled by the framework, a Toolbar
may be placed at any arbitrary level of nesting within a view
hierarchy.
A Toolbar widget can also be used to replace the action bar:
An application may choose to designate a Toolbar as the action bar for
an Activity using the setActionBar() method.
The deprecation of tabs in the action bar is most probably due to this, since toolbars cannot contain tab themselves.
Also, it's available for previous Android verions via the appcompat library. See this post by Chris Banes for more information. An excerpt:
Android 5.0 introduces a new Toolbar widget. This is a generalization
of the ActionBar pattern but gives you much more control and
flexibility in using it. Toolbar is a view in your hierarchy just like
any other, making it easier to interleave with the rest of your views,
animate, react to scroll events.
It seems like they added a new Class named android.widget.Toolbar that extends ViewGroup. Also they added a new method setActionBar(Toolbar) in Activity. I haven't tested it yet, but it looks like you can wrap all kinds of TabWidgets, Spinners or custom views into a Toolbar and use it as your Actionbar.
The new Toolbar cannot be used for inflating multiple line objects, so it is impossible to add Tabs to it.
If you want to use a Toolbar like a TabWidget you can insert some Tab Objects to it, but only with the old Holo style.
Here there is a custom Library that uses v7 Toolbar like TabWidget with the new Material Design animations, but it uses the same methods from the old ActionBar Tabs, so you can attach your ViewPager to it.
For 'replacement' of deprecated ActionBar, I changed the type of my ActionBar-type variables to PagerTabStrip, as per (old code in comment):
// ActionBar bigActionBar;
PagerTabStrip bigActionBar;
A 'replacement' for ~actionBar's .selectTab(tabindex) was to use my associated ViewPager's .setCurrentItem(int) method, like this (old code in comment):
/*
ActionBar.Tab eventTab = bigActionBar.getTabAt(2);
bigActionBar.selectTab(eventTab);
*/
mViewPager.setCurrentItem(2);
Hope this is helpful.
I had the same problem and this solution suited me quite nicely:
In the layout xml file that contains the viewpager, add the a PagerTabStrip as shown:
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#996633"
android:textColor="#CCCCCC"
android:paddingTop="5dp"
android:paddingBottom="5dp" />
To control page titles, add a switch statement to your ViewPager file:
#Override
public CharSequence getPageTitle(int position)
{
switch (position)
{
case 0:
return "Page 1";
case 1:
return "Page 2";
case 2:
return "Page 3";
}
return null;
}
FragmentTabHost is also an option.
This code is from Android developer's site:
/**
* This demonstrates how you can implement switching between the tabs of a
* TabHost through fragments, using FragmentTabHost.
*/
public class FragmentTabs extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
FragmentStackSupport.CountingFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
LoaderCustomSupport.AppListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
}
}
I found these tutorials helpful while putting together an action bar (now the 'tool bar' - argh) that supports sliding tabs with Material Design:
https://www.youtube.com/watch?v=Fl0xMuo10yA
http://www.exoguru.com/android/material-design/navigation/android-sliding-tabs-with-material-design.html
You sort of have to synthesize these resources to match your particular situation. For example, you may not want to manually create the tabs in the same style that the exoguru.com tutorial did.
Well for me to handle the deprecated navigation toolbar by using toolbar v7 widget appcompat.
setSupportActionBar(toolbar);
getSupportActionBar().setSubtitle("Feed Detail");
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//goToWhere
}
});
I think a suitable replacement for when you have three to five screens of equal importance is the BottomNavigationActivity,this can be used to switch fragments.
You will notice a wizard exists for this in Android Studio, take care however as Android Studio has a tendency to produce overly complex boiler plate code.
A tutorial can be found here:
https://android.jlelse.eu/ultimate-guide-to-bottom-navigation-on-android-75e4efb8105f
Another quality tutorial can be found at Android Hive here:
https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/
Related
Alright, I am trying to understand what I would lose if I use Toolbar as a self-managed ActionBar and not use setSupportActionBar.
AFAIK, all that ActionBar does is, provide placeholders for logo, navigation and menu items and also let Fragments add/customize the menu items.
The same functionality could be achieved using Toolbar.setLogo(), Toolbar.setNavigationIcon(), Toolbar.setNavigationOnClickListener() and Toolbar.inflateMenu() apis. Of course, directing the menu handling logic to Fragments might be lost but I think that is not that big of deal if the Activity knows which Fragment is on top and changes the menu items accordingly.
I am trying to make sure :
If I can achieve every ActionBar capability just by using Toolbar and MenuItems (and not using setSupportActionBar()).
Is it ok to not have an exhaustive knowledge of ActionBar apis. Some of the ActionBar apis are very confusing. Using setHomeAsUp api to show hamburger icon, back icon etc., doesn't feel right. If someone starts learning android today, do they even need to understand the framework's ActionBar apis?
Update : In the article Android Design Support Library under section CoordinatorLayout and the app bar, I learnt that the new paradigm app bar is a replacement for the action bar paradigm. I think action bar will soon be deprecated and we should get used to the new app bar paradigm which covers the new material design user experiences.
Yes you can use achieve similar capabilities of ActionBar in Toolbar.
Mainly the difference lies is Toolbar becomes part of View so we can much more fun playing with them to get Scrolling Techniques
You can use Toolbar separately as View & perform ActionBar alike functionalities. For example, in one of my app I use 2 Toolbar one which is set to setSupportActionBar() while other is just used for some other functionalities.
Conclusion: Well it depends upon your requirements if you want to use Toolbar as self or framework. None the less you can use it as both.
I hope this answers your question well.
To me you are right.
From AppCompatDelegate source code, method setSupportActionBar(),
When set to a non-null value the getSupportActionBar() method will return
an ActionBar object that can be used to control the given toolbar as if it were
a traditional window decor action bar. The toolbar's menu will be populated with the
Activity's options menu and the navigation button will be wired through the standard
android.R.id.home menu select action.
So these are most, if not all, the benefits you will have. As you said, it is easy to implement navigation and menu inflating through Toolbar APIs. However, I don't see what you would gain by not calling setSupportActionBar().
YES YES YES
using setSupportToolBar() is the same old Actionbar the only reason ToolBar is ToolBar is for versatility,same as Fragments is to Views, all lies on how you implement stuff, and also the old Actionbar is kinda boring and much restricted as to Toolbar
This question has been asked (for example, here Using ViewPager with Tabs without actionBar), however the answer there doesn't work. There's some links to Swipey but unfortunately the link is broken too.
The example from Android site EffectiveNavigation uses Actionbar to host the tab fragment, so obviously if I set a .NoActionBar theme, then there's no host. Any different way? Thanks.
Update screenshot of what I want to create, at the top, there's no actionbar.
Update 2 this is from the google example, there's an actionbar on top (titled "Effective navigation), which I want to get rid of
Solution of your problem is already given in http://developer.android.com/
To disableAction-bar Icon and Title, you must do two things:
setDisplayShowHomeEnabled(false); // hides action bar icon
setDisplayShowTitleEnabled(false); // hides action bar title
Follow The Steps given in Using split action bar
Write Following Code in OnCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
getActionBar().setDisplayShowHomeEnabled(false); // hides action bar icon
getActionBar().setDisplayShowTitleEnabled(false); // hides action bar title
//rest of your code...
}
After Android updated Actionbar to Toolbar there are many changes in Actionbar Tabs.
Please follow below links to create swipable tabs in Andoid.
Design Structure :
Tabs Design Guidelines
Some of the very useful links are below. Please refer to them.
Download sample zip from below link
http://developer.android.com/samples/SlidingTabsBasic/index.html
Or Refer these links
http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html
http://www.exoguru.com/android/material-design/navigation/android-sliding-tabs-with-material-design.html
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
https://guides.codepath.com/android/Google-Play-Style-Tabs-using-TabLayout
This may help you...
You can:
Use a ViewPager with a PagerTabStrip
Use a ViewPager with the TabPageIndicator class from the ViewPagerIndicator library
Use a ViewPager with other third-party tab indicators (e.g., in the "View Pagers" category at the Android Arsenal)
Use a ViewPager and design your own tabbed indicator
Use a FragmentTabHost and skip the swiping part
Along with
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
Use this as well
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
I am developing Chat Application in Android and I want to add dynamic Chat Tabs dependent on current matched Users like attached in screenshot below :
In Screen shot Chat Tabs are at Top but I want Chat Tabs at bottom. Now I want to develop logic in onCreate method so that
If there are three matched users then create 3 tabs,
If there are four matched users then create 4 tabs,likewise..
I searched a lot for chat Tabs and found ways to create Chat Tabs by using TabHost..But also found that it is deprecated,not sure.. Another way is to setup Chat Tabs in Action Bar..Somewhere found that use ActionBarSherlock. I am very confused about chat tabs that what to use ?
Any help will be appreciated.
#SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
public static TabHost tabHost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tab_main);
// call addtab() how many times you need and pass tag and image resource id
}
private void addTab(String tag, int drawableId) {
tabHost = getTabHost();
TabHost.TabSpec spec = tabHost.newTabSpec(tag);
// tab_indicator layout contains only imageview. this is for fix image size, position
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
tabHost.addTab(spec);
}
}
Now in latest version of android included ActionBarSherlock library. So you can directly add tab using that library in android.
Sample code:
try
{
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// For each of the sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setText(R.string.firsttab).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.second).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.third).setTabListener(this));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
}
catch(Exception e)
{
e.printStackTrace();
}
I have been using ActionBarSherlock for a while now, but I have now migrated to the new Android SDK 18 Action Bar Compat. Your project looks like a straightforward implementation of action bar tabs and I can see no reason why you should start using ActionBarSherlock (or tabHost) at this point.
Action Bar Compat is very similar to Action Bar Sherlock and has the advantage that it is an integral component of the Android V4 support libraries (compatable back to SDK 7). See the "New v7 appcompat library" section in http://developer.android.com/tools/support-library/index.html .
It also has the advantage that it is clearly documented. This guide details how to set it up:
http://developer.android.com/tools/support-library/setup.html
(pay particular attention to the section "Adding libraries with resources")
Having done that, you follow this guide to set up the support Action Bar:
http://developer.android.com/guide/topics/ui/actionbar.html
The section "Adding Navigation Tabs" gives a clear example of a tabListener and how to add the tabs. You will need to make some minor adjustments to this code (for loop / if statements) to decide how many tabs to add. I have done this before and it is straightforward programming.
How can i archive a Tab layout like this in Android ? The Tab Navigation is part of that
Main ActionBar and is not below. PS: Im using ActionBar Sherlock
At the moment is just do:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
But i want a look like this:
Thanks for help,
Kitesurfer
I don't recommend doing this but you could override the ActionBar and ABS styles:
<bool name="abs__action_bar_embed_tabs">false</bool> //for ActionBarSherlock
<bool name="action_bar_embed_tabs">false</bool> //for default ActionBar
Tab will be displayed in actions bar but only if action bar has space. When you are in landscape mode your tabs will move to action bar because in landscape mode action bar has more room.
From this thread. On the topic of overriding the Action Bar tab functionality.
Jake Wharton
I am obligated to tell you this is probably a Bad Idea™ and I don't recommend it. When you fight the platform you almost always end up losing.
I am inclined to agree about fighting the platform. Instead I opted to abandon the stock tab navigation (from both the android platform and ABS library) and instead wrote a layout that looks basically the same as the tabs, and used it as a custom view, applying it using ActionBar.setCustomView(View) and ActionBar.setDisplayShowCustomEnabled(true) methods.
This uses the ActionBar api in the standard way (ie. Not fighting the platform) and, as is pointed out in that thread, the tab view is simply a LinearLayout.
Try this. This implements a method that will let you select whether you want the tabs in a separate row or in the top row, regardless of screen orientation.
For those who are using this code with the native ActionBar, simply omit the if (actionBar instanceof ActionBarWrapper) and the block underneath it.
Bear in mind, though, that this is somewhat of a hack and might break in future Android versions.
I want to create an ActionBar and a tabbed navigation like the google+ app.
I used this example as a starting point and now I have a great actionbar:
http://developer.android.com/resources/samples/ActionBarCompat/index.html
I've also included a ViewPager and a TabHost to have tabs and scrolling left/right Fragments.
What i need is to show the back arrow in version prior to honeycomb.
If I set getActionBar().setDisplayHomeAsUpEnabled(true), the arrow is automatically show in version >= honeycomb.
How can I do that in version prior to honey?
What I also want to have is Tabs like the google+ app.
This is how is my tab bar looks:
removed dead ImageShack link
...and this is what i want:
removed dead ImageShack link
I can't find any example to style tab bars like that one.
I would recommend you the ActionBarSherlock for ActionBar compatibility with Android <3. It is a better implementation that the example offered in the developers page.
Using this package, the back arrow will appear when you add to your activity the following line.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Regarding the Google+ tabs, I have found the widget ViewFlow very useful and looking exactly the same. Moreover, you don't have any backward compatibility issues. Have a look to the CircleFlowIndicator example.
You can use ActionBarSherlock and a ViewPager with ViewPagerIndicator to achieve the desired look. These libraries also work pre ICS.
As miguel.rodelas suggested, use getSupportActionBar().setDisplayHomeAsUpEnabled(true) for the home arrow. In onOptionsItemSelected you can catch it by the id android.R.id.home.
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
//use your custom xml view here
View actionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom_view, null);
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//your logic for click listner
setListenerForActionBarCustomView(actionBarView);
As stated, download the ActionBarSherlock and import it as a library here
https://api.github.com/repos/JakeWharton/ActionBarSherlock/zipball/4.2.0
Instead of using getActionBar use getSupportActionBar
Download the support library
Instead of Activity, extend your class as SherlockActivity
Same with fragments, use SherlockFragment
Then just copy your code, should work perfectly.