0

I'm adding new fragments dynamically to an existing fragment and I'm doing some calculation to infer the size of the new fragment. After getting the new size i change the layout params of that new fragment to match the calculated size and this is where all the child views disappear - if i will not change the size the view will be presented perfectly ok.

XML code for the generated fragment:

<?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:id="@+id/playerCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#339CEF"
android:clickable="true"
android:focusable="true"
tools:context=".playerCard">

<ImageView
    android:id="@+id/playerImage"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="40dp"
    android:layout_marginEnd="20dp"
    android:layout_marginBottom="20dp"
    android:background="#DA4444"
    android:clickable="false"
    android:contentDescription="TODO"
    android:focusable="true"
    android:visibility="visible"
    app:layout_constraintBottom_toTopOf="@id/textView4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent="0.7"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0"
    app:srcCompat="@drawable/ic_menu_camera"
    tools:srcCompat="@drawable/ic_menu_camera" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginTop="20dp"
    android:layout_marginEnd="20dp"
    android:background="@color/teal_200"
    android:clickable="false"
    android:focusable="true"
    android:text="TextView"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/playerImage" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#99A9D8"
    android:clickable="false"
    android:contentDescription="TODO"
    android:focusable="true"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is how i change the size on the card onCreateView (sizes were checked and are ok - not zero):

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    super.onCreateView(inflater, container, savedInstanceState)
    var view = inflater.inflate(R.layout.fragment_player_card, container, false)
    view.x = this.centerPoint?.x?.toFloat() ?: 0.0f
    view.y = this.centerPoint?.y?.toFloat() ?: 0.0f
    view.layoutParams.width = this.cardSize?.width ?: 0
    view.layoutParams.height = this.cardSize?.height ?: 0
    view.requestLayout()
    

    view.setOnTouchListener { v, event ->
        onCardTouch(event)
    }

    return view
}

Also tried to do the size changing in a post statement but got the same results. Any help will be appreciated.

Itzik984
  • 15,968
  • 28
  • 69
  • 107

1 Answers1

0

Please use this solution here:

    @Override
    public void onResume() {
        super.onResume();

        getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) getLayoutParameters());
    }

    private final ViewGroup.LayoutParams getLayoutParameters(){
        ViewGroup.LayoutParams layoutParameters = getDialog().getWindow().getAttributes();
        layoutParameters.width = MYCALCULATEDSIZE.Width;
        layoutParameters.height = MYCALCULATEDSIZE.Height;

        return layoutParameters;
    }

It's important to make sure that the layout parameters are updated when the Fragment Resumes, otherwise you could risk an exception, where the view has not been inflated yet. Writing it here, makes sure that the fragment, along with its child views have been inflated properly.

Dan Gerchcovich
  • 168
  • 5
  • 12
  • Not sure how this will solve the issue, am i missing out on something? – Itzik984 Jan 31 '21 at 09:57
  • You need to add this to the fragment that contains the child views. So the calculated final size of the fragment, needs to be passed as the Width & Height parameters. I've edited my answer to make it a little clearer – Dan Gerchcovich Jan 31 '21 at 10:05
  • Ok, checking (i'm using kotlin so it may take me some time to translate this) – Itzik984 Jan 31 '21 at 10:09