user10651308
user10651308

Reputation:

Android notification custom layout XML imageview not showing

<?xml version="1.0" encoding="utf-8"?>
<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="96dp"
    android:gravity="center"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:paddingStart="24dp"
        android:paddingBottom="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/pushIcon"
                android:layout_width="16dp"
                android:layout_height="16dp"
                app:srcCompat="@drawable/push_icon" />

            <TextView
                android:id="@+id/textView23"
                style="@style/TextAppearance.Compat.Notification.Info.Media"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="6dp"
                android:text="YOLOL"
                android:textColor="@color/browser_actions_title_color" />
        </LinearLayout>

        <TextView
            android:id="@+id/text_view_collapsed_1"
            style="@style/TextAppearance.Compat.Notification.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:text="New Message!"
            android:textColor="@color/colorPrimary" />

        <TextView
            android:id="@+id/text_view_collapsed_2"
            style="@style/TextAppearance.Compat.Notification.Info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Expand to show!" />
    </LinearLayout>

    <ImageView
        android:id="@+id/notifPreviewImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        app:srcCompat="@drawable/bb" />

</LinearLayout>

The pushIcon ImageView takes the place but is invisible, any idea why?

Upvotes: 0

Views: 646

Answers (1)

zuko
zuko

Reputation: 727

A quick tip that always helps in the debug process - add a background color to the view in question (also helps to set a specific Width and Height as well for testing) - it will enable you to see if the view is being rendered at all or not. If you see the background color, then the view is being rendered properly, but the image is not loading correctly. Without knowing this, it could be any combination of those issues.

With that aside, if you are dynamically changing the icon at runtime, I would look at your code there. If this is a static image then I would check that your resource file is a Vector drawable as that is only allowed when using app:srcCompat="@drawable/push_icon"

If your file is a non-vector drawable, then use android:src="@drawable/push_icon"

Here's a discussion that elaborates on this in more detail.

Upvotes: 5

Related Questions