0

I have recently created an app called Hoops and I have been developing it for quite some time now. However, the main problem that I seem to be getting, is that the navigation drawer's swipe margin is off screen and other users find it incredibly hard to use the navigation drawer's swipe feature. The menu button works just fine though. I have looked at other tutorials online and all of them seem to be showing the same answer as the one in the forum I have linked: Set drag margin for Android Navigation Drawer However, I have tried this solution, and it does not seem to be working for me. If anyone has any ideas why it is not working please can you help me out. Here is the coding for the Main activity with the navigation drawer below:

package com.jehan.sportstutorials;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBar actionBar;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    actionBar = getSupportActionBar();
    actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
    actionBar.setDisplayHomeAsUpEnabled(true);


    drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    if (navigationView != null) {
        setupNavigationDrawerContent(navigationView);
    }

    setupNavigationDrawerContent(navigationView);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            drawerLayout.openDrawer(GravityCompat.START);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

private void setupNavigationDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    textView = (TextView) findViewById(R.id.textView);
                    switch (menuItem.getItemId()) {
                        case R.id.item_navigation_drawer_basketball:
                            menuItem.setChecked(true);
                            textView.setText(menuItem.getTitle());
                            drawerLayout.closeDrawer(GravityCompat.START);
                            Intent i = new Intent(MainActivity.this, BasketballTutorial.class);
                            startActivity(i);
                            return true;
                        case R.id.item_navigation_drawer_help:
                            menuItem.setChecked(true);
                            textView.setText(menuItem.getTitle());
                            drawerLayout.closeDrawer(GravityCompat.START);
                            Intent intent2 = new Intent(MainActivity.this, HelpScreen.class);
                            startActivity(intent2);
                            return true;
                    }
                    return true;
                }
            });
}

}

activity_main.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"
android:id="@+id/navigation_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="@bool/fitsSystemWindows">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/status_bar_kitkat_height"
        android:background="?colorPrimary"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="?colorPrimaryDark"/>

</LinearLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/welcome"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textColor="@color/md_text"
        android:singleLine="false"
        android:padding="20dp"
        android:gravity="center"
        android:textStyle="bold"
        android:textIsSelectable="false" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom"
        android:text="@string/swipe"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textColor="@color/md_text"
        android:singleLine="false"
        android:padding="20dp"
        android:gravity="center"
        android:textSize="20sp"
        android:textStyle="italic"
        android:layout_marginBottom="30dp"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:background="?colorPrimaryDark"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:minHeight="?attr/actionBarSize"
        android:background="@drawable/action_bar_color"
        android:elevation="4dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ToolbarTheme"
        android:layout_marginTop="25dp"/>

</FrameLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="@bool/fitsSystemWindows"
    app:headerLayout="@layout/navigation_drawer_header"
    app:menu="@menu/navigation_drawer_menu"
    app:theme="@style/NavigationViewTheme" />

If anyone wants a live example of the navigation drawers swipe feature not working you can test out my app in the app store. Here is the link: https://play.google.com/store/apps/details?id=com.jehan.sportstutorials

Summary: Does anyone know how to increase the navigation drawers drag margin, however not following the conventional methods stated in the link in my first paragraph? Help would be appreciated.

Community
  • 1
  • 1
Jehan Dastoor
  • 647
  • 1
  • 8
  • 15

1 Answers1

0

Check your R.layout.activity_main layout. Android Studio IDE, for some reason, sets 15dp margin to the topmost container of automatically-created Activity layouts. This could interfere with the Drawer.

Just check if the topmost Relative/Frame/Whichever Layout has any android:layout_marginX attributes set and remove them.

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
  • My topmost layout does not have a layout_margin tag on it. I have updated the existing question by adding the activity_main.xml. Is there any other solution? – Jehan Dastoor Aug 07 '15 at 13:39
  • Please put the DrawerLayout xml file into your question – Kelevandos Aug 07 '15 at 13:40
  • My app does not have a DrawerLayout xml file. It uses the default navigation drawer layout. It uses a menu to display the different actions and a navigation_drawer_header.xml, which is used just for the header of the navigation drawer. Is this what you mean? – Jehan Dastoor Aug 07 '15 at 13:46
  • Yes, that is fine. Ok, lets try another thing - set a fixed value to the NavigationView layout_width param, try 240dp for example. Let me know if it worked. – Kelevandos Aug 07 '15 at 13:51
  • Sorry, it took me a while to figure out what you were saying since I am a beginner. Sad to say but it did not work. – Jehan Dastoor Aug 07 '15 at 14:22
  • Ok, another one then - the first LinearLayout, what is it for? Try removing it and see if the Drawer will start working properly, – Kelevandos Aug 07 '15 at 15:14