1

I need the Navigation drawer icon to be displayed in the top right corner of the activity. I have included the code which is not working properly. I am using android Studio 3.5.3, newest version

enter image description here

Java code:

  package com.example.myapplication;
  import android.os.Bundle;
  import com.google.android.material.floatingactionbutton.FloatingActionButton;
  import com.google.android.material.snackbar.Snackbar;
  import android.view.View;
  import androidx.navigation.NavController;
  import androidx.navigation.Navigation;
  import androidx.navigation.ui.AppBarConfiguration;
  import androidx.navigation.ui.NavigationUI;
  import com.google.android.material.navigation.NavigationView;
  import androidx.drawerlayout.widget.DrawerLayout;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.appcompat.widget.Toolbar;
  import android.view.Menu;

  public class MainActivity extends AppCompatActivity {

  private AppBarConfiguration mAppBarConfiguration;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();

}

@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;
 }
}

I have made some changes to Java and XML code. I am a beginner to Android Studio. Please help me to correct the mistake.

Here is the XML code

   <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     tools:showIn="@layout/app_bar_main">
  </RelativeLayout>
Edric
  • 24,639
  • 13
  • 81
  • 91
  • Welcome to StackOverlow! As your question is currently written, it is a little unclear as to what you are trying to achieve. Please elaborate on what you already tried to solve your problem and where you failed. Furthermore, please try to reduce your code sample to a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and take a look at [this help page](https://stackoverflow.com/help/how-to-ask). Cheers :) – vatbub Dec 26 '19 at 17:47
  • to avoid all issues, create a new project with Navigation Drawer Activity: **Android Studio -> File -> New - > New Project -> Navigation Drawer Activity** and click **Next** – Mouaad Abdelghafour AITALI Dec 26 '19 at 17:50
  • You made plethora of mistakes in Java and XML code. Try to create new project and check – Md. Asaduzzaman Dec 26 '19 at 18:40
  • Does this answer your question? [Navigation Drawer Icon (ic\_drawer) not showing](https://stackoverflow.com/questions/20385101/navigation-drawer-icon-ic-drawer-not-showing) – Edric Dec 27 '19 at 08:26

1 Answers1

2

Try to add this:

supportActionBar?.setDisplayHomeAsUpEnabled(true)

in Android:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

You may have a problem opening the menu. After that add this: (in kotlin)

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    super.onOptionsItemSelected(item)
    return when (item.itemId) {
        android.R.id.home -> {
            drawer_layout.openDrawer(GravityCompat.START)
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
}
EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
iiec
  • 21
  • 6