You are on page 1of 4

ANDROID

LAB Record

Lab Exercise 5

Aim:
Create an android application that contains a single button named click me. Whenever the
button is clicked it should always trigger a notification. On clicking the notification in the
notification bar a new activity should launch.

Learning Objective:

Through this activity we will come to create custom notification as well as we will be
able to navigate through one activity to other using notifications. Moreoever we will also learn
how to handle notifications.

Prerequisites:

ANDROID STUDIO

Standard Procedure

XML Code
Java Code

LAB 5:
LAB 5:
XML CODE:
activity_main.xml
<?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:id="@+id/activity_main"
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="com.example.android.notificationtest.MainActivity">
<Button
android:text="Click Me"
android:textSize="20dp"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="124dp"
android:id="@+id/button"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_marginStart="110dp" />
<TextView
android:text="@string/activity_message"
android:layout_width="match_parent"
android:layout_marginTop="80dp"
android:id="@+id/textView"
android:textSize="30sp"
android:textColor="#FF7C0EEA"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

activity_second.xml
<?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:id="@+id/activity_second"
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="com.example.android.notificationtest.SecondActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:id="@+id/outputTxt"
android:textSize="30sp" />
</RelativeLayout>
LAB 5:
JAVA CODE:
MainActivity.java
package com.example.android.notificationtest;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button setNotification;
private TextView titleMsg;
private NotificationCompat.Builder notiBuilder;
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setNotification = (Button)findViewById(R.id.button);
titleMsg = (TextView)findViewById(R.id.textView);
notificationManager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
setNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notiBuilder = new NotificationCompat.Builder(MainActivity.this);
notiBuilder.setContentTitle("Notification Generated");
notiBuilder.setContentText("Build from My App");
notiBuilder.setSubText("Coded By Kapil");
notiBuilder.setSmallIcon(R.mipmap.ic_launcher);
notiBuilder.setAutoCancel(true);
Intent secondActivityIntent = new
Intent(getBaseContext(),SecondActivity.class);
secondActivityIntent.putExtra("Key","Called From MainActivity\n Do You
Like It?");
PendingIntent pendingIntent =
PendingIntent.getActivity(getApplication(),30,secondActivityIntent,PendingIntent.FLAG_UP
DATE_CURRENT);
notiBuilder.setContentIntent(pendingIntent);
notificationManager.notify(30,notiBuilder.build());
Toast.makeText(getBaseContext(),"Check The Notification
Bar",Toast.LENGTH_SHORT).show();
}
});
}
}

SecondActivity.java

package com.example.android.notificationtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView = (TextView)findViewById(R.id.outputTxt);
Bundle b = getIntent().getExtras();
textView.setText(b.getString("Key"));
}}
LAB 5:

OUTPUT:

CONCLUSION:
The above application has a Click Me. As you can see whenever the button is
clicked new notification is triggered in the notification bar. As you click the
notification from the notification bar a new activity is launched.

You might also like