I'm trying to get the new TabLayout in the android design library working.
I'm following this post:
http://android-developers.blogspot.com/2015/05/android-design-support-library.html
and the documentation:
http://developer.android.com/reference/android/support/design/widget/TabLayout.html
And have come up with the following code in my activity but the tablayout isn't showing up when I run the activity.
I tried adding in the activity layout file, but it says it can't find that xml tag.
public class TabActivity extends BaseActivity {
SectionPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
LinearLayout v = (LinearLayout)findViewById(R.id.tabContainer);
TabLayout tabLayout = new TabLayout(this);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setLayoutParams(new LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 50));
v.addView(tabLayout);
mSectionsPagerAdapter = new SectionPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
public class SectionPagerAdapter extends FragmentPagerAdapter {
private String TAG = "SectionPagerAdapter";
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position)
{
return new Fragment();
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "test";
case 1:
return "test";
case 2:
}
return null;
}
}
}
Added the following to my gradle file
compile 'com.android.support:design:22.2.0'
I try to solve here is my code.
first add dependency in build.gradle(app).
Create PagerAdapter.class
create activity_main.xml
create MainActivity.class
and finally create fragment to add in viewpager
crate fragment_one.xml
Create FragmentOne.class
I had to collect information from various sources to put together a functioning TabLayout. The following is presented as a complete use case that can be modified as needed.
Make sure the module
build.gradle
file contains a dependency oncom.android.support:design
.In my case, I am creating an About activity in the application with a TabLayout. I added the following section to
AndroidMainifest.xml
. Setting the parentActivityName allows the home arrow to take the user back to the main activity.styles.xml
contains the following entries. This app has a white AppBar for the main activity and a blue AppBar for the About activity. We need to setcolorPrimaryDark
for the About activity so that the status bar above the AppBar is blue.There is also a
styles.xml (v19)
. It is located atsrc/main/res/values-v19/styles.xml
. This file is only applied if the API of the device is >= 19.AboutActivity.java
contains the following code. In my case I have a fixed number of tabs (7) so I could remove all the code dealing with dynamic tabs.AboutTabFragment.java
is used to populate each tab. In my case, the first tab has aLinearLayout
inside of aScrollView
and all the others have aWebView
as the root layout.about_coordinatorlayout.xml
is as follows:about_tab_version.xml
is as follows:And
about_tab_webview.xml
:There are also entries in
strings.xml
And
colors.xml
src/main/assets
contains the HTML files referenced inAboutTabFragemnt.java
.DashboardFragment
dashboardfragment.xml
DashboardPagerAdapter
}
And in each page of Fragment
setHasOptionMenu(true)
in onCreate and implementonCreateOptionMenu
. then it will work properly.dASHaCTIVITY
I've just managed to setup new TabLayout, so here are the quick steps to do this (ノ◕ヮ◕)ノ*:・゚✧
Add dependencies inside your build.gradle file:
Add TabLayout inside your layout
Setup your Activity like this: