bottomnavigationview - Android Bottom Navigation -
i have been implementing bottom navigation bar app, problem no matter activity on dashboard icon highlighted one. how whichever activity on highlighted one?
public class dashboard extends appcompatactivity implements view.onclicklistener { private bottomnavigationview.onnavigationitemselectedlistener monnavigationitemselectedlistener = new bottomnavigationview.onnavigationitemselectedlistener() { @override public boolean onnavigationitemselected(@nonnull menuitem item) { switch (item.getitemid()) { case r.id.navigation_request: intent r = new intent(dashboard.this, request.class); startactivity(r); finish(); break; case r.id.navigation_settings: intent s = new intent(dashboard.this, appsettings.class); startactivity(s); finish(); break; } return false; } }; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_dashboard); bottomnavigationview navigation = (bottomnavigationview) findviewbyid(r.id.navigation); navigation.setonnavigationitemselectedlistener(monnavigationitemselectedlistener); } here xml menu file
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/navigation_home" android:icon="@drawable/ic_home_black_24dp" android:title="@string/title_home" /> <item android:id="@+id/navigation_request" android:icon="@drawable/ic_request_icon" android:title="@string/title_request" /> <item android:id="@+id/navigation_settings" android:icon="@drawable/ic_icon_settings" android:title="@string/title_settings" /> </menu> here how use navigation in activity_dashboard
<?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" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <framelayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <!-- textviews here --> </framelayout> <android.support.design.widget.bottomnavigationview android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?android:attr/windowbackground" app:menu="@menu/navigation" /> </linearlayout>
using many different activity instances duplicate bottomnavigationview , associated code (like onnavigationitemselectedlistener etc) not recommended. instead, use single activity hosts bottomnavigationview multiple fragment instances .replace()d content area user interacts bottomnavigationview.
that being said, let's see if can solve problem.
there 2 pieces puzzle. first simple: have indicate item in bottomnavigationview should selected. can achieved calling setselecteditemid(). add oncreate() method.
navigation.setselecteditemid(r.id.navigation_settings); the second little more complicated. when call setselecteditemid(), system going behave though user had tapped on item. in other words, onnavigationitemselectedlistener triggered.
looking @ posted listener, notice return false. if check documentation onnavigationitemselected(), find
returns:
truedisplay item selected item ,falseif item should not selected
so call setselecteditemid() won't work without changing listener return true.
you still solve problem setselecteditemid() call if place call before setonnavigationitemselectedlistener() call, that's masking problem. it'd better fix listener return true in cases want tapped item appear selected.
Comments
Post a Comment