I have a TextView inside a Constraint Layout. I am trying to animate in a way that the view goes off the screen from top. This is what I did so far,
ConstraintSet constraintSet = new ConstraintSet();
ConstraintLayout layout = (ConstraintLayout)holder.mView;
constraintSet.clone(layout);
constraintSet.clear(R.id.txt_PackageTitle,ConstraintSet.TOP);
constraintSet.clear(R.id.txt_PackageDescription,ConstraintSet.TOP);
constraintSet.clear(R.id.txt_PackageTitle,ConstraintSet.BOTTOM);
constraintSet.clear(R.id.txt_PackageDescription,ConstraintSet.BOTTOM);
constraintSet.setMargin(R.id.txt_PackageTitle,ConstraintSet.TOP,-600);
constraintSet.setMargin(R.id.txt_PackageDescription,ConstraintSet.TOP,-1200);
ChangeBounds transition = new ChangeBounds();
transition.setInterpolator(new BounceInterpolator());
transition.setDuration(600);
TransitionManager.beginDelayedTransition(layout,transition);
constraintSet.applyTo(layout);
Now this code only moves the contents to the very top of the view, its not going out of the view and getting disappeared.
How can I do this with constraint layout?
Create ImageView in the activity_main. xml along with buttons that will add animation to the view. Navigate to the app > res > layout > activity_main. xml.
Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken button to show /hide linear layout with animation.
If not, providing animations for each of the views will be a tiresome job. ConstraintLayout will only perform animation on its direct children since it only knows when you change layout parameters and constraints on the children that it handles. It may not handle ViewGroups well.
This example demonstrate about how to Show and hide a View with a slide up/down animation in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.
Now to use ConstraintLayout features in our android project, we will need to add the library to our build.gradle app module dependencies section. In Android Studio design and blueprint mode are added which display the layout design in design and blueprint mode. You can enable any mode or both together according to your requirement.
Instead of clearing the bottom constraint of the TextView
, try constraining its bottom to the top of the ConstraintLayout
like this:
constraintSet.connect (R.id.txt_PackageTitle,
ConstraintSet.BOTTOM,
PARENT_ID,
ConstraintSet.TOP);
Now when the view is animated, it should slide off the top edge.
Negative margins are not supported with ConstraintLayout
as is noted here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With