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
    • 162
    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-

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dp" >
        <ImageView android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="10dp" />
        <TextView android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image"
            android:textColor="#e22626"
            style="Custom Notification Title" />
        <TextView android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image"
            android:textColor="#2687e2"
            android:layout_below="@id/title"
            style="Custom Notification Text" />
    </RelativeLayout>

    Step(2)- MainActivity-
     

    public class MainActivity extends AppCompatActivity {
    
    
            @SuppressWarnings("deprecation")
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                int icon = R.drawable.one;
                long when = System.currentTimeMillis();
                Notification notification = new Notification(icon, "Custom Notification", when);
    
                NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    
                RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.activity_main);
                contentView.setImageViewResource(R.id.image, R.drawable.three);
                contentView.setTextViewText(R.id.title, "Custom notification");
                contentView.setTextViewText(R.id.text, "Custom Text");
                notification.contentView = contentView;
    
                Intent notificationIntent = new Intent(this, MainActivity.class);
                PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
                notification.contentIntent = contentIntent;
    
                notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
                notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
                notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
                notification.defaults |= Notification.DEFAULT_SOUND; // Sound
    
                mNotificationManager.notify(1, notification);
            }
    
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.menu_main, menu);
                return true;
            }
        }
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: