Set up toolbar theming per Fragment with NavController in JavaSet theme for a FragmentHow to convert an Array to a Set in JavaForce close onClick of ImageButton, can't figure out whyGoogle map API v2Android Toolbar Not Calling onOptionsItemSelected From Fragments On BackstackHow to enable toolbar controls after open navigation view?Fragment onCreateView method not called in TabLayout with ViewPagerAdapterWeird OutOfMemoryError when loading view pagerRecycler Item view not properly displaying dataUse Activity's toolbar to Navigate up from Fragment

Does French have the English "short i" vowel?

Need to read my home electrical Meter

How can I make an argument that my time is valuable?

What's difference between "depends on" and "is blocked by" relations between issues in Jira next-gen board?

Why did other houses not demand this?

Mercedes C180 (W204) dash symbol

Python program for a simple calculator

Function argument returning void or non-void type

Should there be an "a" before "ten years imprisonment"?

Is superuser the same as root?

What are Antecedent & Consequent Phrases in Music?

Find this cartoon

Can I tell a prospective employee that everyone in the team is leaving?

How to deal with a colleague who is being aggressive?

Can my floppy disk still work without a shutter spring?

Drums and punctuation

Why do we need to chain the blocks (creating blockchain) in a permissioned blockchain?

If a (distance) metric on a connected Riemannian manifold locally agrees with the Riemannian metric, is it equal to the induced metric?

What is the meaning of "<&3" and "done < file11 3< file22"

Why are Stein manifolds/spaces the analog of affine varieties/schemes in algebraic geometry?

Are black holes spherical during merger?

Natural Armour and Weapons

How to politely tell someone they did not hit "reply to all" in an email?

Why does Bran want to find Drogon?



Set up toolbar theming per Fragment with NavController in Java


Set theme for a FragmentHow to convert an Array to a Set in JavaForce close onClick of ImageButton, can't figure out whyGoogle map API v2Android Toolbar Not Calling onOptionsItemSelected From Fragments On BackstackHow to enable toolbar controls after open navigation view?Fragment onCreateView method not called in TabLayout with ViewPagerAdapterWeird OutOfMemoryError when loading view pagerRecycler Item view not properly displaying dataUse Activity's toolbar to Navigate up from Fragment






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















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



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.










share|improve this question






























    0















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



    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.










    share|improve this question


























      0












      0








      0








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



      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.










      share|improve this question
















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



      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.







      java android android-toolbar android-theme android-architecture-navigation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 6 at 1:39







      Artem Ilyumzhinov

















      asked Mar 23 at 20:45









      Artem IlyumzhinovArtem Ilyumzhinov

      15




      15






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55318204%2fset-up-toolbar-theming-per-fragment-with-navcontroller-in-java%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55318204%2fset-up-toolbar-theming-per-fragment-with-navcontroller-in-java%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

          밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

          1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴