Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create Custom Notification in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 172
    Comment on it

     

    In the below example code I have created Custom Notification in android. Here first I have added ImageView and TextView in activity_main.xml layout. Then I have used NotificationManager class and NotificationService in MainActivity. You can see below example code it clearly describe you How to create Custom Notification in android.

     

    Step(1)activity_main.xml layout-

    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:id="@+id/layout"
    3. android:layout_width="fill_parent"
    4. android:layout_height="fill_parent"
    5. android:padding="10dp" >
    6. <ImageView android:id="@+id/image"
    7. android:layout_width="wrap_content"
    8. android:layout_height="fill_parent"
    9. android:layout_alignParentLeft="true"
    10. android:layout_marginRight="10dp" />
    11. <TextView android:id="@+id/title"
    12. android:layout_width="wrap_content"
    13. android:layout_height="wrap_content"
    14. android:layout_toRightOf="@id/image"
    15. android:textColor="#e22626"
    16. style="Custom Notification Title" />
    17. <TextView android:id="@+id/text"
    18. android:layout_width="wrap_content"
    19. android:layout_height="wrap_content"
    20. android:layout_toRightOf="@id/image"
    21. android:textColor="#2687e2"
    22. android:layout_below="@id/title"
    23. style="Custom Notification Text" />
    24. </RelativeLayout>

    Step(2)- MainActivity-
     

    1. public class MainActivity extends AppCompatActivity {
    2.  
    3.  
    4. @SuppressWarnings("deprecation")
    5. @Override
    6. public void onCreate(Bundle savedInstanceState) {
    7. super.onCreate(savedInstanceState);
    8. setContentView(R.layout.activity_main);
    9.  
    10. int icon = R.drawable.one;
    11. long when = System.currentTimeMillis();
    12. Notification notification = new Notification(icon, "Custom Notification", when);
    13.  
    14. NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    15.  
    16. RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.activity_main);
    17. contentView.setImageViewResource(R.id.image, R.drawable.three);
    18. contentView.setTextViewText(R.id.title, "Custom notification");
    19. contentView.setTextViewText(R.id.text, "Custom Text");
    20. notification.contentView = contentView;
    21.  
    22. Intent notificationIntent = new Intent(this, MainActivity.class);
    23. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    24. notification.contentIntent = contentIntent;
    25.  
    26. notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
    27. notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
    28. notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
    29. notification.defaults |= Notification.DEFAULT_SOUND; // Sound
    30.  
    31. mNotificationManager.notify(1, notification);
    32. }
    33.  
    34. @Override
    35. public boolean onCreateOptionsMenu(Menu menu) {
    36. getMenuInflater().inflate(R.menu.menu_main, menu);
    37. return true;
    38. }
    39. }
    40.  
    41.  

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: