Mohammad H
Mohammad H

Reputation: 805

Create custom notification including Edit text android

I wanna create a custom notification to reply SMS directly from notification like this:

enter image description here

As I have understood normal notifications must have 64dp height but you can use bigger one from API >16 as expandable notification but I think 64dp height is suitable for my case. I used this code but it crashes when my custom notification layout has edit text:

RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.widget);
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContent(remoteViews);
                Intent resultIntent = new Intent(context, MainActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addParentStack(MainActivity.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(100, mBuilder.build());

Error:

android.app.RemoteServiceException: Bad notification posted from package com.example.x: Couldn't expand RemoteViews for: StatusBarNotification

What should I do?

Upvotes: 2

Views: 1765

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006799

Create custom notification including Edit text android

You cannot put an EditText widget into a Notification, app widget, or anything else that uses RemoteViews.

why?

Because that is how RemoteViews is written. You are limited to certain widgets and containers.

what should I do to make that custom notification?

Re-design it to not involve an EditText.

UPDATE: On Android 7.0+, you can use a MessagingStyle with RemoteInput to accept input from the user from a Notification. This does not match the requirements of the question, but it is the closest option.

Upvotes: 5

Related Questions