Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Progress notifications are used in Android to indicate the progress of an ongoing operation. Here's how to create and update a progress notification using Kotlin in Android:
Ensure you have the FOREGROUND_SERVICE
permission in the AndroidManifest.xml
if you intend to show the notification from a foreground service:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
It's good practice to create a notification channel for Android 8.0 (API level 26) and above:
private fun createNotificationChannel(channelId: String, channelName: String) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val importance = NotificationManager.IMPORTANCE_LOW val channel = NotificationChannel(channelId, channelName, importance).apply { description = "Channel description" } val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel) } }
Call this method in your onCreate()
:
createNotificationChannel("progress_channel", "Progress Notifications")
Use the NotificationCompat.Builder
class to build and display the notification:
fun showProgressNotification(progress: Int) { val channelId = "progress_channel" val notification = NotificationCompat.Builder(this, channelId).apply { setContentTitle("Download") setContentText("Download in progress") setSmallIcon(R.drawable.ic_notification) setPriority(NotificationCompat.PRIORITY_LOW) setOngoing(true) setProgress(100, progress, false) // here 100 denotes the max progress, and the second parameter is the current progress }.build() val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(1, notification) }
To update the progress, call showProgressNotification()
with the updated progress value:
for (i in 0..100) { showProgressNotification(i) Thread.sleep(100) // just for demonstration purposes, do not use Thread.sleep in production }
Once the operation is complete, you can update the notification to indicate completion:
fun completeNotification() { val channelId = "progress_channel" val notification = NotificationCompat.Builder(this, channelId).apply { setContentTitle("Download") setContentText("Download complete") setSmallIcon(R.drawable.ic_notification) setPriority(NotificationCompat.PRIORITY_LOW) setOngoing(false) setProgress(0, 0, false) // Remove progress bar }.build() val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(1, notification) }
Remember to properly manage the lifecycle of any services or background tasks associated with your notification. If you're doing work in the background, it's also recommended to use a ForegroundService
combined with progress notifications to ensure the task completes.
MotionLayout in Android using Kotlin example:
Include the MotionLayout in your XML layout:
<androidx.constraintlayout.motion.widget.MotionLayout android:id="@+id/motionLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/motion_scene"> <!-- Add your UI elements here --> </androidx.constraintlayout.motion.widget.MotionLayout>
Create a motion_scene.xml
file in res/xml/
to define your animation scenes.