I would like to program a Toolbar with Navigation Drawer that appears on all activities of my app. To do so, I have created an abstract class "BaseActivity" which is then extended by all other activities.
I have succeeded in creating a Toolbar that appears on all activities. Here is a screenshot of what that looks like:
However, when I try to add a Navigation Drawer to that Toolbar, then the contents of the activities that are extending BaseActivity.java do not appear.
I am therefore wondering what I am doing wrong with the implementation of the Navigation Drawer for that to happen. Here is the layout of activity_base.xml
<?xml version="1.0" encoding="utf-8"?>
<merge 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="wrap_content" >
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:title="@string/app_name"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/nav_menu"
android:id="@+id/nav_view"/>
</androidx.drawerlayout.widget.DrawerLayout>
</merge>
And here is the java code for "BaseActivity.java":
package com.example.newsreader;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public abstract class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected boolean useToolbar() {
return true;
}
@Override
public void setContentView(int layoutResID) {
View view = getLayoutInflater().inflate(layoutResID, null);
configureToolbar(view);
super.setContentView(view);
//create NavigationDrawer:
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.open, R.string.closed);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void configureToolbar(View view) {
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
if (toolbar != null) {
if (useToolbar()) {
//loads toolbar and calls onCreateOptionsMenu
setSupportActionBar(toolbar);
} else {
toolbar.setVisibility(View.GONE);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//inflate menu items for use in action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
String message = null;
//Look at your menu XML file. Put a case for every id in that file:
switch(item.getItemId())
{
//what to do when the menu item is selected:
case R.id.item1:
message = "You clicked on item 1";
break;
case R.id.item2:
message = "You clicked on item 2";
break;
case R.id.item3:
message = "You clicked on item 3";
break;
case R.id.item4:
message = "You clicked on the overflow menu";
break;
}
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return true;
}
// Needed for the OnNavigationItemSelected interface:
@Override
public boolean onNavigationItemSelected( MenuItem item) {
String message = null;
switch(item.getItemId())
{
case R.id.item1:
Intent chatPage = new Intent(this, NewsHeadlines.class);
startActivity(chatPage);
break;
case R.id.item2:
Intent weatherForecastPage = new Intent(this, WeatherForecast.class);
startActivity(weatherForecastPage);
break;
case R.id.item3:
setResult(500);
finish();
break;
}
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}
}
Any suggestions as to what I'm doing wrong?