Okay, I decided that using TabHost would be nice in my little app. However, under Android 6.0+, TabActivity and several others have been deprecated. So I have written this:
public class TabMain extends Activity{
@Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.tab_main);
TabHost tabHost = (TabHost)findViewById(R.id.tab_main_tabhost);
tabHost.setup();//several lines here....}}
And when I run this code, IDE throws me an RuntimtException: "java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?" And I found out that ActivityGroup is also deprecated. How should I bypass this?
try this
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="@+id/home_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
in your activity
viewPager = (ViewPager) findViewById(R.id.home_viewpager);
viewPager.setOffscreenPageLimit(4);
if (viewPager != null) {
setupViewPager(viewPager);
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
method is
private void setupViewPager(ViewPager viewPager) {
adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new Store(), "Store");//fragment class name
adapter.addFragment(new Post(), "GG Connect");//fragment class name
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}