Issue
I am trying to add a hamburger to my toolbar but nothing is working. I know there are several StackOverflow posts already about this topic, but after trying almost all of the methods mentioned in those posts I still didn't find anything that made the hamburger icon appear. Please let me know if there is something wrong with my code, and if you know how to display the hamburger icon.
HomePage.class
package com.example.movieapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.navigation.NavigationView;
public class HomePage extends AppCompatActivity implements NavigationBarView.OnItemSelectedListener {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this::onNavigationItemSelected);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
toggle.syncState();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()) {
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
// Intent intent = new Intent(getApplicationContext(),HomePage.class);
// startActivity(intent);
break;
case R.id.nav_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
break;
case R.id.nav_cart:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new CartFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
Solution
Found out that the problem wasn't in my the .class file but instead it was in the XML file. The toolbar layout was set to light, so the hamburger icon was white and my background was also white. So it was displaying but it was just not able to seen.
Answered By - Malik Safwan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.