简体   繁体   中英

Android Navigation Drawer three line hamburger icon

I'm going crazy trying to figure out what I'm doing wrong and tried all the solutions on SO but still seem to be missing something. This is the first time I am creating a navigation drawer and took the help of an online tutorial.

Everything works great, except the icon shows the back arrow instead of the three lines.

The first bunch of answers deal with the v4 library tied to the drawable icon which didn't apply to me since I am using the v7 library On the v7, the first answer I tried was to use below, but still remained as arrow.

 @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            drawerToggle.syncState();
        }

I also tried this trick, but same result:

drawerLayout.post(new Runnable() {
            @Override
            public void run() {
                drawerToggle.syncState();
            }

I then decided to give up and try to just force the icon and downloaded the ic_drawer icon and used below to just set it and be done, but still no go:

 toolbar.setNavigationIcon(R.drawable.ic_drawer);

I then went so far as to remove everything tied to making the icon to start from scratch, but even after making my code just as below, the back arrow still persists and clicking it opens the navigation drawer:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawerLayout=(DrawerLayout) findViewById(R.id.navDrawer);
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.nav_open, R.string.nav_close) {
};

Even if I comment out the toolbar and that drawer toggle to have nothing in my onCreate, I am now still stuck with that back arrow. The navigation drawer still works great and opens when I click on it. I just don't know where that back arrow is coming from so I can kill it and try to work back the other code to get the three lines to show up.

Any suggestion would be greatly appreciated!

Below is the code in full. With all the navigation drawer stuff commented out, I can still see the back arrow, and pressing it or swiping opens the fragment perfectly. If I take the comments off, its still the same thing

public class Main extends AppCompatActivity {


    public ArrayList<String> strTrips = new ArrayList<String>();
    ArrayAdapter<String> adpTrips;
    ActionBarDrawerToggle drawerToggle;

    //String[] strDefaultList={"Click the '+' on the toolbar to add a new trip","","Once you have at least one trip, you select it to view/enter information"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Initialize toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
        //toolbar.setNavigationIcon(R.drawable.ic_drawer);
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);






       /* DrawerLayout drawerLayout=(DrawerLayout) findViewById(R.id.navDrawer);
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.nav_open, R.string.nav_close) {
          @Override
           public void onDrawerOpened(View drawerView) {
               super.onDrawerOpened(drawerView);
               //getActivity().invalidateOptionsMenu();
               //drawerToggle.syncState();
           }

           @Override
           public void onDrawerClosed(View drawerView) {
               super.onDrawerClosed(drawerView);
               //getActivity().invalidateOptionsMenu();
              // drawerToggle.syncState();
           }


       };*/

    /*    drawerLayout.addDrawerListener(drawerToggle);
*/
        //drawerToggle.syncState();
       /* drawerLayout.post(new Runnable() {
            @Override
            public void run() {
                //drawerToggle.syncState();
            }
        });*/




        subLoadTrips();

    }

/*
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        //drawerToggle.syncState();
    }
*/

The manifest file is below. The activity where I am adding the NavigationDrawer is 'Main'.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dimbu.tripx"
    android:versionCode="10"
    android:versionName="1.0">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Main">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--
             ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
        -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity android:name=".AddTrip">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".Main" />

        </activity>
    </application>

</manifest>

The answer was provided by Mike M in comments.

With all the code being correct, his suggestion to clean/rebuild the project worked and then the changes took effect!

This one line code should help you. 😎☺

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

toolbar is not your app toolbar here!

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 toolbar.setNavigationIcon(R.drawable.ic_drawer);

It's just a view. Instead of this you should get current app toolbar:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 getSupportActionBar().setIcon(R.drawable.ic_drawer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM