I have been learning to use Sliding Tabs using Material Design using this post. I have managed to achieve SlidingTabs below the Toolbar
, like this one:
But now i would like to create ActionBar/ToolBar Fragment Tabs ...
I was able to recreate exactly what you are looking to implement. I am using the this Library for the tabs.
This is the view I have created:
Import Library Through Dependencies or Download Project and Import Manually
compile 'com.jpardogo.materialtabstrip:library:1.0.9'
styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
</style>
</resources>
MainActivity & Adapter
public class MainActivity extends ActionBarActivity {
Toolbar toolbar;
ViewPager viewPager;
ContactPagerAdapter pagerAdapter;
PagerSlidingTabStrip pagerSlidingTabStrip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.mipmap.logo_two);
toolbar.inflateMenu(R.menu.menu_main);
viewPager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new ContactPagerAdapter(this, getSupportFragmentManager());
pagerSlidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
viewPager.setAdapter(pagerAdapter);
pagerSlidingTabStrip.setViewPager(viewPager);
}
public static class ContactPagerAdapter extends FragmentPagerAdapter implements PagerSlidingTabStrip.CustomTabProvider {
private final int[] ICONS = {R.mipmap.ic_launcher, R.mipmap.ic_launcher};
Context mContext;
private Fragment f = null;
public ContactPagerAdapter(Context ctx, FragmentManager fm) {
super(fm);
mContext = ctx;
}
@Override
public int getCount() {
return ICONS.length;
}
@Override
public Fragment getItem(int position) { // Returns Fragment based on position
switch (position) {
case 0:
f = new FragmentPageOne();
break;
case 1:
f = new FragmentPageTwo();
break;
}
return f;
}
@Override
public View getCustomTabView(ViewGroup parent, int position) {
LinearLayout customLayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.custom_tab, parent, false);
ImageView imageView = (ImageView) customLayout.findViewById(R.id.image);
imageView.setImageResource(ICONS[position]);
return customLayout;
}
}
}
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#02a6d8"
android:minHeight="56dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="wrap_content"
android:layout_height="56dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="8dp"
android:background="#02a6d8"
app:pstsDividerColor="#02a6d8"
app:pstsIndicatorColor="#fff"
app:pstsIndicatorHeight="2dp"
app:pstsShouldExpand="false"
app:pstsUnderlineHeight="0dp"/>
</android.support.v7.widget.Toolbar>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"/>
<!-- Shadow below toolbar-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="@+id/toolbar"
android:background="@drawable/toolbar_shadow"/>
</RelativeLayout>
custom_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="30dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="8dp"
android:paddingTop="8dp"/>
</LinearLayout>
Drawrable toolbar_shadow.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:endColor="@android:color/transparent"
android:startColor="@color/semi_transparent"/>
</shape>