I'm trying to create a custom notification that has 4 ImageViews in it. When I try to add the images, if I add only one it works, but when I try to add a second one it shows nothing.
Here is the function where I create the notification
private void createNotification() {
Intent intent = new Intent(this, RateDia.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
RemoteViews layoutNotificacio = new RemoteViews(getPackageName(), R.layout.notificacio_gran);
layoutNotificacio.setImageViewBitmap(R.id.imatge1, BitmapFactory.decodeResource(getResources(), R.drawable.e01_grinning_face));
layoutNotificacio.setImageViewBitmap(R.id.imatge2, BitmapFactory.decodeResource(getResources(), R.drawable.e02_grimacing_face));
layoutNotificacio.setImageViewBitmap(R.id.imatge3, BitmapFactory.decodeResource(getResources(), R.drawable.e03_grinning_face_with_smiling_eyes));
layoutNotificacio.setImageViewBitmap(R.id.imatge4, BitmapFactory.decodeResource(getResources(), R.drawable.e04_face_with_tears_of_joy));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.mimismo_icono_notificacio)
.setContentTitle("Notificación de miMismo")
.setCustomBigContentView(layoutNotificacio)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
And here is the xml for the Remote View:
<LinearLayout 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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imatge1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:maxWidth="50dp"
android:src="@drawable/e01_grinning_face" />
<ImageView
android:id="@+id/imatge2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:maxWidth="50dp"
android:src="@drawable/e02_grimacing_face" />
<ImageView
android:id="@+id/imatge3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:maxWidth="50dp"
android:src="@drawable/e04_face_with_tears_of_joy" />
<ImageView
android:id="@+id/imatge4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:maxWidth="50dp"
android:src="@android:drawable/ic_menu_add" />
</LinearLayout>
The source of the images is set for testing the layout, but must be changed programmatically and there is where the problem occurs.
As you can see, there are 4 lines like this:
layoutNotificacio.setImageViewBitmap(R.id.imatge1, BitmapFactory.decodeResource(getResources(), R.drawable.e01_grinning_face));
If I comment or delete three of them, I'll see an image in the notification. If I have 2 or more nothing is shown.
Any help would be appreciated.
are inside Drawables
If you mean res/drawable/
, stop using that directory. That is a synonym for res/drawable-mdpi/
, and it means Android may be scaling your images when that is not needed or wanted. Instead, either put the images in res/drawable-anydpi/
(to say that they do not need to be scaled based on screen density) or set up per-density versions of your images in the proper density-specific directories (e.g., res/drawable-hdpi/
, res/drawable-xhdpi/
).
To your original problem, try using setImageViewResource()
instead of setImageViewBitmap()
. This will simplify your code and dramatically reduce the amount of memory used in your RemoteViews
update.