I'm having a fragment overlapping error when I try to change them programmatically.
Attached image where you can see that there is a fragment above another (I clarify that the fragment that is above is a chat, it can be used but the one below is only as a background and is not functional)
I attach the code of the activity that contains the navigation drawer, which is responsible for detecting the selection of the nav_items
NavigationView nv = findViewById(R.id.nav_view);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem){
int menu_id = menuItem.getItemId();
Fragment fragment = null;
FragmentManager fragmentManager = getSupportFragmentManager();
switch(menu_id) {
case R.id.nav_inicio:
fragment = new fragment_inicio();
Toast.makeText(MainActivity.this, "inicio", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_perfil:
fragment = new fragment_perfil();
Toast.makeText(MainActivity.this, "PERFIL", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_chat:
fragment = new fragment_chat();
Toast.makeText(MainActivity.this, "CHAT", Toast.LENGTH_SHORT).show();
}
fragmentManager.beginTransaction().replace(R.id.nav_host_fragment, fragment).commit();
return false;
}
});
I attach the XML code of content_main (layout that Android Studio creates by default when creating a Navigation Drawer type app)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
android:contentDescription="@string/ayuda">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation"
android:contentDescription="@string/ayuda"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Edit: Java code in which I get an error inside the MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = findViewById(R.id.drawer_layout);
nv = findViewById(R.id.nav_view);
nombree = findViewById(R.id.et_ciudad);
abc = new AppBarConfiguration.Builder(R.id.nav_inicio, R.id.nav_perfil,
R.id.nav_chat, R.id.nav_buscar, R.id.nav_mis_clases, R.id.nav_ayuda,
R.id.nav_configuracion)
.setDrawerLayout(drawer).build();
//LÍNEA EN LA CUAL OBTENGO ERROR
nc = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(nv, nc);
nv.setCheckedItem(R.id.nav_inicio);
nv.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
.
.
.}
Thank you for reading
You must first change the return value of the listener to
true
, since this boolean value determines whether the event is consumed or not:Now regarding why you still see a
Fragment
below and when you made the transaction of theFragment
.I suggest that instead of defining a you
Fragment
use aFrameLayout
which would be where you would replace the Fragments: