0

I am working on app where I am using SDK which will provide me one simple Widget that I need to include in my app. That Widget is fragment which can have dynamic height (depending on API response it can show different information)

Problem that I have is that I need to set layout height not knowing will the widget (fragment) fit inside.

I've tried with several options as following:

Set LinearLayout which has minHeight="100dp" and CardView inside that I use to display widget in:

                <LinearLayout
                    android:id="@+id/linearLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginEnd="16dp"
                    android:minHeight="100dp"
                    android:orientation="vertical"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <androidx.cardview.widget.CardView
                        android:id="@+id/widget_container"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />

                </LinearLayout>

But this approach is not working since layout will not resize itself in case that widget is larger/smaller than 100dp.

My idea was to try to get fragments width/height and set that properties to my layout, and after that I would use childFragmentManager to replace cardview with my widget fragment.

import com.custom.sdk.ui.MySuperWidget
...
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //Try to get Width and Height
        val myWidget = MySuperWidget()

        //Update linear layout width and height
        val myWidgetContainerLayout = widget_container
        val parameters: LinearLayout.LayoutParams = myWidgetContainerLayout.layoutParams
        newParameters.width = 100
        newParameters.height = 325
        myWidgetContainerLayout.layoutParams = newParameters

        //Replace fragment
        childFragmentManager.beginTransaction().replace(R.id.widget_container, myWidget).commit()
    }
}

I used this response https://stackoverflow.com/a/57259721/5270396 as idea to set LinearLayout properties programmatically before I replace fragment but I am unable to get fragments height/width

Daniel.Wang
  • 2,242
  • 2
  • 9
  • 27
Venan24
  • 23
  • 5
  • why dont you look in the code of MySuperWidget, what height width its setting? its according to parent or on other basis – NehaK Jan 20 '21 at 18:49
  • 1
    try to set widget_container's width height to wrap_content – NehaK Jan 20 '21 at 18:51
  • @NehaK MySuperWidget has ConstraintLayout that has match_parent as width and wrap_content as height set. Regarding second comment, I tried that and it is not working. I assume that widget_container is created before MySuperWidget and when it tries to wrap_content, there is no content to wrap. – Venan24 Jan 20 '21 at 19:00
  • 1
    wrap_content means it will handled according to child. try postInvalidate() and requestLayout() method of widget_container after 1-2 second of adding fragment – NehaK Jan 21 '21 at 05:54
  • just to clear whether its working or not – NehaK Jan 21 '21 at 05:54
  • @NehaK there was one "container_widget" that had "match_parrent" instead of "wrap_content". I fixed it with your idea. – Venan24 Jan 27 '21 at 13:24
  • so is it working now? – NehaK Jan 27 '21 at 14:50
  • @NehaK Yes it works – Venan24 Jan 28 '21 at 12:29

0 Answers0