Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:visibility changes to children of MotionLayout

People also ask

What is MotionLayout in Android?

MotionLayout is a layout type that helps you manage motion and widget animation in your app. MotionLayout is a subclass of ConstraintLayout and builds upon its rich layout capabilities. As part of the ConstraintLayout library, MotionLayout is available as a support library and is backwards-compatible to API level 14.


Turns out this was my inexperience with how MotionLayout works. By default MotionLayout controls the visibility of all views within it. But you can opt out child views by using the app:visibilityMode="ignore" attribute and defining the view in the <ConstraintSet>

In my case <CustomViewGroup1> looks like this...

<Constraint
      android:id="@id/CustomViewGroup1"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/HeaderBackground"
      app:layout_constraintVertical_weight="1"
      app:visibilityMode="ignore" />

And this is defined the same in both collasped and expended ConstraintSets as I don't want it to move/animation when the recycler view is scrolled.

Thanks to John Hoford for the advice in another channel.


My approach is to exclude the view from the motion layout, ignore the visibility from the scene so it can be programmatic and also inherit the constraints in the end constraint set.

The documentation mentioned the app:applyMotionScene="boolean" but it doesn't tell you where. It has to be in a PropertySet

Also visibilityMode only worked for me inside a PropertySet as well

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
        ...
    </Transition>

    <ConstraintSet android:id="@+id/start">

        <Constraint android:id="@id/viewId">
            <PropertySet
                app:applyMotionScene="false"
                app:visibilityMode="ignore" />
        </Constraint>

    </ConstraintSet>

    <ConstraintSet
        android:id="@+id/end"
        motion:deriveConstraintsFrom="@id/start">
        ...
    </ConstraintSet>

</MotionScene>

Careful with the app namespace auto-import from motion scene, that one is wrong you have to use the same that is used on the layouts.

The benefit of this is:

  • No width, height or constraints in the scene
  • No need to repeat the properties since is derived from the start constraint set

Programmatically way

View clickableArea = motionLayout.findViewById(R.id.video_overlay_thumbnail);    
motionLayout.getConstraintSet(R.id.start).getConstraint(clickableArea.getId()).propertySet.mVisibilityMode = 1; // 1 - ignore or 0 - normal
clickableArea.setVisibility(GONE);

Set visibilityMode property to normal, working fine for me with 2.0.0-beta3.

 app:visibilityMode="normal"
 android:visibility="visible"