Skip to content Skip to sidebar Skip to footer

Using Viewpager With Tabs Without Actionbar

I am using the google example called EffectiveNavigation to create a ViewPager with tabs. The problem is that in the manifest, for my main activity, I have set android:theme='@andr

Solution 1:

Using the ActionBar to hold the tabs is actually deprecated because, even if your app does have action bar, it may lead to NullPointerException. Good news are the TabLayout from the Design package brings you the possibility to easily create a ViewPager with tabs. Just write this in the XML of your Activity (or Fragment):

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />

</android.support.v4.view.ViewPager>

And once you have your Adapter for the ViewPager, include this code in the onCreate method of your Activity (or Fragment) after the setContentView method:

ViewPagerviewPager= (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(newMyPagerAdapter(getSupportFragmentManager()));

TabLayouttabLayout= (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);

Hope it helps ;)

Solution 2:

I found a fix. Basically in onCreate, before setContentView, I call

requestWindowFeature(Window.FEATURE_ACTION_BAR);

Then everything works fine.

Solution 3:

To anyone else arriving here finding that @learner's solution doesn't work for them, take a look at http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/

Post a Comment for "Using Viewpager With Tabs Without Actionbar"