android - how to set the value in custom notification textview -
please me set text in textview in custom notification
private void startnotification() { string ns = context.notification_service; notificationmanager notificationmanager = (notificationmanager) getsystemservice(ns); notification notification = new notification(r.drawable.ic_launcher, null, system.currenttimemillis()); remoteviews notificationview = new remoteviews(getpackagename(),r.layout.mynotification); mremoteviews.settextviewtext(r.id.appname, getresources().gettext(0,"gfhf")); mbuilder.setcontent(mremoteviews); intent notificationintent = new intent(this, flashlight.class); pendingintent pendingnotificationintent = pendingintent.getactivity(this, 0, notificationintent, 0); notification.contentview = notificationview; notification.contentintent = pendingnotificationintent; notification.flags |= notification.flag_no_clear; intent switchintent = new intent(this, switchbuttonlistener.class); pendingintent pendingswitchintent = pendingintent.getbroadcast(this, 0, switchintent, 0); notificationview.setonclickpendingintent(r.id.closeonflash, pendingswitchintent); notificationmanager.notify(1, notification); }
java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.remoteviews.settextviewtext(int, java.lang.charsequence)' on null object reference
please me how resolve . how set value on textview in notification in android , in advance .
check this:
private void startnotification() { int icon = r.drawable.icon_48; long when = system.currenttimemillis(); notification notification = new notification(icon, "custom notification", when); notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(notification_service); remoteviews contentview = new remoteviews(getpackagename(), r.layout.custom_notification); contentview.setimageviewresource(r.id.image, r.drawable.icon_48); contentview.settextviewtext(r.id.title, "notification title/app name"); contentview.settextviewtext(r.id.text, "message"); notification.contentview = contentview; intent notificationintent = new intent(this, yourclass.class); pendingintent contentintent = pendingintent.getactivity(this, 0, notificationintent, pendingintent.flag_update_current); notification.contentintent = contentintent; notification.flags |= notification.flag_auto_cancel; notification.defaults |= notification.default_lights; // led notification.defaults |= notification.default_vibrate; //vibration notification.defaults |= notification.default_sound; // sound mnotificationmanager.notify(1, notification); }
custom_notification.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" android:padding="10dp"> <imageview android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignparentleft="true" android:layout_marginright="10dp" /> <textview android:id="@+id/title" style="custom notification title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_torightof="@id/image" android:textsize="18sp" android:textcolor="#336ba4" /> <textview android:id="@+id/text" style="custom notification text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/title" android:layout_torightof="@id/image" android:textsize="16sp" android:textcolor="#336ba4" /> </relativelayout>
Comments
Post a Comment