In the following activity, i have a fragment and an image on it. The Fragment is just a darker Action Bar that has a picture on it. I\'m trying to have a left slide menu as
Step - 1 add lines into app level Build.gradle
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.navigation:navigation-fragment:2.3.0'
implementation 'androidx.navigation:navigation-ui:2.3.0'
implementation 'com.google.android.material:material:1.1.0'
Step - 2 file add below code into activity_main.xml
Step - 3 add below code into layout/app_bar_main.xml
Step - 4 add below code into layout/nav_header_main2.xml
Step - 5 add below code into menu/activity_main_drawer2.xml
Step - 6 add below code into styles.xml
Step - 7 add below code into layout/content_main2.xml
<\RelativeLayout>
Step - 8 add below code into AndroidManifest.xml
Step - 9 add below code into Home2Activity.java
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class Home2Activity extends AppCompatActivity {
DrawerLayout drawer2;
DrawerLayout.DrawerListener drawerListener2;
NavigationView navigationView2;
Toolbar toolbar2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
allocatememory();
setSupportActionBar(toolbar2);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_launcher_background); // set drawable icon
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Home");
sliderDrawer();
}
private void allocatememory() {
toolbar2 = findViewById(R.id.toolbar);
drawer2 = findViewById(R.id.drawer_layout2);
navigationView2 = findViewById(R.id.nav_view2);
}
private void sliderDrawer() {
View headerView = navigationView2.getHeaderView(0);
TextView navUsername = (TextView) headerView.findViewById(R.id.txt_user_name_drawer);
navUsername.setText("User Name2");
drawerListener2 = new ActionBarDrawerToggle(this, drawer2, toolbar2, R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
};
View headerview = navigationView2.getHeaderView(0);
headerview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawer2.close();
}
});
navigationView2.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.nav_profile: {
startActivity(new Intent(Home2Activity.this, TestActivity.class));
}
break;
case R.id.nav_share: {
//ShareApp();
}
break;
case R.id.nav_orders: {
startActivity(new Intent(Home2Activity.this, TestActivity.class));
}
break;
case R.id.nav_log_out: {
//storage.write("id", -1);
Toast.makeText(Home2Activity.this, "Logged out successfully", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Home2Activity.this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finishAffinity();
}
break;
case R.id.nav_about_us: {
Intent intent = new Intent(Home2Activity.this, TestActivity.class);
intent.putExtra("where", 1);
startActivity(intent);
}
break;
case R.id.nav_terms_and_conditions: {
Intent intent = new Intent(Home2Activity.this, TestActivity.class);
intent.putExtra("where", 2);
startActivity(intent);
}
break;
}
return false;
}
});
}
}