Reputation: 87
I'm developing android notifications.
My notification has a progressbar and I want to show the percentage below the progressbar, notificationBuilder.setContentText(MyPercentage) area.
Showing progress percentage is not difficult issue.
but I want to show my percentage text with right alignment.
My situation is more complicated than that example,
Because I have to show another information with progress
like
============================================-------------------------------------------
blablablabla, orange, apple 50%
Is there any simple way to align my notification contentText right?
If not, then how can I implement notification contentText?
Upvotes: 1
Views: 2034
Reputation: 341
You can create a custom layout for your notification with two TextViews, where one is aligned to the right. Then create a RemoteViews object (notificationView) for that layout and set the builder's content accordingly: notificationBuilder.setContent(notificationView);
Here is a tutorial on creating custom notification layouts: http://www.laurivan.com/android-notifications-with-custom-layout/
Example
The notification 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:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_toRightOf="@+id/icon"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/the_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAppearance="@android:style/TextAppearance.Material.Notification.Title"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/some_text_left"
android:textAppearance="@android:style/TextAppearance.Material.Notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/some_text_right"
android:textAppearance="@android:style/TextAppearance.Material.Notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Creating notificationView:
private RemoteViews getNotificationView(){
RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.activity_temperature_notificaiton);
notificationView.setImageViewResource(R.id.icon, R.drawable.le_icon);
notificationView.setTextViewText(R.id.the_title, "OMGWTFBBQ");
notificationView.setTextViewText(R.id.some_text_left, "blablablabla, orange, apple");
notificationView.setTextViewText(R.id.some_text_right, "50%");
return notificationView;
}
Showing the notification:
private void addNotification() {
RemoteViews notificationView = getNotificationView();
if(notificationView == null)
return;
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.le_icon);
notificationBuilder.setContent(notificationView);
....
manager.notify(NOTIFICAITON_ID, notificationBuilder.build());
}
Upvotes: 1