java - android toolbar inside a tab -
i want make toolbar inside 1 of tabs nothing works me. can done? have toolbar above tabs need appear inside first tab.
here's code (with toolbar above tabs):
import ... public class mainactivity extends actionbaractivity { toolbar toolbar; viewpager pager; viewpageradapter adapter; slidingtablayout tabs; charsequence titles[] = {"projects", "people", "files"}; int numboftabs = 3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.tool_bar); setsupportactionbar(toolbar); adapter = new viewpageradapter(getsupportfragmentmanager(), titles, numboftabs); pager = (viewpager) findviewbyid(r.id.pager); pager.setadapter(adapter); tabs = (slidingtablayout) findviewbyid(r.id.tabs); tabs.setdistributeevenly(true); tabs.setviewpager(pager); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_primary, menu); return true; } final arraylist<string> listitems = new arraylist<string>(); @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.addbutton) { final textview noproject = (textview) findviewbyid(r.id.noproject); final listadapter addadapter = new arrayadapter<string>(this, r.layout.list_item, r.id.listframe, listitems); final listview lv = (listview) findviewbyid(r.id.lv); lv.setadapter(addadapter); noproject.setvisibility(view.gone); lv.setvisibility(view.visible); listitems.add("new project"); lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent switchtoedit = new intent(mainactivity.this, teamcreate.class); startactivity(switchtoedit); } }); } return super.onoptionsitemselected(item); } }
first tab:
import ... public class tab1 extends fragment { @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view v =inflater.inflate(r.layout.tab_1,container,false); return v; } }
i faced similar challenge non-standard toolbar , tabs , got past it, though frankly results odd looking. can toolbar work under tab using similar technique, may leave users scratching heads. technique have 2 toolbars, 1 holding tablayout widget , 1 inside fragment(s) in viewpager.
i using material design tabs detail view in app. looked fine until did 2-pane master-detail view tablets. had issue tabs wanting directly under toolbar, no obvious way have master & detail share toolbar tabs showing under detail portion of screen.
i got working by:
create "noactionbar" style in styles.xml, refer in main layout using "android:theme="@style/apptheme".
<!-- turn off normal application bar can put our toolbars please. replacing "theme.appcompat.light.darkactionbar" noactionbar per http://www.truiton.com/2015/04/android-action-bar-dialog-using-toolbar/ --> <style name="apptheme" parent="materialtheme.base"/> <style name="materialtheme.base" parent="theme.appcompat.light.noactionbar"> <item name="windownotitle">true</item> <!--we using toolbar no need show actionbar--> <item name="windowactionbar">false</item>
toss android.support.design.widget.coordinatorlayout , use linearlayout.
here snippet has toolbar containing tabs taking part of screen real estate rather entire width. note toolbar has "app:layout_scrollflags="scroll|enteralways":
<framelayout android:id="@+id/detail_container" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="end" android:layout_weight="2" tools:name="com.android.bazemom.popularmovies.tabcontainerfragment" > <!-- second toolbar used tabs --> <android.support.v7.widget.toolbar android:id="@+id/toolbar" 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="wrap_content" android:background="?attr/colorprimary" android:minheight="?attr/actionbarsize" app:layout_scrollflags="scroll|enteralways" /> <android.support.design.widget.tablayout android:id="@+id/tabanim_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!-- helps handing fragments load each tab --> <android.support.v4.view.viewpager android:id="@+id/tabanim_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
here master-detail view looked on wide tablet. tabs circled in green. toolbar action above tabs.
Comments
Post a Comment