Skip to content

Instantly share code, notes, and snippets.

@talhahasanzia
Created June 7, 2016 06:09
Show Gist options
  • Save talhahasanzia/84e44e831f9c5f9b14835a253f3614dd to your computer and use it in GitHub Desktop.
Save talhahasanzia/84e44e831f9c5f9b14835a253f3614dd to your computer and use it in GitHub Desktop.
Sliding Tab Layout with Design Support Library
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.qibladirection.tzia.mpachart.MainActivity">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
android:id="@+id/tabs"
android:background="@color/colorPrimary"
></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tabs"
android:layout_weight="1"
android:id="@+id/pager"
></android.support.v4.view.ViewPager>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout=(TabLayout)findViewById(R.id.tabs);
MyPagerAdapter myPagerAdapter=new MyPagerAdapter(getSupportFragmentManager());
ViewPager viewPager=(ViewPager)findViewById(R.id.pager);
viewPager.setAdapter(myPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
getSupportActionBar().setTitle("Charts");
}
class MyPagerAdapter extends FragmentStatePagerAdapter
{
public MyPagerAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
if(position==0)
return new BarChartFragment();
else
return new PieChart();
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
if(position==0)
return "Bar Chart";
else
return "Pie Chart";
}
}
}
@talhahasanzia
Copy link
Author

Add gradle dependency in app module:
compile 'com.android.support:design:23.4.0'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment