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