Reputation: 179
Hello ı want to use navigation drawer in android studio using kotlin. I am looking some documents and videos but before 2018 documents, system use onNavigationItemSelected method. But ı cannot see that method in android studio version 3.5. So, i cannot pass between fragments.
This is my mainactivity prepared by android studio:
package com.example.myapplication;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
Upvotes: 1
Views: 1354
Reputation: 182
I have done transaction like this.
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.nav_home) {
getSupportActionBar().setTitle("Home");
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, menuFragment).commit();
} else if (id == R.id.nav_profile) {
profileFragment = new ProfileFragment();
//profileFragment.setArguments(bundle);
getSupportActionBar().setTitle("Profile");
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, profileFragment).commit();
}
else if (id == R.id.nav_logout) {
showDialogg("Do you want to Logout?", 2, "Logout");
return true;
}
try {
// ft.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
Upvotes: 0
Reputation: 447
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull
NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_home) {
Toast.makeText(context, "home", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.nav_gallery) {
Toast.makeText(context, "nav_gallery", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.nav_slideshow) {
Toast.makeText(context, "nav_slideshow", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.nav_tools) {
Toast.makeText(context, "nav_tools", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.nav_share) {
Toast.makeText(context, "nav_share", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.nav_send) {
Toast.makeText(context, "nav_send", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.logout) {
Toast.makeText(context, "logout", Toast.LENGTH_LONG).show();
}
}
Upvotes: 0
Reputation: 91
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
Upvotes: 3