androidandroid-layoutnavigation-drawerhamburger-menudrawertoggle

Android Navigation drawer not displaying hamburger


Why is my navigation drawer in android not displaying hamburger icon. I have done everything as it is supposed to be done by it is still not getting displayed?

            public class Main extends ActionBarActivity {

            private String[] mPlanetTitles;
            private DrawerLayout mDrawerLayout;
            private ListView mDrawerList;
            private CharSequence mTitle;
            private ActionBarDrawerToggle mDrawerToggle;

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

                mTitle = "test";

                ArrayList<Integer> draw=new ArrayList();
                draw.add(R.drawable.elec);draw.add(R.drawable.mob);draw.add(R.drawable.auto);draw.add(R.drawable.pet);draw.add(R.drawable.est);

                mPlanetTitles = new String[]{"Electronic ads", "Mobile ads", "Automobile ads","Pets ads","Real Estate ads"};
                mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
                mDrawerList = (ListView) findViewById(R.id.left_drawer);
                Adapt c=new Adapt(this,draw);
                mDrawerList.setAdapter(c);

                DisplayMetrics displayMetrics =getApplicationContext().getResources().getDisplayMetrics();
                int height=(int)((300 * displayMetrics.density) + 0.5);


                GridView g=(GridView)findViewById(R.id.gridview);


                ImageAdapter a=new ImageAdapter(this,draw);
                g.setAdapter(a);
                g.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                     @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        startActivity(new Intent(Main.this, MainActivity.class));
                    }
                });


                // Set the adapter for the list view
            //    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                       // R.layout.drawer_list_item, mPlanetTitles));
                // Set the list's click listener
                mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        mDrawerList.setItemChecked(i, true);
                        //setTitle(mPlanetTitles[position]);
                        mDrawerLayout.closeDrawer(mDrawerList);
                    }
                });

                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) {
                        mDrawerToggle.syncState();
                        getSupportActionBar().setTitle(mTitle);
                        invalidateOptionsMenu();
                    }

                    /** Called when a drawer has settled in a completely open state. */
                    public void onDrawerOpened(View drawerView) {
                        mDrawerToggle.syncState();
                        getSupportActionBar().setTitle(mTitle);
                        mDrawerList.bringToFront();
                        invalidateOptionsMenu();
                    }

                };

                // Set the drawer toggle as the DrawerListener
                mDrawerLayout.setDrawerListener(mDrawerToggle);

                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);
                getSupportActionBar().setIcon(R.drawable.ic_drawer);


            }





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

            @Override
            protected void onPostCreate(Bundle savedInstanceState) {

                super.onPostCreate(savedInstanceState);
                mDrawerToggle.syncState();

                // Sync the toggle state after onRestoreInstanceState has occurred.

            }





            @Override
            public void onConfigurationChanged(Configuration newConfig) {
                super.onConfigurationChanged(newConfig);
                mDrawerToggle.onConfigurationChanged(newConfig);
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Pass the event to ActionBarDrawerToggle, if it returns
                // true, then it has handled the app icon touch event
                if (mDrawerToggle.onOptionsItemSelected(item)) {
                    return true;
                }
                // Handle your other action bar items...

                return super.onOptionsItemSelected(item);
            }

            /**
             * Swaps fragments in the main content view
             */
            private void selectItem(int position) {
                Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT).show();

                // Highlight the selected item, update the title, and close the drawer
                mDrawerList.setItemChecked(position, true);
                setTitle(mPlanetTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);
            }




            @Override
            public void setTitle(CharSequence title) {
                mTitle = title;
                getSupportActionBar().setTitle(mTitle);
            }

            private class DrawerItemClickListener implements ListView.OnItemClickListener {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    selectItem(position);
                }
            }

        }

This is my layout file.

           <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"
            tools:context=".MainActivity">

            <!-- The main content view -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            <GridView
                android:id="@+id/gridview"
                android:layout_width="match_parent"
                android:numColumns="3"
                android:gravity="center"
                android:stretchMode="columnWidth"
                android:verticalSpacing="10dp"
                android:horizontalSpacing="10dp"
                android:layout_height="match_parent"/>
            </RelativeLayout>
            <!-- The navigation drawer -->
            <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.support.v4.widget.DrawerLayout>

Why is icon hamburger not showing?


Solution

  • Also remember that, ActionBarActivity is Deprecated use: AppCompatActivity and about NavigationDrawer you should use NavigationView.