Reputation: 13
I am using following code to generate notifications with two action button and some details. Please help me, why i am not able to see action button in notification?
public static void sendNotification(Context mContext, int mode, String title, String message) { try { RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification);
// Set Notification Title
String strtitle = "Instevent";
// Set Notification Text
String strtext = "Event has created";
// Open NotificationView Class on Notification Click
Intent intent = new Intent(mContext, MainActivity.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
// Set Icon
.setSmallIcon(R.drawable.ic_address_book)
// Set Ticker Message
.setTicker("Ticker message")
// Dismiss Notification
.setAutoCancel(true)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Set RemoteViews into Notification
.setContent(remoteViews);
// Locate and set the Image into customnotificationtext.xml
// ImageViews
remoteViews.setImageViewResource(R.id.nfImage, R.drawable.ic_action_accept);
remoteViews.setTextViewText(R.id.nfEventName, "Custom notification");
remoteViews.setTextViewText(R.id.nfEventTime, "This is a custom layout");
remoteViews.setTextViewText(R.id.nfEventAddress, "This is a custom layout");
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
catch (Exception e)
{
e.printStackTrace();
Log.print("Notification====>>", e.getMessage());
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.instevent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/rlMainNotification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_rounded_white"
android:padding="10dp" >
<ImageView
android:id="@+id/nfImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:src="@drawable/ic_group" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_toEndOf="@+id/nfImage"
android:layout_toRightOf="@+id/nfImage"
android:orientation="vertical" >
<TextView
android:id="@+id/nfEventName"
style="@style/BlackTextN"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true"
android:text="The Big Meeting"
android:textSize="16sp" />
<TextView
android:id="@+id/nfEventTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="7dp"
android:paddingRight="5dp"
android:singleLine="true"
android:text="4:15 - 5:15 PM"
android:textColor="#8b8b8b"
android:textStyle="normal" />
<TextView
android:id="@+id/nfEventAddress"
style="@style/GrayColorN"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:paddingLeft="7dp"
android:paddingRight="5dp"
android:singleLine="true"
android:text="The Big Conference Room"
android:textColor="#8b8b8b"
android:textStyle="normal" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="5dp"
android:background="#dcdcdc" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="@+id/nfDecline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/ic_action_cancel"
android:drawablePadding="10dp"
android:drawableStart="@drawable/ic_action_cancel"
android:gravity="center_vertical"
android:text="@string/decline"
android:textColor="#676767" />
<TextView
android:id="@+id/nfAccept"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/ic_action_accept"
android:drawablePadding="10dp"
android:drawableStart="@drawable/ic_action_accept"
android:gravity="center_vertical"
android:text="@string/accept"
android:textColor="#676767" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Upvotes: 1
Views: 4423
Reputation: 56
I had a similar error in my App. The problem was that RemoteViews does not support every kind of layout or view.
In your case, I think the problem is the <View/>
part in your custom_notification.xml.
Try removing or replacing it with a supported layout element. You can find them here: http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
Upvotes: 1