0

I want to display navigation indicator for a drawer like in this image:

enter image description here

For some reason the indicator does not display. I spent many hours on google and SO and no solution worked for me.

Manifest:

android:theme="@style/Theme.AppCompat.Light"

Gradle:

compile 'com.android.support:appcompat-v7:23.1.1'

Layout (without parent):

<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">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:id="@+id/puzzleScreen"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".activities.PuzzleActivity">

    <lelisoft.com.lelimath.view.TilesView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tiles"/>
</LinearLayout>

<ListView
    android:id="@+id/navList"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#ffeeeeee"/>
</android.support.v4.widget.DrawerLayout>

Activity:

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;

public class PuzzleActivity extends AppCompatActivity {

protected void onCreate(Bundle state) {
    super.onCreate(state);
    mDrawerList = (ListView)findViewById(R.id.navList);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();
    addDrawerItems();
    setupDrawer();

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle("Navigation!");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(mActivityTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

 //   getSupportActionBar().setHomeButtonEnabled(true);
 //   getSupportActionBar().setDisplayShowHomeEnabled(true);

    mDrawerToggle.syncState();
    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

private void addDrawerItems() {
    String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
    mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, osArray);
    mDrawerList.setAdapter(mAdapter);

    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(PuzzleActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
        }
    });
}

protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

Where is the issue? I tried Nexus 4 with Android 5 and Nexus 5x with Android 6.

UPDATE:

I have downloaded Android navigation drawer sample, it displayed the drawer in the second activity. Then I copied relevant parts to main activity - and the indicator disappeared. So it seems to be related that my activity has no parent.

Leos Literak
  • 8,805
  • 19
  • 81
  • 156

1 Answers1

0

It looks like you are missing the icon when creating your ActionBarDrawerToggle. See: http://developer.android.com/intl/es/training/implementing-navigation/nav-drawer.html#ActionBarIcon

 mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
            ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getActionBar().setTitle(mTitle);
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActionBar().setTitle(mDrawerTitle);
        }
    };
  • This was obsoleted. See V7 javadoc for current constructor signature: http://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html – Leos Literak Jan 07 '16 at 21:35
  • Correct, but I think this is the only way to get it to work if you are using an action bar and not a toolbar. I could be wrong though.. Before upgrading to toolbar the above approach is what I used. –  Jan 07 '16 at 21:39
  • It seems to be related to fact that this activity has no parent. I tried google sample and the indicator disappeared when I set the drawer to a top level activity. – Leos Literak Jan 08 '16 at 20:47