android - How to attach fragment to Navigationview -
i using naivgationview in app, when select item drawer,the view of fragment set not displaying. added toast shows while select item can 1 me ?
public class mainactivity extends appcompatactivity { private drawerlayout drawerlayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); //setting navigation view item selected listener handle item click of navigation menu navigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() { // method trigger on item click of navigation menu @override public boolean onnavigationitemselected(menuitem menuitem) { fragmentmanager fragmentmanager = getfragmentmanager(); //checking if item in checked state or not, if not make in checked state if(menuitem.ischecked()) menuitem.setchecked(false); else menuitem.setchecked(true); //closing drawer on item click drawerlayout.closedrawers(); //check see item being clicked , perform appropriate action switch (menuitem.getitemid()){ //replacing main content contentfragment our inbox view; case r.id.home: toast.maketext(getapplicationcontext(),"inbox selected", toast.length_short).show(); return true; // rest of options show toast on click case r.id.be_the_donor: toast.maketext(getapplicationcontext(),"stared selected",toast.length_short).show(); registerdonor frag = new registerdonor(); // update main content replacing fragments fragmentmanager.begintransaction() .replace(r.id.frame_container, frag) .settransition(fragmenttransaction.transit_fragment_open) .addtobackstack(null) .commit(); return true; case r.id.search_donor: toast.maketext(getapplicationcontext(),"send selected",toast.length_short).show(); return true; case r.id.tools: toast.maketext(getapplicationcontext(),"drafts selected",toast.length_short).show(); return true; default: toast.maketext(getapplicationcontext(),"somethings wrong",toast.length_short).show(); return true; } } }); // initializing drawer layout , actionbartoggle drawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle actionbardrawertoggle = new actionbardrawertoggle(this,drawerlayout,toolbar,r.string.navigation_drawer_open, r.string.navigation_drawer_close){ @override public void ondrawerclosed(view drawerview) { // code here triggered once drawer closes dont want happen leave blank super.ondrawerclosed(drawerview); } @override public void ondraweropened(view drawerview) { // code here triggered once drawer open dont want happen leave blank super.ondraweropened(drawerview); } }; //setting actionbartoggle drawer layout drawerlayout.setdrawerlistener(actionbardrawertoggle); //calling sync state necessay or else hamburger icon wont show actionbardrawertoggle.syncstate(); } @override public void onbackpressed() { drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawer(gravitycompat.start); } else { super.onbackpressed(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
fragment
public class registerdonor extends fragment{ @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.register_donor, container, false); return rootview; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout 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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:opendrawer="start"> <linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" > <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <framelayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent"> </framelayout> </linearlayout> <android.support.design.widget.navigationview android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitssystemwindows="true" app:headerlayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.drawerlayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout 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_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.dayshift.bloodfinder.mainactivity" tools:showin="@layout/app_bar_main"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent" /> </android.support.constraint.constraintlayout>
register_donor
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.textinputlayout android:id="@+id/usernamewrapper" android:layout_width="match_parent" android:layout_height="wrap_content"> <edittext android:id="@+id/register_donor_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textemailaddress" android:hint="enter name"/> </android.support.design.widget.textinputlayout> <android.support.design.widget.textinputlayout android:id="@+id/mobilewrapper" android:layout_width="match_parent" android:layout_height="wrap_content"> <edittext android:id="@+id/register_donor_mobile" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="number" android:hint="enter mobile"/> </android.support.design.widget.textinputlayout> <com.weiwangcn.betterspinner.library.material.materialbetterspinner android:id="@+id/register_donor_bloodgroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="material design spinner" android:textcolorhint="#05ab9a" app:met_floatinglabel="normal" /> <com.weiwangcn.betterspinner.library.material.materialbetterspinner android:id="@+id/register_donor_city" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="material design spinner" android:textcolorhint="#05ab9a" app:met_floatinglabel="normal" /> <autocompletetextview android:id="@+id/register_donor_area" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:text=""> <requestfocus /> </autocompletetextview> </linearlayout>
your app bar occupies entire screen.
<include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <framelayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent"> </framelayout>
change app bar wrap content on height
Comments
Post a Comment