Why android navigation drawer creating two headers? -
i followed tutorial push facebook info login activity navigation, put on top, shown, which, after days of altering code, got work. cant original header go away. i've tried changing every part of code, end or nothing. know 2 heads better one, exception rule.
also, guess cant post images, so, original header on top, should be. new header facebook profile picture , info, underneath that. , below that, starts navigation drawer menu.
public class homeactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { jsonobject response, profile_pic_data, profile_pic_url; textview user_name, user_email, tokens; imageview user_picture; navigationview navigation_view; string name; button button2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); tokens = (textview)findviewbyid(r.id.textview17); button2 = (button)findviewbyid(r.id.button2); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); intent intent = getintent(); string jsondata = intent.getstringextra("jsondata"); final string uid = intent.getstringextra("uid"); setnavigationheader(); // call setnavigationheader method. setuserprofile(jsondata, uid); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setvisibility(view.invisible); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { firebase ref = new firebase("https://luckycashslots.firebaseio.com/data/users/" + mainactivity.uid + "/"); tokens token = new tokens("100"); ref.setvalue(token); snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); 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); //toast.maketext(getapplicationcontext(), uid, toast.length_long).show(); button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // toast.maketext(getapplicationcontext(), name, toast.length_long).show(); updatetext(); } }); firebase ref = new firebase("https://luckycashslots.firebaseio.com/data/users/" + mainactivity.uid + "/"); firebase tokref = ref.child("tokens"); //tokref.setvalue(mauthdata.getprovider()); //tokens token = new tokens(100); //ref.setvalue(token); ref.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { if (datasnapshot.child("tokens").getvalue() != null) { name = (string) datasnapshot.child("tokens").getvalue().tostring(); tokens.settext(name); //tokens.settext(datasnapshot.getvalue().tostring()); // toast.maketext(getapplicationcontext(), name, toast.length_long).show(); // system.out.println(datasnapshot.getvalue()); // string woot = datasnapshot.getvalue().tostring(); // tokens.settext(woot); } } @override public void oncancelled(firebaseerror firebaseerror) { toast.maketext(getapplicationcontext(), "couldnt update token text", toast.length_long).show(); } }); } public void updatetext(){ firebase ref = new firebase("https://luckycashslots.firebaseio.com/data/users/" + mainactivity.uid + "/"); firebase tokref = ref.child("tokens"); //tokref.setvalue(mauthdata.getprovider()); // tokens token = new tokens(100); // ref.setvalue(token); ref.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { (datasnapshot tokensnapshot: datasnapshot.getchildren()){ tokens token = tokensnapshot.getvalue(tokens.class); system.out.println(token.tostring()); name = token.tostring(); // name = (string) datasnapshot.child("tokens").getvalue(); tokens.settext(name); } // name = (string) datasnapshot.child("tokens").getvalue().tostring(); // tokens.settext(name); } @override public void oncancelled(firebaseerror firebaseerror) { toast.maketext(getapplicationcontext(), "couldnt update token text", toast.length_long).show(); } }); } public void setnavigationheader(){ navigation_view = (navigationview) findviewbyid(r.id.nav_view); navigation_view.removeheaderview(null); view header = layoutinflater.from(this).inflate(r.layout.nav_header_home, null); navigation_view.addheaderview(header); user_name = (textview) header.findviewbyid(r.id.username); user_picture = (imageview) header.findviewbyid(r.id.profile_pic); user_email = (textview) header.findviewbyid(r.id.email); } public void setuserprofile(string jsondata, string uid){ try { response = new jsonobject(jsondata); user_email.settext(response.get("email").tostring()); // user_email.settext(mainactivity.uemail); user_name.settext(response.get("name").tostring()); // user_name.settext(mainactivity.uname); profile_pic_data = new jsonobject(response.get("picture").tostring()); profile_pic_url = new jsonobject(profile_pic_data.getstring("data")); picasso.with(this).load(profile_pic_url.getstring("url")).into(user_picture); } catch (exception e) { e.printstacktrace(); } } @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.home, 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); } @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) { // handle navigation view item clicks here. int id = item.getitemid(); if (id == r.id.nav_slots) { // handle camera action1 intent intent2 = new intent(this, slotpageview.class); startactivity(intent2); } else if (id == r.id.nav_spin) { intent intent2 = new intent(this, dailyspinactivity.class); startactivity(intent2); } else if (id == r.id.nav_offers) { } else if (id == r.id.nav_prizes) { intent intent2 = new intent(this, prizesactivity.class); startactivity(intent2); } else if (id == r.id.nav_winners) { intent intent2 = new intent(this, winnersactivity.class); startactivity(intent2); } else if (id == r.id.nav_stats) { } else if (id == r.id.nav_account) { intent intent2 = new intent(this, accountactivity.class); startactivity(intent2); } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); return true; } }
activity_home.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"> <include layout="@layout/app_bar_home" 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_home" app:menu="@menu/activity_home_drawer" />
app bar home xml
<?xml version="1.0" encoding="utf-8"?> <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="learn2crack.learn2crackfb.homeactivity"> <android.support.design.widget.appbarlayout android:layout_height="wrap_content" android:layout_width="match_parent" 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 layout="@layout/content_home" /> <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" /> </android.support.design.widget.coordinatorlayout>
nav header home xml
<?xml version="1.0" encoding="utf-8"?> <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: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" android:orientation="vertical" android:gravity="bottom"> <de.hdodenhof.circleimageview.circleimageview android:id="@+id/profile_pic" android:layout_width="80dp" android:layout_height="80dp" android:paddingtop="@dimen/nav_header_vertical_spacing" android:src="@android:drawable/sym_def_app_icon" /> <textview android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingtop="@dimen/nav_header_vertical_spacing" android:text="android studio" android:textsize="14dp" android:textappearance="@style/textappearance.appcompat.body1" android:textcolor="#000000" /> <textview android:id="@+id/email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android.studio@android.com" /> </linearlayout>
when add header using app:headerlayout="@layout/nav_header_home"
below
<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_home" app:menu="@menu/activity_home_drawer" />
then no need add header again code
remove below lines setnavigationheader()
method
navigation_view.removeheaderview(null); view header = layoutinflater.from(this).inflate(r.layout.nav_header_home, null); navigation_view.addheaderview(header);
after remove method below
edit /
public void setnavigationheader(){ navigation_view = (navigationview) findviewbyid(r.id.nav_view);
view header = layoutinflater.from(this).inflate(r.layout.nav_header_home, null);
user_name = (textview) navigation_view.findviewbyid(r.id.username); user_picture = (imageview) navigation_view.findviewbyid(r.id.profile_pic); user_email = (textview) navigation_view. findviewbyid(r.id.email); }
Comments
Post a Comment