Skip to content

Instantly share code, notes, and snippets.

@diegohkd
Last active April 11, 2020 22:11
Show Gist options
  • Save diegohkd/9f1fdd1614b2d75c308504c0d977f539 to your computer and use it in GitHub Desktop.
Save diegohkd/9f1fdd1614b2d75c308504c0d977f539 to your computer and use it in GitHub Desktop.
Toolbar menu item with animation for notifications counter using Action View. Sample project: https://github.com/diegohkd/AndroidKotlinLab/tree/master/ToolbarMenuItemWithAnimation
<?xml version="1.0" encoding="utf-8"?>
<example.com.toolbarmenuitemwithanimation.NotificationsActionView 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"
style="?attr/actionButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/bellImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:src="@drawable/ic_notifications"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/notificationsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="@drawable/round_corners_background"
android:ellipsize="end"
android:maxLines="1"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:textColor="@color/white"
android:visibility="gone"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_max="wrap"
tools:text="4"
tools:visibility="visible" />
</example.com.toolbarmenuitemwithanimation.NotificationsActionView>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/notification"
app:actionLayout="@layout/action_view_notifications"
android:title="@string/notifications"
app:showAsAction="always" />
</menu>
class MainActivity : AppCompatActivity() {
...
private var notificationsActionView: NotificationsActionView? = null
...
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
return true
}
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
val notificationItem = menu?.findItem(R.id.notification)?.actionView
notificationsActionView = notificationItem as? NotificationsActionView
notificationsActionView?.setOnClickListener {
Toast.makeText(this, "Not implemented", Toast.LENGTH_SHORT).show()
}
return super.onPrepareOptionsMenu(menu)
}
}
class NotificationsActionView(
context: Context, attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
private val bounceAnimation: Animation by lazy {
AnimationUtils.loadAnimation(context, R.anim.bounce_interpolar)
}
fun setNotificationsCount(count: Int) {
if (count <= 0) {
notificationsTextView.text = ""
notificationsTextView.isVisible = false
} else {
notificationsTextView.text = if (count > 99) {
resources.getString(R.string.max_count_plus)
} else {
count.toString()
}
notificationsTextView.isVisible = true
notificationsTextView.startAnimation(bounceAnimation)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment