Open In App
Related Articles

Create an Expandable Notification Containing a Picture in Android

Improve Article
Save Article
Like Article

Notification is a type of message that is generated by any application present inside the mobile phone, suggesting to check the application and this could be anything from an update (Low priority notification) to something that’s not going right in the application (High priority notification). A basic notification consists of a title, a line of text, and one or more actions the user can perform in response. To provide even more information, one can also create large, expandable notifications by applying one of several notification templates as described in this article. Some daily life examples could be the notifications appended by Whatsapp, Gmail, SMS, etc in the notification drawer, where the user can expand it and can find some details about the message received such as sender name, subject and some part of the text in case of Gmail. In this article let’s create a notification inside an application that contains a picture.

expandable notification


Step 1: Create a new project 

To create a new project in android studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Modify activity_main.xml file

Inside the XML file just add a button, which on click would build an expandable notification. By expanding the notification from the notification drawer would display a picture.


<?xml version="1.0" encoding="utf-8"?>

Step 3: Modify the MainActivity file

Now, look at the code below which is in Kotlin. To start, build a notification with all the basic content as described in Create a Notification. Then, call setStyle() with a style object and supply information corresponding to each template, as shown below.


package com.example.expandablenotification
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
    // Assigning variables to Notification Manager, Channel and Builder
    lateinit var notifManager: NotificationManager
    lateinit var notifChannel: NotificationChannel
    lateinit var notifBuilder: Notification.Builder
    // Evaluating ChannelID and Description for the Custom Notification
    private val description = "Some Description"
    private val channelID = "Some Channel ID"
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring the button which onclick generates a notification
        val btn = findViewById<Button>(
        // Notification Service for the Manager
        notifManager = getSystemService(Context.NOTIFICATION_SERVICE)
                as NotificationManager
        // Notifications are in the form of Intents
        val someintent = Intent(this,
        val pendingIntent = PendingIntent.getActivity(
            this, 0, someintent,
        // Idea is to click the button and the notification appears
        btn.setOnClickListener {
            // This is how an Image to be displayed in our Notification
            // is decoded and stored in a variable. I've added a picture
            // named "download.jpeg" in the "Drawables".
            val myBitmap = BitmapFactory.decodeResource(resources, 
            // If Min. API level of the phone is 26, then notification could be
            // made asthetic
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notifChannel = NotificationChannel(
                    channelID, description,
                notifChannel.lightColor = Color.RED
                notifBuilder = Notification.Builder(this, channelID)
                    .setContentTitle("Some Title")
                    .setContentText("Some Content Text")
                    // Command to Insert Image in the Notification
                        Notification.BigPictureStyle() // <---- Look here
                    ) // <---- Look here
            // Else the Android device would give out default UI attributes
            else {
                notifBuilder = Notification.Builder(this)
                    .setContentTitle("Some Title")
                    .setContentText("Some Content Text")
            // Everything is done now and the Manager is to be notified about
            // the Builder which built a Notification for the application

Note: If you have previously searched for the code for expandable notifications, then you must have seen this particular line of code:


As “NotificationCompat” is currently deprecated and your code would always crash whenever an attempt is made to build a notification (on button click in our case). Instead, just use “Notification”.

Output: Run on Emulator

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 01 Sep, 2020
Like Article
Save Article
Similar Reads