Android Question

Navigation Drawer not showing at all (android)

I followed a fairly simple guide as to add a navigation drawer, but the drawer isn't there when running the app, and the layouts for the main screen and the drawer are both being displayed in the main avtivity, overlapping. ://

Any ideas?

EDIT: misspelled an attribute... Wrote "android:gravity", not "android:layout_gravity" feeling stupid :P

Here's the xml layout:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kapres.test2.MainActivity">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/main_screen"
android:scaleType="centerCrop"/>

<TextView
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test2"
android:textSize="38dp"
android:paddingTop="32dp"/>

</RelativeLayout>

<ListView
android:id="@+id/main_menu_layout"
android:layout_width="200dp"
android:layout_height="match_parent"
android:gravity="left|start">

</ListView>

</android.support.v4.widget.DrawerLayout>


Here's the java code:

package com.kapres.test2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

ArrayList<String> mainMenuList = new ArrayList<String>();

mainMenuList.add("item1");
mainMenuList.add("item2");
mainMenuList.add("item3");
mainMenuList.add("item4");
mainMenuList.add("item5");

ArrayAdapter mainMenuAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mainMenuList);
ListView listView = (ListView) findViewById(R.id.main_menu_layout);
listView.setAdapter(mainMenuAdapter);
}
}

Answer

Here's the layout for fully working drawer. Pay attention to android:layout_gravity="start" attribute. If you remove that line, the drawer would always be opened. This is useful for editing.

<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">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->

<LinearLayout
    android:id="@+id/drawer_content"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:background="@color/background">

</LinearLayout>

DrawerActivity:

// 1. Set content view
    setContentView(R.layout.app_drawer);
    // 2. Set drawer layout
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {

        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Update user data end etc.
            loadUserDataOntoDrawer();
        }

        @Override
        public void onDrawerClosed(View drawerView) {

        }

        @Override
        public void onDrawerStateChanged(int newState) {

        }
    });
    mDrawerContent = (LinearLayout) findViewById(R.id.drawer_content);

    // Initialize your data adapter
    mMenuDataAdapter = new MenuDataAdapter(MenuHolder.getInstance().getMenuItemList());
    // Find recyclerview
    mRecyclerView = (RecyclerView) findViewById(R.id.left_drawer);
    // Define layout manager
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    // Set adapter
    mRecyclerView.setAdapter(mMenuDataAdapter);
    // Set animator
    mRecyclerView.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f)));