0

here is the code I use to set a notification for any event. But my requirement is, Instead of just only text I also want to display a picture in the notification panel, which is not a logo, a complete picture (or may be cropped). How to modify this code to get this work done.

Secondly, my notification does it job properly,but when there are too many text to display, it does not expands. Is there any way to make a notification expandable.

Thirdly, some portion of my code, you will see that, those commands are deprecated( when I am initiating the notification and also at notification.setLatestInfo()). So, what is the right way to write it, and if I write it would it work on lower version as well? thanks in advance.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ok = (Button) findViewById(R.id.dones);
    et = (EditText) findViewById(R.id.ets);


    ok.setOnClickListener(MainActivity.this);

    counter = getSharedPreferences("count", 0);

    notificationId = counter.getInt("val", 0);
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
    String Text = et.getText().toString();

    if(Text.equals("")){
        Toast.makeText(getApplicationContext(), "You Don't have anything to clip", Toast.LENGTH_SHORT).show();
    }
    else{
        notification = new Notification(R.drawable.ic_launcher, "You Got Things ToDo!", System.currentTimeMillis());
        notificationmanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);
        notification.setLatestEventInfo(getApplicationContext(), "I want to ..", Text, pending);

        notificationmanager.notify(notificationId, notification);
        notificationId++;

        notification.flags |= Notification.FLAG_NO_CLEAR;

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
androCoder-BD
  • 498
  • 7
  • 13

2 Answers2

2

You should check out Notification.Builder, particularly setStyle():

http://developer.android.com/reference/android/app/Notification.Builder.html

and then Notification.BigPictureStyle should allow you to show an image like you desire:

http://developer.android.com/reference/android/app/Notification.BigPictureStyle.html

Edited with code sample, since you're processing the Bitmap as well, I've added an AsyncTask so that the processing doesn't occur on the UI thread:

else{
    new AsyncTask<Void, Void, Void>(){

        public Void doInBackround(Void... args){
            PendingIntent pending = PendingIntent.getActivity(arg0.getContext(), 0, new Intent(arg0.getContext(),MainActivity.class), 0);

            Bitmap bmp = BimapFactory.decodeResource(arg0.getContext().getResources(), R.id.my_img);

            notification = new NotificationCompat.Builder(arg0.getContext())
             .setContentTitle("I want to...")
             .setContentText(Text)
             .setContentIntent(pending)
             .setSmallIcon(R.drawable.ic_launcher)
             .setStyle(new NotificationCompat.BigPictureStyle()
                 .bigPicture(bmp))
             .build();
            notificationmanager = (NotificationManager)arg0.getContext().getSystemService(Context.NOTIFICATION_SERVICE);

            notificationmanager.notify(notificationId, notification);
            notificationId++;

        }
    }.execute();
}
sddamico
  • 2,130
  • 16
  • 13
  • This is available in a compatible way for pre-JellyBean using the `NotificationCompat.Builder` – sddamico Jan 04 '14 at 16:37
  • thank you guys for your quick responses. I think these are the resources I wanted. Gonna give it those a try... – androCoder-BD Jan 04 '14 at 16:43
  • Sorry to bug you but after reading it all seems not helped me much. Firstly, I am very new to android programming. However, on those guide, within the parameter they used a **bitmap type** but I really could not understand how to cope with that.l – androCoder-BD Jan 04 '14 at 19:37
  • To get a `Bitmap` from a resource, use the `BitmapFactory` http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeResource(android.content.res.Resources, int, android.graphics.BitmapFactory.Options) – sddamico Jan 04 '14 at 19:41
  • Edited my response with a code example of using the builder and decoding the Bitmap. – sddamico Jan 04 '14 at 19:58
  • I really don't know how to thank you for your effort. But till now getting an error at **setStyle**. :( – androCoder-BD Jan 04 '14 at 20:22
  • Woops, sorry. That should have been `NotificationCompat` not `Notification`. Answer edited. – sddamico Jan 04 '14 at 20:40
  • Though the rules says I can't thank you within this discussion but.... A BIG THAAAANKS TO YOU. Wish I could offer you a cup of tea :( THANKS AGAIN... – androCoder-BD Jan 04 '14 at 21:06
  • What should be the specifications of the bitmap (resolution and/or aspect ratio) ? – android developer Nov 03 '14 at 08:08
1

You can do this with RemoteViews in a Custom Notification Layout. If you have a specific question feel free to post it.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • really thanks for your feedback. Helped me finding a portion of my question. – androCoder-BD Jan 04 '14 at 16:44
  • well, my specific requirement is, I want to select some picture and show them on notification bar. (which changes time to time). However, my code can get all the source of selected picture(path, I mean) and I also can show simple notification which consists text. But now I want to display all those pictures I selected before in the notification bar. – androCoder-BD Jan 04 '14 at 19:38
  • and also, my pictures need not be displayed in the actual size, a cropped size or specific size should be shown.I know this is possible, but could not figure out how. Can you please help me out?? – androCoder-BD Jan 04 '14 at 19:41