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.
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.
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"/>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With