Kapil
Kapil

Reputation: 1810

Custom Notification with dynamic layout in remote view

I am creating custom notification with dynamic layout instead of xml layout in remote views but end up with following error.

03-12 14:42:59.907: E/AndroidRuntime(2399): android.app.RemoteServiceException: Bad notification posted from package com.example.notificationexample: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.example.notificationexample id=0 tag=null score=0 notn=Notification(pri=0 contentView=com.example.notificationexample/0x1e vibrate=null sound=null defaults=0x0 flags=0x10 kind=[null]))
03-12 14:42:59.907: E/AndroidRuntime(2399):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at android.os.Looper.loop(Looper.java:137)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at android.app.ActivityThread.main(ActivityThread.java:4905)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at java.lang.reflect.Method.invoke(Method.java:511)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)

I have seen many questions with almost same problem but they are using xml in layout. I am using dynamic layout. Here is my code:

public void CustomNotification() {


        //creating Custom Layout
        RelativeLayout parent_layout=new RelativeLayout(this);
        RelativeLayout.LayoutParams rlparent = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);

        parent_layout.setLayoutParams(rlparent);
        parent_layout.setBackgroundColor(Color.WHITE);
        parent_layout.setId(30);

        //creating image
        ImageView iv=new ImageView(this);
        RelativeLayout.LayoutParams rl_iv=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        iv.setId(31);
        iv.setImageResource(R.drawable.androidhappy);
        iv.setLayoutParams(rl_iv);
        parent_layout.addView(iv);

        //creataing text view "title"

         TextView title = new TextView(this);
         RelativeLayout.LayoutParams title_Param = new RelativeLayout.LayoutParams(
                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                      RelativeLayout.LayoutParams.WRAP_CONTENT);
         title_Param.addRule(RelativeLayout.RIGHT_OF,iv.getId());
         title_Param.addRule(RelativeLayout.ALIGN_LEFT);
        // title.setText("Title");
         title.setEllipsize(TextUtils.TruncateAt.END);
         title.setPadding(6, 5, 0, 0);
         title.setSingleLine(true);
         title.setTextColor(Color.BLACK);
         title.setTextSize(12);
        title.setText("Good Morning");
         title.setId(11);
          title.setLayoutParams(title_Param);
         parent_layout.addView(title);


        // Using RemoteViews to bind custom layouts into Notification
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(),parent_layout.getId());
      //    RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.orignal_customnotification);

        // Set Notification Title
        String strtitle = getString(R.string.customnotificationtitle);
        // Set Notification Text
        String strtext = getString(R.string.customnotificationtext);

        // Open NotificationView Class on Notification Click
        Intent intent = new Intent(this, NotificationView.class);
        // Send data to NotificationView Class
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);
        // Open NotificationView.java Activity
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                this)
        // Set Icon
                .setSmallIcon(R.drawable.logosmall)
                // Set Ticker Message
                .setTicker("My Ticker text...")
                // Dismiss Notification
                .setAutoCancel(true)
                // Set PendingIntent into Notification
                .setContentIntent(pIntent)
                // Set RemoteViews into Notification
                .setContent(remoteViews);


        remoteViews.setImageViewResource(iv.getId(),R.drawable.androidhappy);
        remoteViews.setTextViewText(title.getId(), "Good Morning..");


// Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Build Notification with Notification Manager
        notificationmanager.notify(0, builder.build());


    }

Upvotes: 5

Views: 2317

Answers (1)

dsandler
dsandler

Reputation: 2481

What you want to do is not possible; live View objects cannot be used with notifications; RemoteViews is the only type supported for Notification.contentView. If you look at the API docs you can see that the only way to create one is by referencing a layout xml file, although once you have one you can modify it in lots of different ways, including adding other RemoteViews to the layout.

Upvotes: 1

Related Questions