Android Notification
Android Toast class provides a handy way to show users alerts but problem is
that these alerts are not persistent which means alert flashes on the screen for
a few seconds and then disappears.
To see the details of the notification, you will have to select the icon which will
display notification drawer having detail about the notification. While working
with emulator with virtual device, you will have to click and drag down the
Create Notification App:
Open Android Studio & Create New Project
Name Of Project Ex: Notification
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxciLvCImD8eq79AP-V28jD6rr3JGR0TrSYL_m5uefg8IP55eimmmHHULqshdXsQOQkcheKe15bX-UuR-jBAQCszeFsLzhJ8uMbc6a0QlSuvjvc0jjj8sKimqlY2v4zx2CW-LjztzhYW8/s400/minstall.png)
2. Rename Application Name Below Screen Ex: Notification
Click Next
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihryGIvqjbYBnRivsIBmLFvMdfr-DoRD2-40IPx-jIwWbzIQTrkvpn_WWyHlbyBxuqiEjzuwYhqNQ89Dbbo0seDwszcweqspZ4Qwnwx4oDmUcRZFjh_GidLcyXsIBDQD7jnbz95uj5bLY/s400/first.png)
3. After Spacific Minium sdk API 23 Select Lolipop 5.0
Click Next
4. Final Select Blank Activity And
Click Next
5. Then Select MainActivity Click Next & Finish
6. Then Open a Android Studio
1. MainActivity.Java
package com.prakash.Notification;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNotification();
}
});
}
private void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.abc)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
2. Create A New Java File
Name Below NotificationView.java
package com.prakash.appsshortcut;
import android.os.Bundle;
import android.app.Activity;
public class NotificationView extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
}
}
3. Notification.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:text="Hi, Your Detailed notification view goes here...."/>
</LinearLayout>
4. Activity Main.xml
<?xml version="1.0" encoding="utf-8"?>
<?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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification Example"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:id="@+id/button"
android:layout_marginTop="62dp"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
5. Activity Mainifests.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.prakash.notification">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NotificationView">
</activity>
</application>
</manifest>
<< PREVIEW >>
<< NEXT >>
No comments:
Post a Comment