0% found this document useful (0 votes)
24 views2 pages

Aim: Implement The Notification Concept in Android: Activity - Main - XML

The document discusses implementing notifications in an Android application. It includes the layout and Java code for a simple activity with a button that generates a test notification when clicked. The activity layout contains a button centered on the screen. The Java code builds a notification using NotificationCompat and shows it using the NotificationManager when the button is clicked.

Uploaded by

shafe SP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Aim: Implement The Notification Concept in Android: Activity - Main - XML

The document discusses implementing notifications in an Android application. It includes the layout and Java code for a simple activity with a button that generates a test notification when clicked. The activity layout contains a button centered on the screen. The Java code builds a notification using NotificationCompat and shows it using the NotificationManager when the button is clicked.

Uploaded by

shafe SP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Mohammadshafe Pahochiya 18BECE30537

Aim : Implement the notification concept in Android

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Notify"
android:onClick="btn_notify" />

</RelativeLayout>

MainActivity.java
package com.example.notify;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void btn_notify(View view) {


NotificationCompat.Builder builder = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_android_black_24dp)
.setContentTitle("Notification Title")
.setContentText("This is a test notification shafe sp");

Intent notificationIntent = new Intent(this,MainActivity.class);


PendingIntent contentIntent =
PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CUR
RENT);
Mohammadshafe Pahochiya 18BECE30537

builder.setContentIntent(contentIntent);

Object context;
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,builder.build());
}
}

Output :

You might also like