0

I have an app in which I have to add a custom layout with imageview, textview and button. To do this I have used RemoteView. Everything is working fine but I want to add on click listener on button. I searched a lot but not found any solution for that issue. How do I do that?

private void customNotification(String title, int message, byte[] image) {
    // Using RemoteViews to bind custom layouts into Notification
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_card);
    builder = new NotificationCompat.Builder(this);

    builder.setSmallIcon(R.mipmap.ic_launcher_round);
    builder.setAutoCancel(false);
    builder.setOngoing(true);
    builder.setContent(remoteViews);
    builder.setCustomBigContentView(remoteViews);
    builder.setPriority(android.app.Notification.PRIORITY_MAX);

    Intent i = new Intent(context, CNotificationHomeActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(intent);

    // Locate and set the Image into customnotificationtext.xml ImageViews
    ByteArrayInputStream imageStream = new ByteArrayInputStream(image);
    Bitmap theImage = BitmapFactory.decodeStream(imageStream);
    remoteViews.setImageViewBitmap(R.id.imagenotileft, theImage);

    // Locate and set the Text into customnotificationtext.xml TextViews
    remoteViews.setTextViewText(R.id.title, title);
    if (message == 1) {
        remoteViews.setTextViewText(R.id.count_text, RewardUtil.fromHtml("<font color='#8AC34B'><b>" + message + "</b></font>" + " notification"));
    } else {
        remoteViews.setTextViewText(R.id.count_text, RewardUtil.fromHtml("<font color='#8AC34B'><b>" + message + "</b></font>" + " notifications"));
    }
    // Create Notification Manager
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Build Notification with Notification Manager
    notificationManager.notify(NOTIFICATION_ID, builder.build());

}

code of custom_notification_card.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="8dp">

<ImageView
    android:id="@+id/imagenotileft"
    android:layout_width="50dp"
    android:layout_height="50dp" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/clean"
    android:layout_alignBottom="@+id/clean"
    android:layout_marginLeft="14dp"
    android:layout_marginStart="14dp"
    android:layout_toEndOf="@+id/imagenotileft"
    android:layout_toLeftOf="@+id/clean"
    android:layout_toRightOf="@+id/imagenotileft"
    android:layout_toStartOf="@+id/clean"
    android:ellipsize="end"
    android:lines="1"
    android:text="Title"
    android:textColor="@android:color/black" />

<Button
    android:id="@+id/clean"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="clean"
    android:textAllCaps="true"
    android:textSize="12dp" />


<TextView
    android:id="@+id/count_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/image_list"
    android:textColor="@android:color/black"
    android:textSize="14dp" />

CDspace
  • 2,639
  • 18
  • 30
  • 36
Niraj
  • 31
  • 1
  • 10

0 Answers0