Reputation: 2461
I have created a MainActivity
where I have a DrawerLayout
XML File
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/gray_95"
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp">
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="My toolbar"
android:textColor="@color/white"></EditText>
</androidx.appcompat.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
// ALL MY MAIN ACTIVITY XML
</RelativeLayout>
</LinearLayout>
<!-- Navigation View -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/drawer_menu"
android:layout_gravity="start"
android:background="@color/gray_95"
app:itemTextColor="@color/white"
app:itemIconTint="@color/white"
app:headerLayout="@layout/drawer_menu_header"/>
</androidx.drawerlayout.widget.DrawerLayout>
On this Activity
I have created the basic code that is required to making work the Drawer
which is called when from onCreate(Bundle savedInstanceState)
and overrided onNavigationItemSelected(@NonNull MenuItem item)
JAVA
private void setDrawerLayout() {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, mDrawerLayout , toolbar, R.string.open_drawer, R.string.close_drawer);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout.addDrawerListener(mToggle);
}
The Navigation
works, but I am dealing with an error. As the previous XML
file shows, I have added the whole Activity
under the Toolbar
. I am assuming that what I am doing wrong is that instead of the activity, I should create a Fragment
and add the following lines of code to onCreate
method:
getSupportFragmentManager().beginTransaction().replace(R.id.MYMAINFRAGMENT, new SettingFragment()).commit();
but before doing that, I wanted to confirm if this is the issue or not, since it would make me to spend a lot of time on it (the project is advanced, and I would need to make tons of changes)
I attach a picture to show the current solution
As you can see, as soon as I launch the application the back button is shown. When I click on it, it simply opens the drawer. What could be happening? Do I need to create a MAINFRAGMENT
?
Upvotes: 0
Views: 26
Reputation: 363677
Remove this line:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Upvotes: 1