Latest News

The Big View Notification - Android Studio

In this tutorial we will see how to display a big view notification having title , icon , multiple lines etc. When button is clicked notification will be displayed and when we click notification it will open an activity.

Step 1: Create a new project OR Open your project

Step 2: Place an icon in res>drawable folder

Step 3: Code

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="10dp"tools:context=".MainActivity" > <Button android:id="@+id/btnShowNotif" android:text="Show Notification" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>

MainActiviy.java
package com.blogspot.devofandroid.myapplication;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.TaskStackBuilder;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {Button mBtnShowNoti;NotificationManager mNotificationManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mBtnShowNoti = findViewById(R.id.btnShowNotif);mBtnShowNoti.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {// notificationID allows you to update the notification later on.displayNotification();}});}protected void displayNotification() {Log.i("Start" , "notification");int numMessages = 0;/* Invoking the default notification service */NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);mBuilder.setContentTitle("New Message");mBuilder.setContentText("You have received new message.");mBuilder.setTicker("New Message Alert...!");mBuilder.setSmallIcon(R.drawable.icon);/* Increase notification number every time a new notification arrives */mBuilder.setNumber(++numMessages);/* Add Big View Specific Configuration */NotificationCompat.InboxStyle mInboxStyle = new NotificationCompat.InboxStyle();String[] mEvents = new String[4];mEvents[0] = new String("This is the 1st line...");mEvents[1] = new String("This is the 2nd line...");mEvents[2] = new String("This is the 3rd line...");mEvents[3] = new String("This is the 4th line...");// Sets a title for the Inbox style big viewmInboxStyle.setBigContentTitle("Big Title Detail:");// Moves mEvents into the big viewfor (int i=0; i < mEvents.length; i++) {mInboxStyle.addLine(mEvents[i]);}mBuilder.setStyle(mInboxStyle);/* Creates an explicit intent for an Activity in your app */Intent resultIntent = new Intent(this , MainActivity.class);TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);stackBuilder.addParentStack(MainActivity.class);/* Adds the Intent that starts the Activity to the top of the stack */stackBuilder.addNextIntent(resultIntent);PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0 ,PendingIntent.FLAG_UPDATE_CURRENT);mBuilder.setContentIntent(resultPendingIntent);mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);/* notificationID allows you to update the notification later on. */mNotificationManager.notify(0 , mBuilder.build());}}

Step 4: Output

 

0 Response to "The Big View Notification - Android Studio"