How to use a drawable with adjustable text as notification action icon - android

What I need
What I want to achieve is something like
where it should be possible to programmatically set the number displayed within the icon.
I.e. I want a notification action icon with a programmatically adjustable text.
Is there a simple way to achieve this? It does not seem to be possible to add a TextView to an android drawable and set the text when I'm building my notification. What other ways are there?
Code
The rough code for building my notification is (although I think it's not necessary for answering the question):
private void buildNotification() {
createNotificationChannel();
Intent playbackActionIntent = new Intent(this, MediaPlayerService.class);
playbackActionIntent.setAction(ACTION_BACKWARD);
PendingIntent backwardAction = PendingIntent.getService(this, 3, playbackActionIntent, 0);
String audioTitle = "testTitle";
String albumTitle = "testAlbumTitle";
// Create a new Notification
mNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
// Hide the timestamp
.setShowWhen(false)
// Set the Notification style
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
// Attach our MediaSession token
.setMediaSession(mediaSession.getSessionToken())
// Show our playback controls in the compat view
.setShowActionsInCompactView(0, 1, 2))
// Set the Notification color
.setColor(getResources().getColor(R.color.colorAccent))
.setSmallIcon(R.drawable.ic_notification_new)
// Set Notification content information
.setContentText(albumTitle)
.setContentTitle(audioTitle)
// Set the visibility for the lock screen
.setVisibility(VISIBILITY_PUBLIC)
// Add playback actions
.addAction(R.drawable.ic_media_backward, "backward", backwardAction)
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
}
}
So for R.drawable.ic_media_backward I want a drawable with an adjustable text as in the linked image.
Any help is appreciated! So far I could not find anything that would solve my problem.

Related

NotificationCompat android - how to show only Large icon without small

When I add notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.plus)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(text)
.setSound(RingtoneManager .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(bm);
I see large icon and small in it:
How can I set only large Icon, without small.
If use only setLargeIcon, I don't see notification at all, just sound alert.
Small icon is mandatory.
If you don't set a large one you'll get your small icon bigger in the middle of the circle with the color of your choice (setColor).
If i were you i'd put that blank E on a transparent background for smallicon, and set a red color for the circle.
get the small icon id and then try to hide it
int smallIconId = ctx.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconId != 0) {
if (notification.contentView!=null)
notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
}
try looking at this post it will help too
i test the code on api 18,23 (samsung j1,galaxy S6) work fine
Based on the previous answer you can also hide the expended view too :
int smallIconId = AnghamiApp.getContext().getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconId != 0) {
notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
notification.bigContentView.setViewVisibility(smallIconId, View.INVISIBLE);
}
You can create a custom notification and then show whatever you want in the large notification area.
See and example here TutorialsFace: Build all types of Notifications in Android
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Test")
.setContentText("Hii There")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.smallicon))
.setAutoCancel(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(123, notification);
}
}

Android notification: the top notification is not expanded

According to the code below, I create a notification which is expandable.
private void bannerNotif() {
Notification foregroundNote;
RemoteViews bigView = new RemoteViews(getApplicationContext()
.getPackageName(), R.layout.banner_notif);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.featuredimagehandler);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
this);
foregroundNote = mNotifyBuilder.setContentTitle("Title")
.setContentText("Description")
.setSmallIcon(R.drawable.app_icon).setLargeIcon(icon).build();
foregroundNote.bigContentView = bigView;
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyManager.notify(2, foregroundNote);
}
According to this document
A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture.
the problem is when this notification is placed on top, it is not expanded.
I had this problem and the issue was that the layout I created for the notification had a height of "MATCH_PARENT" so it was too big to fit. Setting the height to a reasonable value solved the issue and the notification expanded.
I also had something similar with remoteviews and notifications. Mine ended up not expanding because my notification_id was not unique throughout my app :)

How to increase the height of remoteview in android notification

I am trying to create a notification with custom RemoteView.But when displaying notification,bottom of the RemoteView is getting cropped.Only half of the remote view is visible in the notification.Could any one tell me about increasing the height of notification in android?
First of all, Notification height is limited to 256dp on Android. Views will be clipped at the bottom that exceed this height. Second, to create a custom Big Notification layout, use RemoteViews like this:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_notification_icon)
RemoteViews content = new RemoteViews(this.getPackageName(), R.layout.content);
Notification notif = builder.build();
notif.bigContentView = content;
notificationManager.notify(NOTIFICATION_ID, notif);
Set the style of the notification intent to bigstyle,
Notification noti = new Notification.Builder(this).setStyle(new Notification.BigTextStyle().bigText(longText))

how to show multiple line notification in android?

I doing one notification with multiple line, code like:
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Home.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher).setContentTitle("");
//
// Set Big View style to see full content of the message
NotificationCompat.BigTextStyle inboxStyle =
new NotificationCompat.BigTextStyle();
inboxStyle.setBuilder(mBuilder);
inboxStyle.bigText(msg);
inboxStyle.setBigContentTitle("");
inboxStyle.setSummaryText("");
// Moves the big view style object into the notification object.
mBuilder.setStyle(inboxStyle);
mBuilder.setContentText(msg);
mBuilder.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
But it's only show one line notification, I would like it show all content of notification with multiple line. Can you help me?
Thanks you so much
There are three styles to be used with the big view: big picture style, big text style, Inbox style. The following code demonstrates the usage of the BigTextStyle() which allows to use up to 256 dp.
String longText = "...";
Notification noti = new Notification.Builder(this).
.....
.setStyle(new Notification.BigTextStyle().bigText(longText))
for more reference ,check this reference link
Yes it can be done. Just use the Big View for notification. See the official documentation.
Here is a good tutorial by vogella.
What you are looking for is Big view in Notifications:
https://developer.android.com/guide/topics/ui/notifiers/notifications.html#BigNotify
try to add both SetStyle and SetPriorityas as the following :
.SetStyle(new Notification.BigTextStyle().BigText(message.Long_Message))
.SetPriority((int)NotificationPriority.Max)
it is working fine for me :)

what to code for notification to stick until music stop playing

I want notification which will stick on notification bar until music stop playing it.
currently i have written some code in which i can show notification but when i press clear notification button or swap it than it disappear from notification center.
I want notification like spotify which stays on bar until you stop playing music.
Here is my code for notification
int pendingRequestCode = 0;
// final Resources res = getResources();
notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
Intent i = new Intent(getApplicationContext(),Mainactivity.class);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_search)
.setAutoCancel(true)
.setTicker("test ckick")
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), NOTIFICATION_DEFAULT, i,0));
// Sets a custom content view for the notification, including an image button.
layout = new RemoteViews(getPackageName(), R.layout.notification);
layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
Intent clickIntent = new Intent();
clickIntent.setAction(ACTION_DIALOG);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
builder.setContent(layout);
// Notifications in Android 3.0 now have a standard mechanism for displaying large
// bitmaps such as contact avatars. Here, we load an example image and resize it to the
// appropriate size for large bitmaps in notifications.
layout.setImageViewResource(R.id.notification_button, R.drawable.pause);
notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
waiting for reply
Use setOngoing(true) to indicate that the event is ongoing. You may also wish to remove setAutoCancel(true), as that clears the Notification when the user taps upon it.
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

Categories

Resources