Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tabMode="scrollable" not working

I am using the TabLayout from Android Design support library. I am using material design theme. Below is the code

activity_scrollable_Tab.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 <android.support.design.widget.AppBarLayout

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"/>

    </android.support.design.widget.AppBarLayout>

   <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

scrollableTabActivity.java

public class ScrollableTabsActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrollable_tabs);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment(), "ONE");
        adapter.addFrag(new TwoFragment(), "TWO");
        .....
        adapter.addFrag(new TenFragment(), "TEN");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

Tab scrolling is working in Lollipop. But the same code is not working on Kitkat and Jelly bean devices. On Kitkat and Jelly bean device I face two problem,

  1. When I scroll the Tab, Tab is scrolling along with page navigation in Jellybean and kitkat devices, but in Lollipop Tab alone gets scroll and it is working file. For eg: if 1,2,3..10 are the Tab and if I scroll the Tab, it scroll and for each scroll the Layout gets navigated. If I scroll, Tab gets navigate to 2nd Tab and then 3rd and so on.

  2. Up navigation is not working in Jellybean and Kitkat devices. If I swipe the Toolbar, Tab scrolling is happening with page navigation (above-mentioned issue).

Can anyone tell me what could be the problem and in which portion I need to check and sort it. I can send APK file if provide me the email for a better understanding of the problem.

I am new to android, need some help to fix this.

like image 200
user2681579 Avatar asked Oct 03 '15 17:10

user2681579


People also ask

How do you scroll tabs on Android?

Simply just put app:tabMode="scrollable" inside the xml. Save this answer.


3 Answers

Maybe you should set that tab mode programmatically? Try to call setTabMode method on TabLayaut instance tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

I don't know why it doesn't work for you, but I tested these modes few weeks ago, and for me scrollable mode worked on all Android versions.

Also check if you use the latest version of Design Support Library, maybe you use some previous bugged version?

like image 90
Kamil Kamiński Avatar answered Oct 14 '22 03:10

Kamil Kamiński


To make Tab scrollable work in kitkat and Jelly bean devices you can refer this tutorial

http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html

Google docs is also there for this https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html

It is well tested and working for me on all devices

like image 34
Awadesh Avatar answered Oct 14 '22 03:10

Awadesh


My case is some thing different which is not solved by most of the answers i tried.

The answer is add xmlns:app="http://schemas.android.com/apk/res-auto" in your android.support.design.widget.CoordinatorLayout.

I am posting the complete code here,

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

After adding xmlns:app="http://schemas.android.com/apk/res-auto" in my TabLayout xml file, Tab scroll function working fine in Jelly bean, Kitkat and lollipop.

like image 2
user2681579 Avatar answered Oct 14 '22 05:10

user2681579