im very new to android development and i am trying to implement a hamburger button drawer with a few different tabs. here is my code for the different components. I have tried everythignn i could think of and have been banging my head on the computer for about 3 hours trying to figure this out.
main activity.java
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration appBarConfiguration;
private ActivityMainBinding binding;
DrawerLayout drawerLayout;
NavigationView navigationView;
NavController navController;
NavHostFragment navHostFragment;
ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(R.layout.activity_main);
setSupportActionBar(findViewById(R.id.toolbar));
//get the drawer, navigation vew and navcontroller
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
navHostFragment =(NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_fragment_view);
assert navHostFragment != null;
navController = navHostFragment.getNavController();
assert navController != null;
//set up the action bar
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
.setOpenableLayout(drawerLayout)
.build();
//NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//set up the navigation view
//NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(item -> {
int id = item.getItemId();
if(id == R.id.Home_navigation_button){
navController.navigate(R.id.Home);
}
else if (id == R.id.ReservationPage_navigation_button){
navController.navigate(R.id.ReservationPage);
}
drawerLayout.close();
return true;
});
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
String label = destination.getLabel().toString();
getSupportActionBar().setTitle(label);
});
}
@Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
activity main.xml
<?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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/navigation_fragment_view"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/navigation_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/grey"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Header"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:padding="16dp"
tools:context=".Home"
android:background="@color/black"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/Home_to_ReservationPage_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="@string/ReservationPage_label"
android:textColor="@color/white"
app:icon="@android:drawable/ic_menu_myplaces" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Home" />
</TableRow>
</TableLayout>
</FrameLayout>
home.java
package com.example.parkstreet.cms.homeapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import com.example.parkstreet.cms.homeapp.databinding.HomeBinding;
public class Home extends Fragment {
private HomeBinding binding;
@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
)
{
binding = HomeBinding.inflate(inflater, container, false);
return binding.getRoot();
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.HomeToReservationPageButton.setOnClickListener(v ->
Navigation.findNavController(view).navigate(R.id.ReservationPage)
);
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
the drawer seems to work as the name of the page shows on the top of the toolbar but the actual page is just plain white.
i have tried everything from changing the layout width of every layer of the tree from the button on the home page to the constrained layout box. i dont see why its always invisible regardless if on the reservation page or home page.
Lalo Riojas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.