Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click on tab layout doesn't work at all

I have implemented this example.

but not able to click on 2nd tab.

my xml file looks like

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header"
    android:orientation="horizontal">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_below="@+id/header"
        android:background="@color/colorPrimary"
        app:tabSelectedTextColor="#ffffff" />

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/padding"
        android:layout_height="wrap_content"
        android:layout_below="@id/tab_layout" />

</LinearLayout>

and Main activity

 pager= (ViewPager) findViewById(R.id.view_pager);
    tabLayout= (TabLayout) findViewById(R.id.tab_layout);

    FragmentManager manager=getSupportFragmentManager();
    BuyCurrencyPagerAdapter adapter=new BuyCurrencyPagerAdapter(manager);

    //set Adapter to view pager
    pager.setAdapter(adapter);
    tabLayout.setupWithViewPager(pager);
    pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setTabsFromPagerAdapter(adapter);

please give any suggestions.

like image 722
Shruti Avatar asked Dec 21 '15 13:12

Shruti


People also ask

How to add tab layout in Android studio?

This example demonstrates how do I create a Tab Layout in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 3 − Add the following code to res/layout/activity_main. xml.


2 Answers

Sometimes I believe the problem might be that the ViewPager is positioned above the TabLayout. You want to place it below like this:

<AppBarLayout>
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

<android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"/>
like image 118
Gouvernator Avatar answered Sep 16 '22 21:09

Gouvernator


UPDATE: setOnTabSelectedListener deprecated, use addOnTabSelectedListener

Try this:

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
    @Override
        public void onTabSelected(TabLayout.Tab tab) {
        pager.setCurrentItem(tab.getPosition());
    }

});

If it's not working in your case, check your layout. May be there is a view overlapping your TabLayout.

like image 31
Oğuzhan Döngül Avatar answered Sep 18 '22 21:09

Oğuzhan Döngül