Search code examples
androidandroid-notificationsandroid-notification-bar

Create own NotificationStyle


In my Android application I want to create a expendable notification. For this I have to set a Style for the notification like this:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setSmallIcon(android.R.drawable.ic_menu_info_details);
builder.setContentTitle("My notification");
builder.setContentText("Hello World!");

builder.setStyle(...);

Android gives me some styles I can use with this method but I like to create my own, so I can load a layout in the notification. So how can I do this? If I create a subclass of Style there are only these two methods:

public Notification build();
public void setBuilder(Builder builder);

So how can I load my own layout in the notification? builder.setContent is insufficient for me because then I only have 64dp for the layout.


Solution

  • Found a solution for this problem. It's really easy. Just use this code here:

    String packageName = this.getPackageName();
    RemoteViews bigView = new RemoteViews(packageName, R.layout.test);
    
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(android.R.drawable.ic_menu_info_details);
    builder.setContentTitle("My notification");
    builder.setContentText("Hello World!");
    
    Notification noti = builder.build();
    noti.bigContentView = bigView;
    
    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mgr.notify(1234, noti);