Advertisement
amjadArabia

SncakBar Types

Nov 22nd, 2017
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. package com.example.teacher.mysnackbarnotification;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.graphics.Color;
  9. import android.support.design.widget.Snackbar;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.support.v7.app.NotificationCompat;
  13. import android.view.View;
  14. import android.widget.Toast;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18.     Context context;
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.         this.context = this;
  25.     }
  26.  
  27.     public void onClick(View view) {
  28.         String message = "רק היום, מבצע פיצוץ, 8 קילו לפרצוף";
  29.         int myDuration = 5000;
  30.  
  31.         Snackbar.make(view, message, Snackbar.LENGTH_LONG) //create the snack bar like we create Toast
  32.                 .setAction("הבנתי כפרה", new View.OnClickListener() {
  33.                     @Override
  34.                     public void onClick(View view) {
  35.                         Toast.makeText(MainActivity.this, "תומר בדרך אלייך", Toast.LENGTH_LONG).show();
  36.                     }
  37.                 })
  38.                 .setActionTextColor(Color.YELLOW)  //set the color!!!
  39.                 .setDuration(myDuration)   //set the duration to display
  40.                 .show(); //show the snack bar
  41.     }
  42.  
  43.     private void createNotification(int nId, //Notification id
  44.                                     int iconRes, //icon resource from R file
  45.                                     String title, //notification title
  46.                                     String body) //notification body
  47.     {
  48.         NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) //createing to android support.v7.app.NotificationComapt
  49.                 new NotificationCompat.Builder (context)
  50.                 .setSmallIcon(iconRes)
  51.                 .setContentTitle(title)
  52.                 .setContentText(body);
  53.  
  54.         NotificationManager myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  55.         //nId -> notification id -> allow us to update the notification later on
  56.         myNotificationManager.notify(nId,notificationBuilder.build());
  57.     }
  58.  
  59.     private void createNotificationWithAction(int nId,int iconRes, String title, String body)
  60.     {
  61.         //first lets define the intent to triger when notifcation is selected
  62.         //start out by creating a normal intent (but we can use the present also a fragment)
  63.         Intent intent=new Intent(context,Main2Activity.class);
  64.         //next, let's turn this into a pending intent
  65.         //using: public static pandingIntent getActivity(Context context, int requestCode, Intent intent, int flag)
  66.         int requestId=(int)System.currentTimeMillis(); //unique request id to diff between various notification with same notification message
  67.         int flags = PendingIntent.FLAG_CANCEL_CURRENT; //cancel an old intent to create a new one
  68.         PendingIntent pendingIntent=PendingIntent.getActivity(this,requestId,intent,flags);
  69.        
  70.         //now we can attach the pending intent to a new notication use set content intent
  71.         Notification noti=new NotificationCompat.Builder(this)
  72.                 .setSmallIcon(iconRes)
  73.                 .setContentTitle(title)
  74.                 .setContentText(body)
  75.                 .setContentIntent(pendingIntent)
  76.                 .addAction(R.drawable.thumb_up, "רוצה קונה", pendingIntent)
  77.                 .addAction(R.drawable.thumb_down, "לא רוצה", pendingIntent)
  78.                 .setAutoCancel(true) //hides the notification after it been selected
  79.                 .build();
  80.  
  81.         //get the notification managar system service
  82.         NotificationManager myNotificationManagar = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  83.         //Nid allows us to update the notification later on......
  84.         myNotificationManagar.notify(nId,noti);
  85.     }
  86.  
  87.  
  88.     public void simpleNotification(View view)
  89.     {
  90.         createNotification(69,R.drawable.gas,"מבצע של מאוס גאוס", "רק היום, מבצע פיצוץ, 8 קילו לפרצוף");
  91.     }
  92.  
  93.     public void zeevikNotification(View view)
  94.     {
  95.         createNotificationWithAction(43,R.drawable.gas,"גז במחיר פגז","הזמן עכשיו, תומר בשטח כולם במתח (במקלטים)");
  96.     }
  97.  
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement