3

EDIT: I have found a solution. I created separate activity, including another toolbar inside that activity. Then, I created the second navigation graph which I added to the second activity. I set up the second toolbar with the NavController using the same method I used for my MainActivity. It worked just as I wanted!

Original post: I am using the new navigation component with a single Activity approach in the app. I want to set ToolBar style per Fragment keeping the navigation controller functionality (like navigation up and titles).

Initially, I tried searching for how to inflate Fragments with an alternative theme. I rejected this idea because

  • it did not really work

  • I only need to theme the Toolbar. URL

I have found this project that does it with Kotlin. However, it uses BottomNavigation which is not present in my app. https://android.jlelse.eu/a-quick-glance-on-single-activity-approach-with-navigation-component-f3b96b3b0a58

From that project:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragHost"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/nav_graph"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:defaultNavHost="true" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:orientation="horizontal"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        navigation.setupWithNavController(findNavController(R.id.fragHost))
    }

override fun onSupportNavigateUp() = findNavController(R.id.fragHost).navigateUp()

FirstFragment.kt

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        collapsingToolbar.setupWithNavController(toolbar, findNavController())
    }

SecondFragment.kt

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        changeStatusBarColor(R.color.colorBlue)
        return inflater.inflate(R.layout.fragment_second, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        toolbar.setupWithNavController(findNavController())
    }

    override fun onDestroyView() {
        super.onDestroyView()

        changeStatusBarColor(R.color.colorPrimaryDark)
    }

    private fun changeStatusBarColor(colorResId: Int) {
        activity?.window?.statusBarColor = ContextCompat.getColor(context!!, colorResId)
    }

So far, I have been able to find how to set up Navigation controller with Toolbar once in MainActivity. This allows for correct navigation up action but does not allow custom themes. Reference: https://developer.android.com/guide/navigation/navigation-ui#top_app_bar

In my MainActivity.java, I did the following:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Toolbar toolbar = findViewById(R.id.toolbar);
        final int[] topDestination = { R.id.loginFragment, R.id.mainFragment };
        AppBarConfiguration.Builder abcBuiler = new AppBarConfiguration.Builder(topDestination);
        AppBarConfiguration abc = abcBuiler.build();
        NavController navController = Navigation.findNavController(this, R.id.fragHost);
        // Set up navigation controller toolbar
        NavigationUI.setupWithNavController(toolbar, navController, abc);
}

toolbar.setupWithNavController(findNavController()) function is not present in Java or at least I was not able to find it.

NavigationUI.setupWithNavController(toolbar, navController, AppBarConfiguration); sets up the Toolbar once in MainActivity and I cannot find a way how to theme it inside fragments.

0 Answers0