Different ways for Notifying Users in Android

The 3 main different ways to notify an user are,

Dialog
Toast
Notification

Dialog

For notifying users with dialog, We will create an object of dialog. In this, I am removing the title using requestWindowFeature(Window.FEATURE_NO_TITLE) and adding a layout to the dialog to it using addContentView().

Toast

Second one is toast, that will flash and will display for a predefined period either as, Toast.LENGTH_SHORT or Toast.LENGTH_LONG.

Notification

Third one is notification, to notify user, about any background activity is started.

The complete sample programs is,

 
public class NotifyUser extends Activity {

LayoutParams panelParams, compParams, tvParams;
LinearLayout popupGrid;
LinearLayout buttonLayout;
TextView tv;
Button b1, b2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
displayDialog();
displayToast();
displayNotification();
}

public void displayDialog() {
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().getAttributes().width = 250;
popupGrid = new LinearLayout(this);
buttonLayout = new LinearLayout(this);
tv = new TextView(this);
b1 = new Button(this);
b2 = new Button(this);
panelParams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
panelParams.width = 250;
compParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
tvParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);

buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
popupGrid.setOrientation(LinearLayout.VERTICAL);
tv.setText("Are you sure?");
b1.setWidth(100);
b2.setWidth(100);
b1.setText("Yes");
b2.setText("No");
tvParams.setMargins(80, 0, 0, 0);
panelParams.setMargins(20, 0, 0, 0);
popupGrid.addView(tv, tvParams);
buttonLayout.addView(b1, compParams);
buttonLayout.addView(b2, compParams);
popupGrid.addView(buttonLayout, panelParams);
dialog.addContentView(popupGrid, panelParams);
dialog.show();
}

public void displayToast() {
Toast.makeText(this, "Sorry, please try again", Toast.LENGTH_SHORT)
.show();
}

public void displayNotification() {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Nithin";
CharSequence message = "Crazy About Android...";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
"Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(1, notif);
}
}



In notification, itself different forms are there. - when we click on the notification, it will take us to a new activity. For this, in pendingIntent, for the intent object, specify the acitivity, that we want to go.


PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, SampleActivity.class), 0);


Another form is, we can customize the notification layout using custom expanded view. For this create a layout in XML and use remoteView to use the XML. Use remoteView methods to set ImageView and TextView. The XML is,


android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
>
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000"
/>




The complete method is,


public void displayNotificationWithExandedView() {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.icon,
"Crazy About Android...", System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.notify_layout);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.text,
"Hello, this message is in a custom expanded view");
notif.contentView = contentView;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, SampleActivity.class), 0);
notif.contentIntent = contentIntent;
nm.notify(1, notif);
}



The screenshots are,







Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android

Difference between SurfaceView and View in Android