I am suffering problem with too much notifications for my app and I am trying to divide single Notification to RemoteViews with multiple Views and serve swipe gesture on each of them (to dismiss single View), but I see only option to inflate View and setOnTouchListener on inflated View, but it doesn't detect touch on those views in any way:
public class CustomNotification {
RemoteViews remoteViewsSmall;
RemoteViews remoteViewsBig;
Context context;
public CustomNotification(Context context) {
this.context = context;
RemoteViews remoteViewsSmall = new RemoteViews(context.getPackageName(), R.layout.notification_main);
this.remoteViewsSmall = addLine(remoteViewsSmall);
RemoteViews remoteViewsBig = new RemoteViews(context.getPackageName(), R.layout.notification_main);
this.remoteViewsBig = addLine(remoteViewsBig);
LinearLayout linearLayoutParent = ((LinearLayout) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.notification_main, null));
linearLayoutParent.setOnTouchListener((v, event) -> detectMove(event));
for (int i = 0; i < linearLayoutParent.getChildCount(); i++) {
View childAt = linearLayoutParent.getChildAt(i);
childAt.setOnTouchListener((v, event) -> detectMove(event));
}
LinearLayout linearLayout1stLine = ((LinearLayout) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.notification_line, linearLayoutParent));
linearLayout1stLine.setOnTouchListener((v, event) -> detectMove(event));
for (int i = 0; i < linearLayout1stLine.getChildCount(); i++) {
View childAt = linearLayout1stLine.getChildAt(i);
childAt.setOnTouchListener((v, event) -> detectMove(event));
}
View viewBig = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(remoteViewsBig.getLayoutId(), null);
View viewSmall = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(remoteViewsSmall.getLayoutId(), null);
LinearLayout parentLinearLayoutBig = viewBig.findViewById(R.id.notificationParentLinearLayout);
parentLinearLayoutBig.setOnTouchListener((v, event) -> detectMove(event));
for (int i = 0; i < parentLinearLayoutBig.getChildCount(); i++) {
View childAt = parentLinearLayoutBig.getChildAt(i);
childAt.setOnTouchListener((v, event) -> detectMove(event));
}
LinearLayout parentLinearLayoutSmall = viewSmall.findViewById(R.id.notificationParentLinearLayout);
parentLinearLayoutSmall.setOnTouchListener((v, event) -> detectMove(event));
for (int i = 0; i < parentLinearLayoutSmall.getChildCount(); i++) {
View childAt = parentLinearLayoutSmall.getChildAt(i);
childAt.setOnTouchListener((v, event) -> detectMove(event));
}
}
private boolean detectMove(MotionEvent event) {
Log.i("TAG", event.getAction() + " detected");
return true;
}
private RemoteViews addLine(RemoteViews remoteView) {
RemoteViews remoteViewLine = new RemoteViews(context.getPackageName(), R.layout.notification_line);
remoteViewLine.setTextViewText(R.id.notification1stLine1stRowTxtView, "some string1");
remoteViewLine.setTextViewText(R.id.notification1stLine2ndRowTxtView, "some string2");
remoteView.addView(R.id.notificationParentLinearLayout, remoteViewLine);
return remoteViewLine;
}
}
That's the main parent layout of notification:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/notificationParentLinearLayout">
</LinearLayout>
This below is added in addLine method:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_marginStart="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/notification1stLine1stRowTxtView"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
tools:text="Example text" />
<TextView
android:id="@+id/notification1stLine2ndRowTxtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
tools:text="Example text" />
</LinearLayout>