I am using Custom notification for my Notification. Below is the code for Notification,
RemoteViews collapseView = new RemoteViews(getPackageName(), R.layout.notification_layout);
collapseView.setImageViewResource(R.id.notifiction_icon,R.drawable.notify);
collapseView.setTextViewText(R.id.notification_title, "Custom notification");
collapseView.setTextViewText(R.id.notification_message, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb ccccccccccccccc dddddddddddddd eeeeeeeeeeeeeeeeeeeeee ffffffffffffffffffff");
RemoteViews expandView=new RemoteViews(getPackageName(),R.layout.notification_expandable);
expandView.setImageViewResource(R.id.notification_icon_expand,R.drawable.notify);
expandView.setTextViewText(R.id.notification_title, "Custom notification");
expandView.setTextViewText(R.id.notification_message, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb ccccccccccccccc dddddddddddddd eeeeeeeeeeeeeeeeeeeeee ffffffffffffffffffff");
int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
Log.d(TAG, "onStartCommand: Notification channel created");
Intent intent=new Intent(this,Activity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, channelId)
.setContent(collapseView)
.setContentIntent(pendingIntent)
.setCustomContentView(collapseView)
.setSmallIcon(R.drawable.notify)
.setDefaults(Notification.DEFAULT_ALL)
.setCustomBigContentView(expandView)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH);
manager.notify(m, notification.build());
Below is my collapse layout,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_height="match_parent">
<ImageView
android:id="@+id/notifiction_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="@+id/notification_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.Compat.Notification.Title"
android:layout_toRightOf="@id/notifiction_icon"
android:layout_marginLeft="8dp"/>
<TextView
android:id="@+id/notification_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:maxLines="2"
android:textAppearance="@style/TextAppearance.Compat.Notification"
android:layout_toRightOf="@id/notifiction_icon"
android:layout_below="@id/notification_title"/>
</RelativeLayout>
</RelativeLayout>
below is my expand layout,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:id="@+id/notification_icon_expand"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textAppearance="@style/TextAppearance.Compat.Notification.Title"
android:layout_toRightOf="@id/notification_icon_expand"
android:id="@+id/notification_title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@id/notification_icon_expand"
android:textAppearance="@style/TextAppearance.Compat.Notification"
android:id="@+id/notification_message"
android:layout_below="@id/notification_title"/>
</RelativeLayout>
I have read about custom notification here https://developer.android.com/training/notify-user/custom-notification In that mentioned a caution for custom notification, eventhough I used fully custom notification. Because I dont want my small icon to be visible as it provides grey color. Here is my previous question regarding that Setting small icon in Notification above lollipop version So I end up with using Fully created custom notification.
When I tested this notification in my devices I found a issue.
a) If Notification contains single line, then there is extra space in bottom. b) If Notification contains multiple lines, then there is no space at the bottom which looks bad.
How to align my notification to center? So that has some decent space at the top and bottom of the notification.
below is my screenshot for this,
3)In regular notification, if notification contains long text, then to denote users about that there are some dots in end of the text. For example like (aaaaaaa bbbbbbb ccccccc...) in collapse view How to achieve that in Custom notification??
- When expanding notification and moving back to collapsed notification instead of getting two lines(As I set max lines to 2), my notification text is cutting off. I know that collapsed notification height is 64dp and expanded height is 256 dp. For just two lines also text cutting off. I am wondering why?
I didn't find any solutions for those issues. Thanks in advance.. Any help will be appreciated.
Also if required any other clarification to answer these questions please ask me.