I am working on an android application and I spent a long time implementing a NavController with my BottomNavigationView. It works properly, I can transition between the different buttons but the content is empty. It only shows when I switch light to dark mode (of my device not the app) or vice versa in order for the content to start showing.
My home activity has 3 fragments that it calls home frag, search frag and profile frag.
The navigation bar works properly and I can transition between the buttons but the content only shows when I switch the current theme of my device.
It is not a problem of colors. I did try changing textColor and it did not work. It is also not an issue in themes.xml.
The preview of my home activity.xml shows the home fragment’s data showing in it so I think somewhere programatically, the text is gone and I just have no clue where.
HomeActivity.java
public class Home extends AppCompatActivity {
TextView tvSong, tvArtist;
ImageView ivCover, ivPlayButton;
ActivityHomeBinding binding;
MaterialCardView cardView;
BottomNavigationView bottomNavigationView;
NavController navController;
private static final int ANIMATION_DURATION = 150; // Duration in milliseconds
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
binding = ActivityHomeBinding.inflate(getLayoutInflater());
setContentView(R.layout.activity_home);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.homePage), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
tvSong = findViewById(R.id.tvSong);
tvArtist = findViewById(R.id.tvArtist);
ivPlayButton = findViewById(R.id.ivPlay);
cardView = findViewById(R.id.mcvCurrentSong);
bottomNavigationView = findViewById(R.id.bnBotNav);
final MutableLiveData<Boolean>[] musicPlaying = new MutableLiveData[]{new MutableLiveData<Boolean>()};
musicPlaying[0].setValue(true);
musicPlaying[0].observe(this, aBoolean -> {
if (Boolean.TRUE.equals(aBoolean)) {
cardView.setVisibility(View.VISIBLE);
cardView.setAlpha(0f);
cardView.animate()
.alpha(1f)
.setDuration(ANIMATION_DURATION)
.setListener(null);
ivPlayButton.setImageResource(R.drawable.ic_pause);
} else {
cardView.animate()
.alpha(0f)
.setDuration(ANIMATION_DURATION)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
cardView.setVisibility(View.GONE);
}
});
ivPlayButton.setImageResource(R.drawable.ic_play);
}
});
ivPlayButton.setOnClickListener(v -> {
if (Boolean.TRUE.equals(musicPlaying[0].getValue())) {
ivPlayButton.setImageResource(R.drawable.ic_pause);
musicPlaying[0].setValue(false);
} else {
ivPlayButton.setImageResource(R.drawable.ic_play);
musicPlaying[0].setValue(true);
}
});
final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navHostFragment);
assert navHostFragment != null;
navController = navHostFragment.getNavController();
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
}
ActivityHome.xml
<?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:id="@+id/homePage"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Home">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bnBotNav"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navgraph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bnBotNav"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@style/SCREEN"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
NAV GRAPH.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/navgraph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.synthshine_music.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_searchFragment"
app:popUpToInclusive="true"
app:destination="@id/searchFragment" />
<action
android:id="@+id/action_homeFragment_to_profileFragment"
app:popUpToInclusive="true"
app:destination="@id/profileFragment" />
</fragment>
<fragment
android:id="@+id/searchFragment"
android:name="com.example.synthshine_music.SearchFragment"
android:label="fragment_search"
tools:layout="@layout/fragment_search" >
<action
android:id="@+id/action_searchFragment_to_profileFragment"
app:popUpToInclusive="true"
app:destination="@id/profileFragment" />
<action
android:id="@+id/action_searchFragment_to_homeFragment2"
app:popUpToInclusive="true"
app:destination="@id/homeFragment" />
</fragment>
<fragment
android:id="@+id/profileFragment"
android:name="com.example.synthshine_music.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile" >
<action
android:id="@+id/action_profileFragment_to_homeFragment"
app:popUpToInclusive="true"
app:destination="@id/homeFragment" />
<action
android:id="@+id/action_profileFragment_to_searchFragment"
app:popUpToInclusive="true"
app:destination="@id/searchFragment" />
</fragment>
</navigation>
The first fragment to load is Home Fragment.java
ALL MY FRAGMENTS backend code is currently just inflating the layout and xml is just simple text and an image. nothing shows until the theme is switched.
package com.example.synthshine_music;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
Matthias Sunbreeze is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.