Stack Overflow Asked on December 18, 2021
The following code are from the project sun flower.
I add the code binding.toolbar.setTitle("My Label")
to HomeViewPagerFragment.kt.
But the title "My Label" doesn’t dsiplayed on the Toolbar, what’s wrong with my code?
HomeViewPagerFragment.kt
class HomeViewPagerFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentViewPagerBinding.inflate(inflater, container, false)
val tabLayout = binding.tabs
val viewPager = binding.viewPager
viewPager.adapter = SunflowerPagerAdapter(this)
// Set the icon and text for each tab
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
tab.setIcon(getTabIcon(position))
tab.text = getTabTitle(position)
}.attach()
binding.toolbar.setTitle("My Label") //I added, it deosn't effect
(activity as AppCompatActivity).setSupportActionBar(binding.toolbar)
return binding.root
}
...
}
fragment_view_pager.xml
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--
Note: even though the IDs for the CoordinatorLayout and the AppBarLayout unused in HomeViewPagerFragment, they are
both required to preserve the toolbar scroll / collapse state when navigating to a new screen and then coming back.
-->
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/Theme.Sunflower.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|snap"
app:toolbarId="@id/toolbar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="parallax">
<TextView
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:gravity="center"
android:text="@string/app_name"
android:textAppearance="?attr/textAppearanceHeadline5" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
<!-- Override tabIconTint attribute of style with selector -->
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
style="@style/Widget.MaterialComponents.TabLayout.Colored"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabIconTint="@drawable/tab_icon_color_selector"
app:tabTextColor="?attr/colorPrimaryDark"/>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Looking at your xml layout declaration you have used a TextView
inside your toolbar to display your title, whilst you're setting "My Label" on your toolbar. So for your fragment to have a title you should give the text view an id and set your title on the TextView
instead of toolbar:
fragment_view_pager.xml:
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="parallax">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:gravity="center"
android:text="@string/app_name"
android:textAppearance="?attr/textAppearanceHeadline5" />
</androidx.appcompat.widget.Toolbar>
And then in your HomeViewPagerFragment
:
binding.toolbarTitle.setText("My Label")
Answered by Sdghasemi on December 18, 2021
have you tried this-->
collapsingToolbarLayout.setTitleEnabled(false);
toolbar.setTitle("My Title");
Or
try adding in your collapsingtoolbarlayout-->
app:title="Title You Want"
Answered by Wini on December 18, 2021
Change to
(activity as AppCompatActivity).setSupportActionBar(binding.toolbar)
(activity as AppCompatActivity).supportActionBar!!.title = "My Label"
Answered by Công Hải on December 18, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP