Android navigation box is not fullscreen

Since Google introduced the navigation box, I tried to use this component to create a menu similar to facebook. The problem is that the visual effect seems different.

There is an action bar in google when the box is open, but not in facebook. Instead, the entire screen is pressed right.

I found that some libs can achieve this, but since I prefer not to include a third-party library in the project, is there a way to achieve this? Thanks

Google:

Facebook:

Code Based on the Navigation Box Tutorial

protected void setupMenu(List<String> list, final ListView menu) { Adapter customAdapter = new Adapter(getActionBar().getThemedContext(), R.layout.item, list); menu.setAdapter(customAdapter); menu.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, final int pos, long id) { String selected = ((TextView) view.findViewById(R.id.itemTxt)) .getText().toString(); // define pass data final Bundle bData = new Bundle(); bData.putString("itemSelect", selected); drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener() { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); FragmentTransaction tx = getSupportFragmentManager() .beginTransaction(); tx.replace(R.id.mainContent, Fragment.instantiate( MainActivity.this, "com.example.utilities.ContentPage", bData)); tx.commit(); } }); drawer.closeDrawer(menu); } }); } 
+6
source share
6 answers

Well, creating a custom navigation box is the best solution for you. I understand that you do not want to use a third party, but this can be a quick solution to your problem. Sliding menu Lib link .

+4
source

Here is a quick solution that worked for me:

 <include android:id="@+id/left_drawer" android:orientation="vertical" **android:layout_width="320dp"** android:layout_height="match_parent" android:layout_gravity="start" layout="@layout/drawer"/> 

Set the width of the included layout. For devices with different screen sizes, you can dynamically set the width of this included layout.

All the best !!!!

+2
source

Hope this helps.

  <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" > <include layout="@layout/nav_header_main" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.design.widget.NavigationView> 

Delete the last two lines in your default code

 app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" 
+2
source

If you check the source code of DrawerLayout, you will see that resposnible for this minimum field is the variable mMinDrawerMargin

So, there are at least 2 solutions (tricks) 1. continue DrawerLayout and set this variable to 0 with reflection. call this method from all the constructors.

 private void init() { try { Field declaredField = getClass().getSuperclass().getDeclaredField("mMinDrawerMargin"); declaredField.setAccessible(true); declaredField.setInt(declaredField, 0); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } 
  1. not so hard

overryde onMeasure method like this

 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // taken from parents logic float density = this.getResources().getDisplayMetrics().density; int minMargin = (int) (64.0F * density + 0.5F); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int newWidth = MeasureSpec.makeMeasureSpec(widthSize + minMargin, widthMode); super.onMeasure(newWidth, heightMeasureSpec); } 

I also created a sample project here https://bitbucket.org/wnc_21/fsnavigationdrawer

+1
source

this problem is caused by margin, so we need to reset the width: I solved this problem by running DrawerListener and wrapping the ListView inside LinearLayout to create rooms for other views in the list view.

here is my listener

 public class NavigationDrawer implements DrawerLayout.DrawerListener { private DrawerLayout mDrawerLayout; protected boolean expanded = false; NavigationDrawer(Activity activity) { this.activity = activity; } public NavigationDrawer bindDrawerLayout(int id) { mDrawerLayout = (DrawerLayout) activity.findViewById(id); mDrawerLayout.setDrawerListener(this); return this; } @Override public void onDrawerSlide(View drawerView, float slideOffset) { if (!expanded) { Log.i("margin", "here we are"); LinearLayout layout = (LinearLayout) findViewById(R.id.left_drawer); layout.getLayoutParams().width = getResources().getDisplayMetrics().widthPixels; layout.requestLayout(); expanded = true; } } @Override public void onDrawerOpened(View drawerView) { } @Override public void onDrawerClosed(View drawerView) { } @Override public void onDrawerStateChanged(int newState) { } } 

here is my layout:

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/application_drawer_layout" android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#ffffff"> <FrameLayout android:id="@+id/application_content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/left_drawer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="start" android:background="#000000" android:orientation="vertical"> <ListView android:id="@+id/application_left_drawer" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_gravity="start" android:background="#111" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </LinearLayout> </android.support.v4.widget.DrawerLayout> 
0
source

If you want to implement facebook as a sliding menu, then you should use SlidingPaneLayout androids instead of NavigationDrawer.

 <android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="250dp" android:layout_height="match_parent" android:background="#CC00FF00" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="match_parent" android:background="#CC0000FF" > // add toolbar and other required layouts </LinearLayout> </android.support.v4.widget.SlidingPaneLayout> 

Add a toolbar instead of an action bar and do not apply an action bar theme.

0
source

Source: https://habr.com/ru/post/958768/


All Articles