FragmentTabHost with HorizontalScrollView - android

I'm trying to add HorizontalScrollView to FragmentTabHost. I was following 'Lore Giver' answer from FragmentTabHost with horizontal scroll
but its not working for me.
After implementing xml like this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.app.FragmentTabHost
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget
android:id="#+id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</HorizontalScrollView>
<FrameLayout
android:id="#+id/tabHostContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="48dp" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
And setting it up like this :
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.home_tabs, container, false)
viewContext = view.context
tabHost = view.findViewById(R.id.tabHost)
tabHost.setup(view.context, fragmentManager, R.id.tabHostContent)
tabHost.addTab(tabHost.newTabSpec("home").setIndicator(context!!.getString(R.string.TAB_EMERGENCY)), EmergencyFragment::class.java, null)
tabHost.addTab(tabHost.newTabSpec("friends").setIndicator(context.getString(R.string.TAB_PRIVATE)), PrivateFragment::class.java, null)
tabHost.addTab(tabHost.newTabSpec("organizations").setIndicator(context!!.getString(R.string.ORGANIZATIONS)), OrganizationsFragment::class.java, null)
tabHost.tabWidget.dividerDrawable = null
tabHost.tabWidget.isStripEnabled = false
changeStyleOfSelectedTab()
tabHost.setOnTabChangedListener {
changeStyleOfSelectedTab()
(activity as MainActivity).tabChangedByClick(currentTab)
}
return view
}
my tabs were not scrolling horrizontaly. The last Title ("Organizations") has just wrapped itself (it was not fitting on screen).
After adding
tabHost.tabWidget.getChildAt(i).findViewById<TextView>(android.R.id.title).setSingleLine(true)
for each of the tabs, organization TITLE started scrolling inside its tab. What am I doing wrong ?
Bar after adding setSingleLine

Related

Why is my merge layout showing up twice inside LinearLayout?

I have a merge layout inside a parent LinearLayout. For some reason, the merge layout is showing up twice, one on top of the other, in the fragment when I run my app. Layout Inspector shows all the elements from the merge layout repeated below the first set, and all directly under the LinearLayout. Even weirder is that the repeated elements have the same id's.
Parent layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/child_layout" />
</LinearLayout>
Child layout:
<merge
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stuff" />
<Button
android:id="#+id/positiveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/negativeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</merge>
Fragment code:
private var _binding: ParentBinding? = null
private val binding get() = _binding!!
private var _contentBinding: ChildBinding? = null
private val contentBinding get() = _contentBinding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = ParentBinding.inflate(layoutInflater)
_contentBinding = ChildBinding.inflate(layoutInflater, binding.root)
return binding.root
}
What's Happening?
You are inflating the parent layout 2 times in onCreateView.
Solution
Replace this: _contentBinding = ChildBinding.inflate(layoutInflater, binding.root)
With this: _contentBinding = ChildBinding.bind(binding.root)

Getting DrawerLayout must be measured with MeasureSpec.EXACTLY when adding the drawer layout

I am trying to add drawerlayout in my fragment home. I am getting java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY. this error when I tried to attach drawer layout to my fragment.
I have root layout as constraint layout. Firstly I tried with root layout as drawerlayout still I wast getting this exception so I tried adding constraint layout to it but did not work.
It works when I give the hard coded value to height and width as 500dp.
But hard coded value cant be given as it will not be good practice for all devices size it will differ.
Below is my code
drawer layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/drawer"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/fragment_home"
android:visibility="visible" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/nav_header"
layout="#layout/nav_header" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadeScrollbars="true"
android:scrollbars="none">
<include
android:id="#+id/menuItems"
layout="#layout/layout_navigation_menu" />
</ScrollView>
</LinearLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Home fragment layout
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fillViewport="true"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include android:id="#+id/app_bar"
layout="#layout/app_bar_main"/>
<RelativeLayout
android:id="#+id/relative_accept"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_55"
android:orientation="horizontal"
android:background="#color/green_500"
app:layout_constraintTop_toBottomOf="#id/app_bar">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:textColor="#color/white"
android:layout_marginLeft="#dimen/margin_16"
android:layout_marginStart="#dimen/margin_16"
android:layout_marginRight="#dimen/margin_16"
android:layout_marginEnd="#dimen/margin_16"
android:text="#string/accepting_orders"
android:fontFamily="#font/roboto_regular"
android:textSize="#dimen/text_size_15"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content" />
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:track="#drawable/switch_track_selector"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
tools:ignore="UseSwitchCompatOrMaterialXml" />
</RelativeLayout>
<FrameLayout
android:id="#+id/frameLayout_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/relative_accept">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_marginStart="#dimen/margin_16"
android:layout_marginLeft="#dimen/margin_16"
android:layout_marginTop="#dimen/margin_20"
android:layout_marginEnd="#dimen/margin_16"
android:layout_marginRight="#dimen/margin_16"
android:background="#drawable/grey_drawable"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
android:onClick="#{(v)->click.onClick(v)}"
app:tabBackground="#drawable/tab_selector"
app:tabSelectedTextColor="#color/white" />
</FrameLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="#+id/frameLayout_tab" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Home fragment
class HomeFragment : BaseFragment() , View.OnClickListener{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.drawer_layout_navigation_view, container, false)
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
init()
controlInit()
}
override fun init() {
val toggle = ActionBarDrawerToggle(
(activity as MainActivity),
drawer,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
drawer.addDrawerListener(toggle)
tabs.setupWithViewPager(view_pager)
bindViewPagerAdapter(view_pager, (activity as MainActivity))
bindViewPagerTabs(tabs, view_pager)
tabSettings()
}
private fun tabSettings() {
tabs.bringToFront()
view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrolled(
position: Int, positionOffset: Float, positionOffsetP: Int
) {
}
override fun onPageSelected(position: Int) {}
override fun onPageScrollStateChanged(i: Int) {}
})
}
private fun bindViewPagerAdapter(view: ViewPager?, mainActivity: MainActivity) {
val adapter = OrdersAdapter(view?.context!!, childFragmentManager)
view.adapter = adapter
}
private fun bindViewPagerTabs(view: TabLayout, pagerView: ViewPager?) {
view.setupWithViewPager(pagerView, true)
}
private fun controlInit() {
imageView_drawer.setOnClickListener(this)
}
#SuppressLint("WrongConstant")
override fun onClick(p0: View) {
when (p0.id) {
R.id.imageView_drawer -> {
drawer.openDrawer(Gravity.START)
}
}
}
companion object {
#JvmStatic
fun newInstance() =
HomeFragment().apply {}
}
}
Please help with this.Thank you....
Your layout actually worked just fine when I tested it so I am not sure if the issue lies with the layout. However your layout files are overly complex, you don't really need any of the nested layouts that you have and can achieve the same results without. ConstraintLayout was developed with the purpose of simplifying layout hierarchies so you should be able to just use that to organise your child views.
Also the NavigationView component is meant to be used to display a menu from a menu resource, not just to be used as a wrapper for your own. Either choose to display your own menu completely or use their implementation as intended. There is some nice documentation for this on the [Material Design website] here.
So may I suggest you try to cut down on nesting your layouts and then see whether this incidentally fixes your problem.
The hierarchy that you might want would probably look something like this:
drawer_layout.xml
<DrawerLayout>
<include layout="#layout/home" />
<NavigationView />
</DrawerLayout>
home.xml
<ScrollView>
<ConstraintLayout>
<include layout="#layout/app_bar_main"/>
<TextView />
<Switch />
<TabLayout />
<ViewPager />
</ConstraintLayout>
</ScrollView>

Android layout design that have two options depending on the data

I would like to display either 4 textViews or just one editText depending on the data that I receive from back-end
Here is my view as below
<LinearLayout 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"
android:orientation="vertical">
<LinearLayout
android:id="#+id/groupOfFour"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="#+id/second"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="#+id/third"
android:layout_width="match_parent"
android:layout_height="match_parent"
t/>
<TextView
android:id="#+id/fourth"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
In my fragment I am doing like this
class MyFragment : Fragment(R.layout.my_fragment) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val backedData = getRemoteData()
if(backedData.hasFour()) {
editText.visibility = Visibility.GONE
// set the four text view
} else {
groupOfFour.visibility = Visibility.GONE
}
}
}
Is there any other way of achieving the same behavior?
You can add the Views programmatically using addView
class MyFragment : Fragment(R.layout.my_fragment) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val backedData = getRemoteData()
if(backedData.hasFour()) {
groupOfFour.addView(addTextView("Textview 1"))
// set the four text view
} else {
groupOfFour.addView(addTextView("Textview 1"))
groupOfFour.addView(addTextView("Textview 2"))
groupOfFour.addView(addTextView("Textview 3"))
groupOfFour.addView(addTextView("Textview 4"))
}
}
fun addTextView(mytext: String):TextView {
return TextView(activity).apply {
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
text = mytext
}
}
}
And remove the TextViews from the layout
<LinearLayout 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"
android:orientation="vertical">
<LinearLayout
android:id="#+id/groupOfFour"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
If you want to set static Ids to the TextViews, you can check this answer

EditText not resize based on content in a viewpager

I have a simple structure. MainActivity with BottomNavigationView and NavController. In this nav controller, i have 4 items. One of them is FavoritesFragment. This fragment has simple layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".view.main.favorites.FavoritesFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tlTest"
android:layout_width="match_parent"
android:layout_height="40dp"
app:layout_constraintTop_toTopOf="parent" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/vpTest"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#id/tlTest"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
With following code:
FragmentAdapter
package com.lust.ahri.view.main.favorites
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.lust.ahri.view.base.BaseFragment
class TestAdapter(fragmentManager: FragmentManager) : FragmentPagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
private val fragments: MutableList<BaseFragment> = mutableListOf()
private val titles: MutableList<String> = mutableListOf()
override fun getItem(position: Int): Fragment {
return fragments[position]
}
override fun getCount(): Int {
return fragments.count()
}
override fun getPageTitle(position: Int): CharSequence? {
return titles[position]
}
fun addFragment(fragment: BaseFragment, title: String) {
fragments.add(fragment)
titles.add(title)
}
}
And FavoritesFragment
class FavoritesFragment : BaseFragment() {
private lateinit var adapter: TestAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_favorites, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
adapter = TestAdapter(childFragmentManager)
adapter.addFragment(TestFragment(), "Test1")
adapter.addFragment(TestFragment(), "Test2")
vpTest.adapter = adapter
tlTest.setupWithViewPager(vpTest)
}
}
Problem that in my TestFragment i have layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/test1"
android:layout_width="wrap_content"
android:hint="10"
android:maxLines="1"
android:maxLength="10"
android:layout_height="50dp" />
<EditText
android:layout_width="wrap_content"
android:hint="10"
android:maxLines="1"
android:maxLength="10"
android:layout_height="50dp"
android:layout_marginTop="70dp" />
</LinearLayout>
I meant that my edit texts will resize with wrap_content like as usual depending on the number of characters, in fact, the fields do not change their size but are only cropped.
I tried to change my layout to Constraint, Relative or another, but actually i couldn't find any solution.
I need an answer how to make edit texts change their width in a viewpager like a default layout behaviour and why it's happening.
I think the problem lies in the ViewPager 0dp height. Try this
<RelativeLayout 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"
>
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/Widget.AppCompat.PopupMenu.Overflow" />
<android.support.design.widget.TabLayout
android:id="#+id/mtab_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/colorAccent1"
app:tabIndicatorHeight="3dp"
app:tabTextColor="#color/colorWhite"
/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/mViewpager_ID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/app_bar"
/>
</RelativeLayout>

Android BottomNavigationBar moves when one of pages contains collapsing toolbar and fitSystemWindows is true

I have an activity with BottomNavigationView. On navigation I show/hide two fragments one of which is empty fragment, another contains collapsing toolbar which has set android:fitsSystemWindows="true" .
The problem is - when I open collapsing fragment tab and navigate back to empty fragment, the bottom navigation view moving down (out of device screen).
The problem doesn't appear, when I don't set fitSystemWindows true or when I create a new fragment every time user changes tab. But I need to keep both.
Is there a solution to escape this?
Here is the sample code, which behaves so
Activity
class BottomNavigationActivity : AppCompatActivity() {
private var currentFragment: Fragment? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bottom_navigation)
val bottomNavigationView = findViewById(R.id.activity_navigation__bottom_bar) as BottomNavigationView
bottomNavigationView.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.action_blank -> openFragment(findFragment("blank")?:BlankFragment(), "blank")
R.id.action_collapsed -> openFragment(findFragment("collapsing")?:CollapsingFragment(), "collapsing")
}
return#setOnNavigationItemSelectedListener true
}
}
private fun openFragment(fragment: Fragment, tag: String) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
currentFragment?.let {
fragmentTransaction.hide(it)
}
if (!fragment.isAdded) {
fragmentTransaction.add(R.id.container, fragment, tag);
}
if (!fragment.isVisible) {
fragmentTransaction.show(fragment)
}
currentFragment = fragment
fragmentTransaction.commitAllowingStateLoss()
}
private fun findFragment(tag: String): Fragment? {
return supportFragmentManager.findFragmentByTag(tag)
}
}
Activity layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#id/activity_navigation__bottom_bar"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/activity_navigation__bottom_bar"
android:background="#color/colorPrimary"
android:layout_width="match_parent"
app:menu="#menu/menu"
android:layout_height="56dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
the empty Fragment
class BlankFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_blank, container, false)
}
}
empty fragment layout xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="blank fragment"/>
</FrameLayout>
the collapsing Fragment
class CollapsingFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_collapsing, container, false)
}
}
collapsing fragment layout xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/fragment_collapsing_toolbar__appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/fragment_collapsing_toolbar__container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="?android:attr/windowBackground"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/fragment_collapsing_toolbar__toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="collapsing fragment"
app:titleMarginStart="72dp"
app:titleMarginEnd="72dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_collapsing_toolbar__content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
here is the menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_blank"
android:enabled="true"
android:title="blank"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_collapsed"
android:enabled="true"
android:title="collapsing"
app:showAsAction="ifRoom" />
</menu>
Thanks.

Categories

Resources