Android : TabLayout not responding to clicks -
i have created layout has drawerlayout , inside there toolbar, tablayout , viewpager. in tablayout, tabs not responding on clicking on them, changing on swiping. here code:
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:context=".activities.homeactivity"> <linearlayout android:id="@+id/main_layout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> <!-- content screen without action bar--> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:background="@color/theme_color" android:elevation="6dp" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar" app:popuptheme="@style/themeoverlay.appcompat.light"/> <framelayout android:id="@+id/frame" 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_below="@+id/toolbar" android:background="@color/theme_color" android:elevation="6dp" android:minheight="?attr/actionbarsize" app:tabindicatorcolor="@color/sub_theme_color" android:theme="@style/themeoverlay.appcompat.dark.actionbar"/> <view android:layout_width="match_parent" android:layout_height="4dp" android:background="@drawable/toolbar_shadow" /> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_below="@id/tab_layout"/> </framelayout> </linearlayout> <android.support.design.widget.navigationview android:id="@+id/navigation_view" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_gravity="start" app:headerlayout="@layout/header_navigation_drawer" app:menu="@menu/menu_drawer_links" />
and in mainactivity.java
private toolbar toolbar; private navigationview navigationview; private drawerlayout drawerlayout; // initializing toolbar , setting actionbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); //intializing tab-layout , viewpager tabs tablayout tablayout = (tablayout) findviewbyid(r.id.tab_layout); tablayout.addtab(tablayout.newtab().settext("tasks")); tablayout.addtab(tablayout.newtab().settext("updates")); tablayout.addtab(tablayout.newtab().settext("redeem")); tablayout.settabgravity(tablayout.gravity_fill); // initializing viewpager hold tab-layout final viewpager viewpager = (viewpager) findviewbyid(r.id.pager); final pageradapter adapter = new pageradapter (getsupportfragmentmanager(), tablayout.gettabcount()); viewpager.setadapter(adapter); viewpager.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout)); tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } });
now here weird part: clickable in lollipop, not working in pre-lollipop version.
you can solve checking layout view in "design" tab of xml page. better put tablayout , viewpager inside relativelayout.
the activity fine.
here corrected xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" android:orientation="vertical"> <android.support.v4.widget.drawerlayout android:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:context=".activities.homeactivity"> <linearlayout android:id="@+id/main_layout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> <!-- content screen without action bar--> <!-- move toolbar above drawerlayout on opening drawer, visible --> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:background="@color/theme_color" android:elevation="6dp" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar" app:popuptheme="@style/themeoverlay.appcompat.light"/> <framelayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="match_parent"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent"/> <android.support.design.widget.tablayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/theme_color" android:elevation="6dp" android:minheight="?attr/actionbarsize" app:tabindicatorcolor="@color/sub_theme_color" android:theme="@style/themeoverlay.appcompat.dark.actionbar"/> <view android:id="@+id/shadow_toolbar" android:layout_width="match_parent" android:layout_height="4dp" android:background="@drawable/toolbar_shadow" /> </relativelayout> </framelayout> </linearlayout> <android.support.design.widget.navigationview android:id="@+id/navigation_view" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_gravity="start" app:headerlayout="@layout/header_navigation_drawer" app:menu="@menu/menu_drawer_links" /> </android.support.v4.widget.drawerlayout>
Comments
Post a Comment