How to Adjust the width of navigation drawer in android studio -
i downloaded navigation template, , want adjust width of navigation. i've tried different methods think nothing suited. how can solve problem? can't adjust width of navigation drawer.
this code navigationaldrawerfragment.java
package com.example.user.navigationkamo; import android.app.activity; import android.app.fragment; import android.content.sharedpreferences; import android.content.res.configuration; import android.content.res.resources; import android.graphics.bitmap; import android.graphics.bitmapshader; import android.graphics.canvas; import android.graphics.colorfilter; import android.graphics.paint; import android.graphics.pixelformat; import android.graphics.rect; import android.graphics.rectf; import android.graphics.shader; import android.graphics.drawable.drawable; import android.os.bundle; import android.preference.preferencemanager; import android.support.v7.app.actionbardrawertoggle; import android.support.v4.widget.drawerlayout; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.util.typedvalue; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.textview; import java.util.arraylist; import java.util.list; /** * fragment used managing interactions , presentation of navigation drawer. * see <a href="https://developer.android.com/design/patterns/navigation-drawer.html#interaction"> * design guidelines</a> complete explanation of behaviors implemented here. */ public class navigationdrawerfragment extends fragment implements navigationdrawercallbacks { /** * remember position of selected item. */ private static final string state_selected_position = "selected_navigation_drawer_position"; /** * per design guidelines, should show drawer on launch until user manually * expands it. shared preference tracks this. */ private static final string pref_user_learned_drawer = "navigation_drawer_learned"; /** * pointer current callbacks instance (the activity). */ private navigationdrawercallbacks mcallbacks; /** * helper component ties action bar navigation drawer. */ private actionbardrawertoggle mactionbardrawertoggle; private drawerlayout mdrawerlayout; private recyclerview mdrawerlist; private view mfragmentcontainerview; private int mcurrentselectedposition = 0; private boolean mfromsavedinstancestate; private boolean muserlearneddrawer; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // read in flag indicating whether or not user has demonstrated awareness of // drawer. see pref_user_learned_drawer details. sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(getactivity()); muserlearneddrawer = sp.getboolean(pref_user_learned_drawer, false); if (savedinstancestate != null) { mcurrentselectedposition = savedinstancestate.getint(state_selected_position); mfromsavedinstancestate = true; } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_navigation_drawer, container, false); mdrawerlist = (recyclerview) view.findviewbyid(r.id.drawerlist); linearlayoutmanager layoutmanager = new linearlayoutmanager(getactivity()); layoutmanager.setorientation(linearlayoutmanager.vertical); mdrawerlist.setlayoutmanager(layoutmanager); mdrawerlist.sethasfixedsize(true); final list<navigationitem> navigationitems = getmenu(); navigationdraweradapter adapter = new navigationdraweradapter(navigationitems); adapter.setnavigationdrawercallbacks(this); mdrawerlist.setadapter(adapter); selectitem(mcurrentselectedposition); return view; } public void setup(int fragmentid, drawerlayout drawerlayout) { mdrawerlayout = drawerlayout; mdrawerlayout.setdrawerlockmode(drawerlayout.lock_mode_locked_closed); } public boolean isdraweropen() { return mdrawerlayout != null && mdrawerlayout.isdraweropen(mfragmentcontainerview); } public actionbardrawertoggle getactionbardrawertoggle() { return mactionbardrawertoggle; } public drawerlayout getdrawerlayout() { return mdrawerlayout; } @override public void onnavigationdraweritemselected(int position) { selectitem(position); } public list<navigationitem> getmenu() { list<navigationitem> items = new arraylist<navigationitem>(); items.add(new navigationitem("driver details", getresources().getdrawable(r.drawable.ic_driver))); items.add(new navigationitem("track me", getresources().getdrawable(r.drawable.ic_track))); items.add(new navigationitem("contact", getresources().getdrawable(r.drawable.ic_contact))); items.add(new navigationitem("report driver", getresources().getdrawable(r.drawable.ic_report))); items.add(new navigationitem("call emergency", getresources().getdrawable(r.drawable.ic_call))); items.add(new navigationitem("rate taxi", getresources().getdrawable(r.drawable.ic_rate))); items.add(new navigationitem("favorites", getresources().getdrawable(r.drawable.ic_favorite))); items.add(new navigationitem("top drivers", getresources().getdrawable(r.drawable.ic_top))); items.add(new navigationitem("security tips", getresources().getdrawable(r.drawable.ic_security))); return items; } /** * users of fragment must call method set navigation drawer interactions. * * @param fragmentid android:id of fragment in activity's layout. * @param drawerlayout drawerlayout containing fragment's ui. * @param toolbar toolbar of activity. */ public void setup(int fragmentid, drawerlayout drawerlayout, toolbar toolbar) { mfragmentcontainerview = getactivity().findviewbyid(fragmentid); mdrawerlayout = drawerlayout; mdrawerlayout.setstatusbarbackgroundcolor(getresources().getcolor(r.color.myprimarydarkcolor)); mactionbardrawertoggle = new actionbardrawertoggle(getactivity(), mdrawerlayout, toolbar, r.string.drawer_open, r.string.drawer_close) { @override public void ondrawerclosed(view drawerview) { super.ondrawerclosed(drawerview); if (!isadded()) return; getactivity().invalidateoptionsmenu(); // calls onprepareoptionsmenu() } @override public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); if (!isadded()) return; if (!muserlearneddrawer) { muserlearneddrawer = true; sharedpreferences sp = preferencemanager .getdefaultsharedpreferences(getactivity()); sp.edit().putboolean(pref_user_learned_drawer, true).apply(); } getactivity().invalidateoptionsmenu(); // calls onprepareoptionsmenu() } }; // if user hasn't 'learned' drawer, open introduce them drawer, // per navigation drawer design guidelines. if (!muserlearneddrawer && !mfromsavedinstancestate) { mdrawerlayout.opendrawer(mfragmentcontainerview); } // defer code dependent on restoration of previous instance state. mdrawerlayout.post(new runnable() { @override public void run() { mactionbardrawertoggle.syncstate(); } }); mdrawerlayout.setdrawerlistener(mactionbardrawertoggle); } private void selectitem(int position) { mcurrentselectedposition = position; if (mdrawerlayout != null) { mdrawerlayout.closedrawer(mfragmentcontainerview); } if (mcallbacks != null) { mcallbacks.onnavigationdraweritemselected(position); } ((navigationdraweradapter) mdrawerlist.getadapter()).selectposition(position); } public void opendrawer() { mdrawerlayout.opendrawer(mfragmentcontainerview); } public void closedrawer() { mdrawerlayout.closedrawer(mfragmentcontainerview); } @override public void onattach(activity activity) { super.onattach(activity); try { mcallbacks = (navigationdrawercallbacks) activity; } catch (classcastexception e) { throw new classcastexception("activity must implement navigationdrawercallbacks."); } } @override public void ondetach() { super.ondetach(); mcallbacks = null; } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint(state_selected_position, mcurrentselectedposition); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); // forward new configuration drawer toggle component. mactionbardrawertoggle.onconfigurationchanged(newconfig); } public void setuserdata(string user, string email, bitmap avatar) { imageview avatarcontainer = (imageview) mfragmentcontainerview.findviewbyid(r.id.imgavatar); ((textview) mfragmentcontainerview.findviewbyid(r.id.txtuseremail)).settext(email); ((textview) mfragmentcontainerview.findviewbyid(r.id.txtusername)).settext(user); avatarcontainer.setimagedrawable(new roundimage(avatar)); } public view getgoogledrawer() { return mfragmentcontainerview.findviewbyid(r.id.googledrawer); } public static class roundimage extends drawable { private final bitmap mbitmap; private final paint mpaint; private final rectf mrectf; private final int mbitmapwidth; private final int mbitmapheight; public roundimage(bitmap bitmap) { mbitmap = bitmap; mrectf = new rectf(); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); final bitmapshader shader = new bitmapshader(bitmap, shader.tilemode.clamp, shader.tilemode.clamp); mpaint.setshader(shader); mbitmapwidth = mbitmap.getwidth(); mbitmapheight = mbitmap.getheight(); } @override public void draw(canvas canvas) { canvas.drawoval(mrectf, mpaint); } @override protected void onboundschange(rect bounds) { super.onboundschange(bounds); mrectf.set(bounds); } @override public void setalpha(int alpha) { if (mpaint.getalpha() != alpha) { mpaint.setalpha(alpha); invalidateself(); } } @override public void setcolorfilter(colorfilter cf) { mpaint.setcolorfilter(cf); } @override public int getopacity() { return pixelformat.translucent; } @override public int getintrinsicwidth() { return mbitmapwidth; } @override public int getintrinsicheight() { return mbitmapheight; } public void setantialias(boolean aa) { mpaint.setantialias(aa); invalidateself(); } @override public void setfilterbitmap(boolean filter) { mpaint.setfilterbitmap(filter); invalidateself(); } @override public void setdither(boolean dither) { mpaint.setdither(dither); invalidateself(); } public bitmap getbitmap() { return mbitmap; } } }
take in activity's layout-file firstly assign navigationdrawer. beside few other information should find section looking this
<fragment android:id="@+id/navigation_drawer" android:name="com.example.myapplication.navigationdrawerfragment" android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" tools:layout="@layout/fragment_navigation_drawer" />
the tag android:layout_width
defines drawer's width , it's linked dimensions-file. if there (cmd/ctrl + click) can or redefine value:
<resources> <!-- default screen margins, per android design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <!-- per design guidelines, navigation drawers should between 240dp , 320dp: https://developer.android.com/design/patterns/navigation-drawer.html --> <dimen name="navigation_drawer_width">240dp</dimen>
of course can set width directly in layout-file too.
Comments
Post a Comment