Android Notification Simple Example

MainActivity.java
—————

package com.example.toastandcustomtoast;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationCompat.InboxStyle;
import android.support.v4.app.TaskStackBuilder;
import android.widget.TextView;

public class NotificationActivity extends Activity
{
@SuppressLint(“ServiceCast”)
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

LayoutParams layoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView textView=new TextView(getApplicationContext());
textView.setText(“Android Notification Example”);

NotificationCompat.Builder builder=new Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentText(“new message from Daddy”);
builder.setContentTitle(“email”);
Intent i=new Intent(this,MainActivity.class);

//coment next 8 lines for 1st run
NotificationCompat.InboxStyle inboxStyle=new InboxStyle();
String[] stringList=new String[12];
inboxStyle.setBigContentTitle(“ur notifications”);
for(int i1=0;i1 < stringList.length;i1++)
{
inboxStyle.addLine(“”+i1);
}
builder.setStyle(inboxStyle);

TaskStackBuilder taskStackBuilder= TaskStackBuilder.create(getApplicationContext());
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(i);
PendingIntent pendingIntent=taskStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT) ;

NotificationManager otificationManager =
(NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
otificationManager.notify(0, builder.build());

}
}

 

References:

Reference1

Ref2

 

 

 

 

 

Advertisements

Android Normal And Custom Toast Example

Toasts:
——-

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a “Draft saved” toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.

Defination:
———-

A toast is a view containing a quick little message for the user. The toast class helps you create and show those.When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two examples are the volume control, and the brief message saying that your settings have been saved.

Notifications:
————-

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.

Toasts vs Notifications vs Dialogs in android:
———————————————-

Android has different ways for apps to give you information or get your attention, and they all have different names. App developers need to know the differences, and when to use each one, so they can code the correct behaviour in their apps. Even as an end user, if you want to troubleshoot a problem, find which app is giving you messages, or describe some behaviour (for example, when reporting a bug to a developer), it saves you time and confusion if you can call each thing by its correct name.

Notifications
Notification bar

Notifications are the most common feedback mechanism on Android. They look like icons in the top-left of the screen (the notification bar). On Honeycomb and ICS tablets, they instead show up in the system bar, in the bottom-right next to the clock.

Notifications in the system bar

Some really unhelpful notifications

In Jellybean and above, once you’ve expanded the notification drawer (like in the screenshot), you can long-tap a notification to get a menu with one item, App info. Clicking this item tells you which app is responsible for the notification. You can also stop this app showing any further notifications from that screen.

Some apps create notifications to advertise things at you. This is now (since September 2013) banned under Google Play’s rules, so you should report as malicious any apps that do this, from the app’s page in the Play Store app.

Often your phone will play a sound or will vibrate with a notification, but not always: it’s up to the app which created it.

Toasts
A toast confirming that an action has been performed

A toast is a small message that shows up in its own box near the bottom of the screen, and disappears on its own after a few seconds. It’s usually a response to an action you’ve just performed, but in fact a toast can come from any app, not just the one in the foreground. A toast can’t interact with you (you can’t click on it or type into it), so there’s no need to worry about it “stealing focus” like on a PC. There’s no indication of which app created the toast.

Dialogs
A dialog requests confirmation

A window that appears, with buttons you can click or a list of options to choose from, is called a dialog box, or dialog for short. (It’s spelt the American way even in British English.) They almost always relate to the foreground app. Strictly speaking, from a developer’s point of view a “dialog” is any non-fullscreen activity window, but they’re usually used for simple confirmations or choices, like in the screenshot.

Why Notifications over Toast:
—————————–

A toast would not be appropriate if the user is expected to respond because it only appears briefly and cannot be recalled. In this situation, the Android docs recommend using a Notification.

Example Code:
————-

package com.example.toastandcustomtoast;

import com.example.toastandcustomtoast.R.layout;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
TextView textView;
Button buttonNormalToast,buttonCustomToast;
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView=(TextView)findViewById(R.id.textView1);
buttonNormalToast=(Button)findViewById(R.id.button1);

//Toast Will be displayed When U click the button

buttonNormalToast.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), “Iam The Toast Bro….”, Toast.LENGTH_LONG).show();
}
});

buttonCustomToast.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

//code for custom toast
LayoutInflater layoutInflater=getLayoutInflater();
//registrering to inflate the view
//u can use getSystemService()

View view=layoutInflater.inflate(R.layout.toast_layout, (ViewGroup)findViewById(R.id.toast_layout_root));

TextView text = (TextView) view.findViewById(R.id.text);
text.setText(“This is a custom toast”);

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();

}
});

}

}

————————————–
main.xml

————————————-
toast.xml

links:

—-http://developer.android.com/reference/android/widget/Toast.html–

http://android.stackexchange.com/questions/52789/popups-dialogs-notifications-toasts-whats-the-difference