user notification android club 2015. agenda toast custom toast notification dialog
TRANSCRIPT
Toast: example
• Toast.makeText(getApplicationContext(), “Hello Android”, Toast.LENGTH_LONG).show();
Toast: practice
• Create new button• Text: Say hello• Set OnClickListener• Toast: “Hello [YOUR_NAME]!”
Custom Toast: example
Toast toast = new Toast(getApplicationContext());toast.setDuration(Toast.LENGTH_LONG);View view = getLayoutInflater().inflate(R.layout.toast_hello, null);toast.setView(view);toast.show();
Custom Toast: practice
• Create new Button• Text: Say hello• Set OnClickListener• OnClick: show one small image and
text• For example: Just do it!
Notification 5 steps
• Create PendingIntent• Create Notification• setLatestEventInfo()• NotificationManager• Notify
Notification: example
Intent intent = new Intent(this, MainActivity.class);PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification(R.drawable.friend, "Text1", System.currentTimeMillis());// Set the info for the views that show in the notification panel.notification.setLatestEventInfo(getApplicationContext(), "Text2", "Text3", pending);
// Send the notification.NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);manager.notify(0, notification);
Notification: practice
• Create PendingIntent which goes to FriendActivity
• Ticker text: “Friend: hello”• Title: Your friend name• Content: Message content
AlertDialog step-by-step
• Create AlertDialog builder• setTitle• setMessage• setPositiveButton• setNegativeButton• Create AlertDialog from builder• show
AlertDialog: example
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( MainActivity.this);
// set titlealertDialogBuilder.setTitle("Delete");
// set dialog messagealertDialogBuilder .setMessage("Do you want to delete this file?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // if this button is clicked, close // current activity MainActivity.this.finish(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } });
// create alert dialogAlertDialog alertDialog = alertDialogBuilder.create();
// show italertDialog.show();
AlertDialog: practice
• Show AlertDialog• Title: Exit app• Message: Do you want to exit app• Positive: Exit• Negative: No, I love this app
Progress Dialog: example
ProgressDialog dialog = ProgressDialog.show(this, "Downloading", "Downloading very big file", true);dialog.setCancelable(true);dialog.show();
ProgressDialog: practice
• Show ProgressDialog• Title: Distance• Message: Calculating Distance from
Tashkent to Tokyo• Cancelable: true
Homework 2: Custom Toast
• Create layout for toast• Put text: This is toast• Put image: Toaster image• Show custom Toast using that layout
Homework 3: Notification
• Show notification• Icon: GooglePlay• Ticker text: Updating app• Message: Updating telegram app• Content: Updating telegram app to
the new version: 2.2.2
Homework 4: AlertDialog
• Title: Terms of service• Message: Are you agree with terms of
service?• Positive: I agree (open new activity)• Negative: I do not agree (close app)• setCancelable: false