java - How to add back button arrow functionality in navigation bar -
i beginner programmer , resent started android developing. watched many answers , couldn't find answer witch fit me. have added arrow button in action bar, couldn't figure out how add navigation first loaded screen.
mainactivity.java
public class mainactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); actionbar actionbar = getsupportactionbar(); if (actionbar != null){ actionbar.setdisplayhomeasupenabled(true); actionbar.sethomebuttonenabled(true); } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); final actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); getsupportfragmentmanager().addonbackstackchangedlistener(new fragmentmanager.onbackstackchangedlistener() { @override public void onbackstackchanged() { toggle.setdrawerindicatorenabled(getsupportfragmentmanager().getbackstackentrycount() == 0); } }); } @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; } switch (item.getitemid()) { // respond action bar's up/home button case android.r.id.home: toast.maketext(getapplicationcontext(),"back button clicked", toast.length_short).show(); return true; } return super.onoptionsitemselected(item); } @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) { // handle navigation view item clicks here. int id = item.getitemid(); textview text = (textview) findviewbyid(r.id.container_text); fragment fragment = null; if (id == r.id.nav_about) { fragment = demofragment.newinstance("about"); text.settext("1"); } else if (id == r.id.nav_settings) { fragment = demofragment.newinstance("nav settings"); } getsupportfragmentmanager() .begintransaction() .replace(r.id.container, fragment) .addtobackstack(fragment.getclass().getsimplename()) .commit(); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); return true; }
demofragment.java
public class demofragment extends fragment { public static final string text = "text"; public static demofragment newinstance(string text) { bundle args = new bundle(); args.putstring(text, text); demofragment fragment = new demofragment(); fragment.setarguments(args); return fragment; } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_demo, container, false); string text = getarguments().getstring(text); return view; }
androidmanifest.xml
<application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity" android:label="@string/app_name" android:theme="@style/apptheme.noactionbar"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>
activity_main.xml
<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"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <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" />
app_bar_main.xml
<android.support.design.widget.coordinatorlayout 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" android:fitssystemwindows="true" tools:context="lt.simbal.drawer.mainactivity"> <android.support.design.widget.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.appbarlayout> <include android:id="@+id/container" layout="@layout/content_main" /> <android.support.design.widget.floatingactionbutton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" />
content_main.xml
<framelayout 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" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="lt.simbal.drawer.mainactivity" tools:showin="@layout/app_bar_main"> <textview android:id="@+id/container_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="main_activity" />
fragment_demo.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:id="@+id/text" android:layout_width="match_parent" android:layout_height="match_parent" />
nav_header_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:background="@drawable/side_nav_bar" android:gravity="bottom" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:theme="@style/themeoverlay.appcompat.dark"> <imageview android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingtop="@dimen/nav_header_vertical_spacing" android:src="@android:drawable/sym_def_app_icon" /> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingtop="@dimen/nav_header_vertical_spacing" android:text="android studio" android:textappearance="@style/textappearance.appcompat.body1" /> <textview android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android.studio@android.com" />
activity_main_drawer.xml
<group android:checkablebehavior="single"> <item android:id="@+id/nav_about" android:icon="@drawable/ic_menu_camera" android:title="@string/about" /> <item android:id="@+id/nav_settings" android:icon="@drawable/ic_menu_manage" android:title="@string/settings" /> </group>
you need call sethomeasupindicator & setdisplayhomeasupenabled
set alternate drawable display next icon/logo/title when display_home_as_up enabled. can useful if using mode display alternate selection navigation, such sliding drawer.
actionbar actionbar = getsupportactionbar(); if (actionbar != null) { ...... actionbar.setdisplayhomeasupenabled(true); //set true if selecting "home" returns single level in ui rather top level or front page. actionbar.sethomeasupindicator(r.drawable.your_icon); // set custom icon default home button }
now
for handling need override onoptionsitemselected(menuitem item)
method in activity .
@override public boolean onoptionsitemselected(menuitem item) { switch(item.getitemid()) { case r.id.home: // add logic here break; default: return super.onoptionsitemselected(item); } return true; }
Comments
Post a Comment