ANDROID TAB
From google material design documentation
Tabs make it easy to explore and switch between different views or functional aspects of an app or to browse categorized data sets.
HOW TO ADD?
I. In your build.gradle add latest design and appcompat libraries.
dependencies { compile 'com.android.support:appcompat-v7:X.X.X' compile 'com.android.support:design:X.X.X' compile 'com.android.support:support-v13:X.X.X' // where X.X.X version // if you want to support android sdk < 13 // you need to add support library v4 instead of v13 }
II. Make your activity extend android.support.v7.app.AppCompatActivity.
public class MainActivity extends AppCompatActivity { ... }
III. Declare TabLayout and ViewPager in your layout.xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary"/> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
IV. Setup your TabLayout with ViewPager.
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); //set adapter to your ViewPager viewPager.setAdapter(new TabPagerAdapter(getFragmentManager())); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout); tabLayout.setupWithViewPager(viewPager);
V. Override getPageTitle method in your ViewPager's adapter to return tab title.
@Override public CharSequence getPageTitle(int position) { switch (position) { case ITEM_ONE: return "Item One"; ... } }
Comments
Post a Comment