How to create custom notification layouts and text colors in Android?


This example demonstrates how to create custom notification layouts and text colors in Android .

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello World!"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Step 3 − Add the following code to src/MainActivity.java

package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.os.Bundle;
import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.notificationmain);
      Button bnotify = (Button) findViewById(R.id.notification);
      Button bcustomnotify = (Button) findViewById(R.id.customnotification);
      bnotify.setOnClickListener(new OnClickListener() {
         public void onClick(View arg0) {
            Notification();
         // TODO Auto-generated method stub
         }
      });
      bcustomnotify.setOnClickListener(new OnClickListener() {
         public void onClick(View arg0) {
            CustomNotification();
            // TODO Auto-generated method stub
         }
      });
   }
   public void Notification() {
      // Set Notification Title
      String strtitle = getString(R.string.notificationtitle);
      String strtext = getString(R.string.notificationtext);
      Intent intent = new Intent(this, NotificationView.class);
      intent.putExtra("title", strtitle);
      intent.putExtra("text", strtext);
      PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
         .setSmallIcon(R.drawable.logosmall)
         .setTicker(getString(R.string.notificationticker))
         .setContentTitle(getString(R.string.notificationtitle))
         .setContentText(getString(R.string.notificationtext))
         .addAction(R.drawable.ic_launcher, "Action Button", pIntent)
         .setContentIntent(pIntent) .setAutoCancel(true);
      NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      notificationmanager.notify(0, builder.build());
   }
   public void CustomNotification() {
      RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.customnotification);
      String strtitle = getString(R.string.customnotificationtitle);
      String strtext = getString(R.string.customnotificationtext);
      Intent intent = new Intent(this, NotificationView.class);
      intent.putExtra("title", strtitle);
      intent.putExtra("text", strtext);
      PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
         .setSmallIcon(R.drawable.logosmall)
         .setTicker(getString(R.string.customnotificationticker))
         .setAutoCancel(true) .setContentIntent(pIntent)
         .setContent(remoteViews);
      remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
      remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.androidhappy);
      remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
      remoteViews.setTextViewText(R.id.text,getString(R.string.customnotificationtext));
      // Create Notification Manager
      NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      // Build Notification with Notification Manager
      notificationmanager.notify(0, builder.build());
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
}

Step 4 − Add the following code to res/layout/notification.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <Button
      android:id="@+id/notification"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:text="Notification - 1" />
   <Button
      android:id="@+id/customnotification"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_below="@+id/notification"
      android:text="Custom Notification - 1" />
</RelativeLayout>

Step 5 − Add the following code to res/layout/customnotification.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <ImageView
      android:id="@+id/imagenotileft"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
   <TextView
      android:id="@+id/title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@+id/imagenotileft" />
   <TextView
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/title"
      android:layout_toRightOf="@+id/imagenotileft" />
   <ImageView
      android:id="@+id/imagenotiright"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:padding="10dp" />
</RelativeLayout>

Step 6 − Add the following code to res/layout/notificationview.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   <TextView
      android:id="@+id/lbltitle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/lbltitle" />
   <TextView
      android:id="@+id/lbltext"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/lbltitle"
      android:text="@string/lbltext" />
   <TextView
      android:id="@+id/title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@+id/lbltitle" />
   <TextView
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/title"
      android:layout_toRightOf="@+id/lbltext" />
</RelativeLayout>

Step 7 − Add the following code to Manifests/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.app.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Click here to download the project code.

Azhar
Azhar

e

Updated on: 15-Nov-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements