Skip to content

Instantly share code, notes, and snippets.

@StelianMorariu
Last active October 14, 2023 10:04
Show Gist options
  • Save StelianMorariu/c6c22c914cec977d3924f02da9c44061 to your computer and use it in GitHub Desktop.
Save StelianMorariu/c6c22c914cec977d3924f02da9c44061 to your computer and use it in GitHub Desktop.
Rudimentary MotionLayout collapsible toolbar
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright © 2019 Stelian Morariu. All rights reserved.
-->
<androidx.constraintlayout.motion.widget.MotionLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/collapsing_scene"
tools:context=".TestActivity"
tools:showPaths="true"
android:elevation="4dp">
<FrameLayout android:id="@+id/drop_shadow_layout"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:elevation="0dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="@+id/title"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"/>
<ImageView
android:id="@+id/toolbar_image"
android:layout_width="0dp"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:contentDescription="@null"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/guideline2"
android:fitsSystemWindows="true"
android:src="@drawable/avd_antrics_loading"
/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:text="@string/app_name"
android:textColor="?android:attr/textColorPrimary"
android:textSize="32sp"
android:textStyle="bold"
android:gravity="center"
android:padding="8dp"
app:layout_constraintTop_toBottomOf="@id/toolbar_image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.constraintlayout.widget.Guideline android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/guideline2"
app:layout_constraintGuide_percent="0.07"
android:orientation="horizontal"/>
</androidx.constraintlayout.motion.widget.MotionLayout>
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright © 2019 Stelian Morariu. All rights reserved.
-->
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Transition
app:constraintSetEnd="@id/collapsed"
app:constraintSetStart="@id/expanded">
<OnSwipe
app:dragDirection="dragUp"
app:touchAnchorId="@id/recyclerview"
app:touchAnchorSide="top" />
</Transition>
<ConstraintSet android:id="@+id/expanded">
<Constraint
android:id="@id/toolbar_image"
android:layout_height="200dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/guideline2"
android:elevation="4dp">
<CustomAttribute
app:attributeName="imageAlpha"
app:customIntegerValue="255" />
</Constraint>
<Constraint
android:id="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:scaleX="1.0"
android:scaleY="1.0"
app:layout_constraintTop_toBottomOf="@id/toolbar_image"
app:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="8dp">
</Constraint>
<Constraint android:id="@id/drop_shadow_layout"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:elevation="0dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="@+id/title"/>
</ConstraintSet>
<ConstraintSet android:id="@+id/collapsed">
<Constraint
android:id="@id/toolbar_image"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:elevation="4dp"
app:layout_constraintTop_toTopOf="parent">
<CustomAttribute
app:attributeName="imageAlpha"
app:customIntegerValue="0" />
</Constraint>
<Constraint
android:id="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleX="0.925"
android:scaleY="0.925"
android:elevation="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent">
</Constraint>
<Constraint android:id="@id/drop_shadow_layout"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:elevation="4dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="@+id/title"/>
</ConstraintSet>
</MotionScene>
/*
* Copyright © 2019 Stelian Morariu. All rights reserved.
*/
package com.stelianmorariu.antrics
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class TestActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment