2

I am trying to display the drawable text view in an Android notification but the text is being displayed, but the drawable is not being displayed. I want the drawable image to be displayed along with the text message.

I have tried adjusting the padding padding. I have set the setCompoundDrawablesWithIntrinsicBounds for a text view inside a remote view but I am not able to the see the drawable icon.

Layout

<RelativeLayout android:id="@+id/notification_carousel"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:paddingBottom="8dp"
    android:paddingTop="8dp">
    <TextView
        android:id="@+id/notification_action_error"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:textSize="16sp"
        android:gravity="bottom"
        android:textAlignment="gravity"
        android:textStyle="bold"
        android:drawableStart="@drawable/ic_error"
        android:layout_above="@+id/notification_action_list"
        android:layout_marginStart="16dp"
        android:visibility="visible"
        android:text="Error message to be displayed"
        android:layout_marginEnd="16dp" />

    <!--buttons-->
    <Button
        android:id="@+id/notification_action_list"
         android:layout_width="match_parent"
        android:layout_height="36dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp" />

</RelativeLayout>

Notification Widget code layout

Context context = ... // Initialize context
String channelId = ... // Initialise channel Id.
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);

RemoteViews rootView = new RemoteViews(context.getPackageName(), R.layout.notificiation);
rootView.setViewVisibility(R.id.notification_action_error, View.VISIBLE);

rootView.setTextViewCompoundDrawables(R.id.notification_action_error, 0, 0, 0, 0);

I want to see the drawable image but I don't see it. Here is the screenshot of the notification which contains the actual notification and the preview.

I also tried

rootView.setTextViewCompoundDrawables(R.id.notification_action_error, R.layout.ic_error, 0, 0, 0);

after removing the android:drawable attribute from the text view, but I get the same value.

https://i.stack.imgur.com/oIK6O.jpg

Krish
  • 63
  • 6
  • Nice question here ... and welcome to upvote levels. Which allows you to show your appreciation for helpful content, even when it doesnt answer your question. – GhostCat Jun 09 '19 at 08:01

1 Answers1

-1

On nofication builder you can call these methods to display notification icons.

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_icon))
            .setSmallIcon(R.drawable.app_icon)
Faisal
  • 705
  • 3
  • 16