Skip to content

Instantly share code, notes, and snippets.

@ZacSweers
Created November 19, 2017 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZacSweers/8ca2db09a69748e231dca5040e101ee8 to your computer and use it in GitHub Desktop.
Save ZacSweers/8ca2db09a69748e231dca5040e101ee8 to your computer and use it in GitHub Desktop.
Stars in notifications
fun notification() {
val channelId = "stars"
val notificationManager = activity.getSystemService<NotificationManager>()
if (VERSION.SDK_INT >= VERSION_CODES.O) {
val channels = notificationManager.notificationChannels
if (channels.none { it.id == channelId }) {
NotificationChannel(
channelId, "stars", NotificationManager.IMPORTANCE_HIGH)
.apply {
description = "Demo"
}
.let {
notificationManager.createNotificationChannel(it)
}
}
}
val notificationView = RemoteViews(activity.packageName, R.layout.stars_notification)
notificationView.setOnClickPendingIntent()
val notificationId = activity.getString(R.string.app_name).hashCode()
NotificationCompat.Builder(activity, channelId)
.apply {
color = ContextCompat.getColor(activity, R.color.colorAccent)
setCustomBigContentView(notificationView)
setContentTitle("Rate something!")
setAutoCancel(true)
}
.let {
notificationManager.notify(notificationId, it.build())
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rate your trip!"
android:padding="8dp"
style="@style/TextAppearance.AppCompat.Headline"
/>
<LinearLayout
android:layout_below="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:dividerPadding="16dp"
android:padding="16dp"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/star1"
android:layout_width="42dp"
android:layout_height="42dp"
android:src="@drawable/ic_star_border_black_24dp"
tools:src="@drawable/ic_star_black_24dp"
/>
<ImageView
android:id="@+id/star2"
android:layout_width="42dp"
android:layout_height="42dp"
android:src="@drawable/ic_star_border_black_24dp"
tools:src="@drawable/ic_star_black_24dp"
/>
<ImageView
android:id="@+id/star3"
android:layout_width="42dp"
android:layout_height="42dp"
android:src="@drawable/ic_star_border_black_24dp"
tools:src="@drawable/ic_star_black_24dp"
/>
<ImageView
android:id="@+id/star4"
android:layout_width="42dp"
android:layout_height="42dp"
android:src="@drawable/ic_star_border_black_24dp"
tools:src="@drawable/ic_star_black_24dp"
/>
<ImageView
android:id="@+id/star5"
android:layout_width="42dp"
android:layout_height="42dp"
android:src="@drawable/ic_star_border_black_24dp"
tools:src="@drawable/ic_star_black_24dp"
/>
</LinearLayout>
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment