SAMD Lab 11

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Android Notification Bar

Lab -

Introduction
A notification is a message you can display
to the user outside of your application's
normal UI. When you tell the system to issue
a notification, it first appears as an icon in
the notification area. To see the details of
the notification, the user opens the
notification drawer. Both the notification
area and the notification drawer are system-
controlled areas that the user can view at
any time.

Design Considerations

Notifications, as an important part of the


Android user interface, have their own
design guidelines. The material design
changes introduced in Android 5.0 (API
level 21) are of particular importance, and
you should review the Material Design
training for more information. To learn how to design notifications and their interactions,
read the Notifications design guide.

Creating a simple notification

Design an interface that takes input from the user. The inputs are the name, age, and
gender of the person. The inputs should be stored in an object of a class named person.
The object should have the attributes of name, age and gender.
Design a notification that is displayed on the notification bar when user presses the ‘OK’
button to submit these inputs. Also, in the notification history list it should display if the
gender entered was male or female and whether the male is older than 30, and if a
female then if she is older than 40 or not.

Procedure:
The code for the person class is as shown below:

public class PersonClass {

public String pName;


public String pGender;
public String pAge;

public String getpName() {


return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public String getpGender() {
return pGender;
}
public void setpGender(String pGender) {
this.pGender = pGender;
}
public String getpAge() {
return pAge;
}
public void setpAge(String pAge) {
this.pAge = pAge;
}
}

For designing the interface and binding the controls to the java file the code is as shown
below:

private void initialize() {

name = (EditText) findViewById(R.id.etName);


gender = (EditText) findViewById(R.id.etGender);
age = (EditText) findViewById(R.id.etAge);
ok = (Button) findViewById(R.id.button1);

ok.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
PersonClass pclass = new PersonClass();
pclass.pName = name.getText().toString();
pclass.pGender = gender.getText().toString();
pclass.pAge = age.getText().toString();

checkMaleorFemale(pclass);
}
});
}

Code for the notification is as shown below:

// Notification count
private int mNotificationCount;

// Notification Text Elements


private final CharSequence tickerText = "This is Really, Really, Super " +
"Long Notification Message";
private final CharSequence contentTitle = "Notification";
private final CharSequence contentText = "You've Been Notified";

// Notification Action Elements


private Intent mNotificationIntent;
private PendingIntent mContentIntent;

RemoteViews mContentViews = new RemoteViews(


"com.example.lab12", R.layout.notificationview);

EditText name, age, gender;


Button ok;

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

mNotificationIntent = new
Intent(getApplicationContext(),NotificationActivity.class );

mContentIntent = PendingIntent.getActivity(getApplicationContext(),
0, mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
initialize();
}

public void checkMaleorFemale(PersonClass object){

int iage = Integer.parseInt(object.pAge);

if(object.pGender.equals("male") && iage>30){

mContentViews.setTextViewText(R.id.text, "Is a Male older than


30");

// Build the Notification


Notification.Builder notificationBuilder =
new Notification.Builder(getApplicationContext())
.setTicker(tickerText)
.setSmallIcon(R.drawable.samllnotification)
.setAutoCancel(true)
.setContentIntent(mContentIntent)
//.setSound(soundURI)
//.setVibrate(mVibratePattern)
.setContent(mContentViews);

// Pass the Notification to the NotificationManager


NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MY_NOTIFICATION_ID,
notificationBuilder.build());

else if(object.pGender.equals("female") && iage>40){

mContentViews.setTextViewText(R.id.text, "Is a Female older than


40");

// Build the Notification

Notification.Builder notificationBuilder =
new Notification.Builder(getApplicationContext())
.setTicker(tickerText)
.setSmallIcon(R.drawable.samllnotification)
.setAutoCancel(true)
.setContentIntent(mContentIntent)
//.setSound(soundURI)
//.setVibrate(mVibratePattern)
.setContent(mContentViews);

// Pass the Notification to the NotificationManager


NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MY_NOTIFICATION_ID,
notificationBuilder.build());
}
}

Observations:
The Following snapshots show the execution of the application:
When user enter till button click:
Task 2: design an application which display a notification with sound and customize text.
When clicking on the notification it will take to your application another activity

You might also like