Not able to inflate a fragment inside the Frame layout which is a children of collapse bar layout

15 Views Asked by At

So, I am trying to inflate a toolbar fragment inside the framelayout at the top of the screen so that it is collapsable while I scroll down the content. But I am unable to do so and when I am removing the collapse toolbar then the code is working perfectly. Can anyone please tell me where I am going wrong as I want the toolbar fragment to be collapsable. Here is code:

activity_home.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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:background="@color/white"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context=".HomeActivity">

<!--    <com.google.android.material.appbar.AppBarLayout-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:background="@color/white"-->
<!--        android:fitsSystemWindows="true">-->

<!--        <com.google.android.material.appbar.CollapsingToolbarLayout-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="wrap_content"-->
<!--            android:fitsSystemWindows="true"-->
<!--            android:background="@color/white"-->
<!--            android:backgroundTint="@color/white"-->
<!--            app:layout_scrollFlags="scroll|snap|exitUntilCollapsed">-->



<!--             Add this to make the included content collapse -->

    

        <FrameLayout
            android:id="@+id/toolbarContainer"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
         
            android:background="@color/white"
            android:backgroundTint="@color/white" />


        <include
            android:id="@+id/main_content"
            layout="@layout/content_main"

           />




</androidx.coordinatorlayout.widget.CoordinatorLayout>

HomeActivity.kt

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction

class HomeActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

//        val fragmentManager:FragmentManager = supportFragmentManager
//        val fragmentTransaction:FragmentTransaction = fragmentManager.beginTransaction()
//        val toolbarFragment = ToolbarFragment()
//
//        if (toolbarFragment != null) {
//            fragmentTransaction.add(R.id.toolbarContainer, toolbarFragment)
//            fragmentTransaction.addToBackStack(null)
//            fragmentTransaction.commit()
//        } else {
//            Log.e("HomeActivity", "ToolbarFragment is null.")
//        }
        val fragmentManager: FragmentManager = supportFragmentManager
        val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()

        val inflater = layoutInflater
        val toolbarFragmentLayout = inflater.inflate(R.layout.fragment_toolbar, null)

        fragmentTransaction.add(R.id.toolbarContainer, ToolbarFragment())
        fragmentTransaction.addToBackStack(null)
        fragmentTransaction.commit()
    }
}

fragment_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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:background="@color/white"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:fitsSystemWindows="true">
<!--                    <com.google.android.material.appbar.CollapsingToolbarLayout-->
<!--                        android:layout_width="match_parent"-->
<!--                        android:layout_height="wrap_content"-->
<!--                        android:fitsSystemWindows="true"-->
<!--                        android:background="@color/white"-->
<!--                        android:backgroundTint="@color/white"-->
<!--                        app:layout_scrollFlags="scroll|snap|exitUntilCollapsed">-->


                            <LinearLayout
                                android:background="@color/white"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="10dp">

                                <ImageView
                                    android:id="@+id/location"
                                    android:layout_width="31dp"
                                    android:layout_height="48dp"
                                    android:layout_gravity="center_vertical"
                                    android:maxHeight="60dp"
                                    android:src="@drawable/star_charcoal" />

                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center_vertical"
                                    android:orientation="vertical"
                                    android:paddingStart="2dp">

                                    <LinearLayout
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_gravity="center_horizontal"
                                        android:orientation="horizontal">

                                        <TextView
                                            android:id="@+id/locale"
                                            android:layout_width="wrap_content"
                                            android:layout_height="24dp"
                                            android:layout_gravity="center_horizontal"
                                            android:fontFamily="sans-serif"
                                            android:text="Ranchi Ring Rd."
                                            android:textColor="@color/black"
                                            android:textSize="18sp"
                                            android:textStyle="bold" />

                                        <ImageView
                                            android:id="@+id/down_arrow"
                                            android:layout_width="24dp"
                                            android:layout_height="24dp"
                                            android:layout_gravity="center_vertical"
                                            android:layout_marginStart="1dp"
                                            android:src="@drawable/star_charcoal" />
                                    </LinearLayout>

                                    <TextView
                                        android:id="@+id/area"
                                        android:layout_width="wrap_content"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center_vertical"
                                        android:fontFamily="sans-serif"
                                        android:text="Mesra"
                                        android:textColor="@color/black"
                                        android:textSize="18sp"
                                        android:textStyle="normal" />

                                </LinearLayout>

                                <ImageView
                                    android:id="@+id/profilePhotoT"
                                    android:layout_width="40dp"
                                    android:layout_height="40dp"
                                    android:layout_gravity="center_vertical"
                                    android:layout_marginStart="130dp"
                                    android:src="@drawable/star_charcoal" />

                            </LinearLayout>



<!--                    </com.google.android.material.appbar.CollapsingToolbarLayout>-->
        </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>


ToolbarFragment.kt

package com.example.myapplication

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.appbar.CollapsingToolbarLayout


class ToolbarFragment : Fragment() {


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_toolbar, container, false)
        val profilePicture:ImageView = view.findViewById(R.id.profilePhotoT)
        val downArrow:ImageView = view.findViewById(R.id.down_arrow)
        val area:TextView = view.findViewById(R.id.area)
        val locale:TextView = view.findViewById(R.id.locale)
        val location:ImageView = view.findViewById(R.id.location)

        area.text = "Your Area"
        locale.text = "Your Locale"

        (activity as AppCompatActivity).supportActionBar?.title = "Your Title"


        return view
    }

}

I tried everthing shifting codes from activity to fragments. So the features workout correctly.But none of the solutions have worked so far. Can anybody help me with this.

0

There are 0 best solutions below