नोटिफिकेशन में EditText — केवल डिस्प्ले, इंटरेक्टिव नहीं
यह आर्टिकल पिछले (ID 1005061585) के समान है — कस्टम RemoteViews लेआउट में EditText डालने का उदाहरण।
चरण 1-4: वही सेटअप
प्रोजेक्ट, मेन लेआउट, कस्टम लेआउट (EditText के साथ), MainActivity, मैनिफेस्ट — सभी पिछले आर्टिकल जैसे।
मुख्य कोड (पुनरावृत्ति)
// कस्टम लेआउट res/layout/custom_notification_layout.xml में EditText // MainActivity में: RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout); mBuilder.setContent(contentView);
❌ यह काम नहीं करता: EditText में टाइप करना
RemoteViews एंड्रॉइड का सीरियलाइज़्ड व्यू रिप्रेजेंटेशन है — लाइव व्यू नहीं। इसमें:
- कोई टच/फोकस इवेंट नहीं
- कीबोर्ड नहीं खुलता
- टेक्स्ट चेंज लिसनर नहीं
✅ सही समाधान: Direct Reply (RemoteInput)
Android 7.0 (API 24) से RemoteInput के साथ इंलाइन रिप्लाई:
// 1. RemoteInput बनाएं
RemoteInput remoteInput = new RemoteInput.Builder("reply_text")
.setLabel("Reply")
.setChoices(new String[]{"Yes", "No", "Later"}) // ऑप्शनल क्विक रिप्लाई
.build();
// 2. PendingIntent — रिप्लाई हैंडल करने वाली एक्टिविटी/ब्रॉडकास्ट
Intent replyIntent = new Intent(this, ReplyReceiver.class);
PendingIntent replyPending = PendingIntent.getBroadcast(this, 0, replyIntent, PendingIntent.FLAG_IMMUTABLE);
// 3. एक्शन में RemoteInput जोड़ें
NotificationCompat.Action action = new NotificationCompat.Action.Builder(
R.drawable.ic_reply, "Reply", replyPending)
.addRemoteInput(remoteInput)
.setAllowGeneratedReplies(true)
.build();
mBuilder.addAction(action);
4. रिप्लाई प्राप्त करें (BroadcastReceiver या Activity में)
public class ReplyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
String replyText = remoteInput.getCharSequence("reply_text").toString();
// replyText प्रोसेस करें
}
}
}
मैनिफेस्ट में रिसीवर रजिस्टर करें
<receiver android:name=".ReplyReceiver" />
निष्कर्ष
कस्टम लेआउट में EditText डालना केवल UI मॉकअप के लिए ठीक है। वास्तविक टेक्स्ट इनपुट के लिए Direct Reply (RemoteInput) उपयोग करें — यह सिस्टम हैंडल करता है, कीबोर्ड दिखाता है, और रिजल्ट आपके ऐप को देता है।