I was using SupportActionBar with tabs and a custom ActionBar theme (created with http://jgilfelt.github.io/android-actionbarstylegenerator/), that show the tabs only when the user expands the search view.
public boolean onMenuItemActionExpand(MenuItem item) {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
return true;
}
}
I migrated from ActionBar to Toolbar. My app really needs to support API 9.
Is there a way to use this code to add the tabs back?:
Toolbar toolbar = (Toolbar) findViewById(R.id.new_actionbar);
setSupportActionBar(toolbar);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
If possible, how can I use my custom theme or style the toolbar?
The documentation says that this is deprecated and suggests using a different type of navigation. But I don't know of any other components in Android that have the same functionality.
Some help?
With the API 21 the method
setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)
is deprecated.You can use a different pattern. For example you can use the same example that you can see in googleio14.
It uses a
SlidingTabLayout
which works with aViewPager
.Here you can find the example (it is in your sdk example)
Here you can find the Google io14 example:
Layout
Java
UPDATE 29/05/2015
With the new Design Support Library now you can use the new TabLayout.
Just add this dependency to your
build.gradle
The code is very simple:
To implement many of the features of material designs you should use it within a CoordinatorLayout and a AppBarLayout.
Something like this:
Although probably a little late to answer this question now, I realised that I wrote an answer on a similar question which covers both using the Design Support Library and prior to Google I/O.
I have included the essential parts below:
Using a
TabLayout
with theToolbar
has become much simpler since the announcement of the Android Design Support Library meaning we no longer need to download custom view classes.From the Android Developers' Blogspot post on the Android Design Support Library:
If you are not using the Design Support library, you will instead need to do the following:
1. Download the
SlidingTabLayout.java
andSlidingTabStrip.java
files from Google's I/O Conference app on GitHub. These would be the views that would be used in the tab layout, so I created a folder with my other Java activities called 'view' and placed them there.2. Edit your layout to include the
SlidingTabLayout
:The line which references the
Toolbar
(<include android:id="@+id/detail_toolbar" layout="@layout/toolbar" />
), is referencing another XML file I had used to make theToolbar
.3. Change the package names in
SlidingTabLayout.java
andSlidingTabStrip.java
corresponding to where they were placed. In my case, I used something similar to:package com.mycompany.myapp.view;
for both of these files. Organise imports and add in any necessary, as noted by the IDE you are using.4. In your
Activity
(which was extendingAppCompatActivity
), setup theToolbar
in theonCreate
method:5. Setup the
ViewPager
andSlidingTabLayout
parts:The colour '
tab_line
' was a colour I had declared incolor.xml
which would be the colour of the tab line indicator. Also note that the variables above were global which I defined previously in this activity:6. Finally, setup the
ViewPagerAdapter
which I had called earlier. This would be responsible for changing the page depending on which tab was selected. I used the following code:As I mentioned above, further details to these solutions are available on another question I answered, about using Sliding Tabs with the Toolbar.