sorry for my bad English.
I have a navigation drawer in some activities, I want the icon of each navigation view item to be placed to the right of that item's text and the whole item get right gravity.
Because I only used Persian in my app, I can not use "supportsRtl" in my code, if I use, it will look weird when I set device language to any rtl languages.
I think probably I can fix this with setting custom layout for navigation view, but I'm looking for a simpler way. this is my NavigationView XML code:
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="#menu/menu"
android:layout_gravity="end" />
Edited :
layoutDirection is a good answer but i used drawerLayout.openDrawer(GravityCompat.START);
for opening navigation drawer. now if device language be an rtl language this make app crash.
Make supportsRtl true and set the layout dirction to RTL on navigation View and LTR in other layouts
Just add android:layoutDirection="rtl" to your NavigationView, like this:
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="#menu/menu"
android:layoutDirection="rtl"
android:layout_gravity="end" />
by default half of hamburger is shown, as seen at the top left corner of the screenshot. I want to display full hamburger. code is taken from https://developer.android.com/training/implementing-navigation/nav-drawer.html. how can I do this?
Seems you are using very old technique to create Navigation Drawer. Its around 4 years old code and seems perfect regarding old version.
FYI, previously this kind of UI was done using DrawerLayout with ListView. But now android itself officially introduced sliding panel menu by introducing a newer concept called Navigation Drawer in which we combine DrawerLayout and NavigationView to achieve the desired output.
How to make hamburger menu fully visible?
SOLUTION:
Use AppCompatActivity instead of Activity and use AppCompat theme to achieve your desired output.
Use NavigationView instead of ListView.
Here is an example of NavigationView:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your contents -->
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
Here is a very good tutorial: Android Sliding Menu using Navigation Drawer
Hope this will help~
I have made an Activity with RelativeLayout and has been set well. Now as per the new requirement I have to add a navigation drawer to top left corner. How can I proceed further? Please anyone explain briefly.
I suggest you to use NavigationView to create your drawer. All you need to do is just to wrap your current RelativeLayout in the DrawerLayout and add the NavigationView
<android.support.v4.widget.DrawerLayout>
<RelativeLayout>
<android.support.v7.widget.Toolbar />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_gravity="start"
app:menu="#menu/drawer" />
</android.support.v4.widget.DrawerLayout>
Note you have to create your menu under res/menu/drawer.xml. You alsou should use AppCompatActivity and set your Toolbar with setSupportActionBar().
This is relatively good tutorial. This one is even more detailed.
Been looking for ways to disable the default clickable header layout in NavigationView
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer_view" />
Tried to do clickable=false in the NavigationView but it didn't work.
I also tried setClickable(false) programmatically didn't work either.
try android:enabled="false" if available for NavigationView.
Found a way to do it by inflating it in Java instead of XML.
navigationView.inflateHeaderView(R.layout.drawer_header);
the navigationView has a listview in it. reference the listview programatically. then add your own header and turn off selection on the header. to turn off selection of header use:
listview.addHeaderView(myheaderView,null,false);
I’ve implemented NavigationDrawer with android-support-v4 as per this tutorial
My result is on this screenshot.
The question is how to remove or configure these transparency on Drawer which looks awful with background text.
UPDATE:
here is my layout configuration, with background been set to dark
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111111"/>
</android.support.v4.widget.DrawerLayout>
You need to add alpha to your ListView.
Use your XML and extend the ListView definition with alpha-attribute.
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111111"
android:alpha="255"
/>
Use this in you code.
Get the background of the drawer view and set alpha for it.
Drawable background = drawerView_.getBackground();
background.setAlpha(255);
Thanks everyone who tried to answer my question.
I'm sorry the trouble was in Fragment initialization in code.
So I've tried to add fragment as new instance, but it should be just a replacement of already defined fragment in layout as follows:
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FeedFragment fragment = new FeedFragment();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
now everything works fine.
To make navigation drawer transparent, you must first define the color for your drawer in res/values/colors.xml file.
Eg:<color name="navigationDrawerNew">#7ba9530c</color>
Now, on the left side of this line, the color that you have chosen will be displayed in a small square.
See image for example
Click on it. It will bring up a color chooser. In the color chooser, at the bottom, there is a slider which allow you to adjust transparency. Adjust it to get the required transparency.
Now, set this color as background color for the list view for navigation drawer in xml file. The list view looks almost like this:-
<ListView
android:id="#+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#color/navigationDrawerNew" />
Set the Background of the Drawer (Which in the linked demo is a ListView) to a solid color/drawable
It doesn't default to be transparent like this. You must have it set in your activities layout file. Change the android:background in the android.support.v4.widget.DrawerLayout tag to be a solid colour
What's the layout that your Adapter inflates in the
getView(LayoutInflater inflater, ..., ...) ?
The ListView inflates some layout and this one seems to be transparent, not the drawer.
The background color of your Drawer becomes the color of ListView's items.
You can notice that the second part of your drawer is a little bit darker then the items.
You only need to setBackgroundColor for DrawerListView in the code like this:
mDrawerListView.setBackgroundColor(Color.LTGRAY);
This is work for me. No need to add Alpha value.
You add background color of ListView in java file then it works correctly,
listview.setBackgroundColor(Color.WHITE);