The best way to implement a tab-based layout is by using the TabLayout
of design library
.
Unfortunately it requires you to have the trio in your dependency group:
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:design:23.0.0'
But nowadays, if you want to make an app that has a good compatibility without much effort and that aggregates some elements of Material Design, there is no escape from it.
The basics to implement is:
Basic layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" />
</LinearLayout>
Setup in your Activity
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(...);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
That’s the basics. When you start combining the AppBarLayout
+ CoordinatorLayout
+ Toolbar
+ some component with NestedScroll
you can have many variations that get very good.
A good source to see the possibilities: https://medium.com/ribot-labs/exploring-the-new-android-design-support-library-b7cda56d2c32 and http://android-developers.blogspot.com.br/2015/05/android-design-support-library.html
Source: https://guides.codepath.com/android/Google-Play-Style-Tabs-using-TabLayout
In place of
ActionBarActivity
useAppCompatActivity
– Skywalker