Skip to content

Commit 8e1adbb

Browse files
Piotr ZawadzkiPiotr Zawadzki
authored andcommitted
Added a morphing animation of the Toolbar icon when opening ApplyFragment i.e. "back" icon to "clear" icon.
1 parent 7516390 commit 8e1adbb

7 files changed

Lines changed: 52 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This is a sample project where I experimented with different aspects of the [And
1010
- passing arguments between Fragments via [Safe Args](https://developer.android.com/guide/navigation/navigation-pass-data#Safe-args)
1111
- starting Fragments for result and waiting for that result (similar to Activity's `startActivityForResult` and `onActivityResult`)
1212
- transitions between fragments with shared Toolbar
13+
- a simple Toolbar navigation icon morphing ("<-" -> "X")
1314

1415
There's no MVP/MVVM/MVI here, no dependency injection - just the Navigation Component to make it simple.
1516

app/src/main/java/com/github/zawadz88/navigationcomponentplayground/ApplyFragment.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import android.os.Bundle
44
import android.view.LayoutInflater
55
import android.view.View
66
import android.view.ViewGroup
7+
import androidx.core.content.ContextCompat
8+
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
9+
import kotlinx.android.synthetic.main.appbar.toolbar
710

811
class ApplyFragment : BaseFragment() {
912

@@ -14,4 +17,16 @@ class ApplyFragment : BaseFragment() {
1417
initEnterTransitions()
1518
return inflater.inflate(R.layout.fragment_apply, container, false)
1619
}
20+
21+
override fun onActivityCreated(savedInstanceState: Bundle?) {
22+
super.onActivityCreated(savedInstanceState)
23+
if (savedInstanceState != null) {
24+
toolbar.setNavigationIcon(R.drawable.ic_clear)
25+
} else {
26+
val animatedIconDrawable = checkNotNull(AnimatedVectorDrawableCompat.create(requireContext(), R.drawable.avd_back_to_clear)) { "Could not create animated vector" }
27+
animatedIconDrawable.setTint(ContextCompat.getColor(requireContext(), R.color.toolbarColorControlNormal))
28+
animatedIconDrawable.start()
29+
toolbar.navigationIcon = animatedIconDrawable
30+
}
31+
}
1732
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<animated-vector
2+
xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:aapt="http://schemas.android.com/aapt">
4+
<aapt:attr name="android:drawable">
5+
<vector
6+
android:name="vector"
7+
android:width="24dp"
8+
android:height="24dp"
9+
android:viewportWidth="24"
10+
android:viewportHeight="24">
11+
<path
12+
android:name="path"
13+
android:pathData="M 20 11 L 7.8 11 L 13.4 5.4 L 12 4 L 4 12 L 12 20 L 13.4 18.6 L 7.8 13 L 20 13 L 20 11 Z"
14+
android:fillColor="#000000"
15+
android:strokeColor="#000000"/>
16+
</vector>
17+
</aapt:attr>
18+
<target android:name="path">
19+
<aapt:attr name="android:animation">
20+
<objectAnimator
21+
android:propertyName="pathData"
22+
android:duration="300"
23+
android:valueFrom="M 20 11 L 13.9 11 L 7.8 11 L 13.4 5.4 L 12 4 L 4 12 L 12 20 L 13.4 18.6 L 7.8 13 L 13.9 13 L 20 13 L 20 11 Z"
24+
android:valueTo="M 19 6.41 L 17.59 5 L 12 10.59 L 6.41 5 L 5 6.41 L 10.59 12 L 5 17.59 L 6.41 19 L 12 13.41 L 17.59 19 L 19 17.59 L 13.41 12 Z"
25+
android:valueType="pathType"
26+
android:interpolator="@android:interpolator/fast_out_slow_in"/>
27+
</aapt:attr>
28+
</target>
29+
</animated-vector>

app/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<color name="colorPrimary">#008577</color>
44
<color name="colorPrimaryDark">#00574B</color>
55
<color name="colorAccent">#D81B60</color>
6+
<color name="toolbarColorControlNormal">@android:color/white</color>
67
</resources>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<item name="ic_back" type="drawable" format="reference">@drawable/abc_ic_ab_back_material</item>
4+
<item name="ic_clear" type="drawable" format="reference">@drawable/abc_ic_clear_material</item>
45

56
</resources>

app/src/main/res/values/styles.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
<item name="windowNoTitle">true</item>
1414
</style>
1515

16-
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
16+
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
17+
<item name="colorControlNormal">@color/toolbarColorControlNormal</item>
18+
</style>
1719

1820
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
1921

22+
<style name="ToolbarColoredBackArrow" parent="AppTheme">
23+
</style>
2024
</resources>

art/navigation_playground_app.gif

658 KB
Loading

0 commit comments

Comments
 (0)