How to create custom layout notification?

1

I wonder if it's possible to create an OnGoing Notification using an xml? if yes, could you give me a basics with buttons and image?

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:layout_gravity="center_horizontal"
    android:background="@color/white"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="256dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="left|center_vertical"
        android:src="@mipmap/without_photo" />

    <TextView
        android:id="@+id/sub_title"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/next"
        android:layout_alignLeft="@+id/previous"
        android:layout_alignRight="@+id/next"
        android:layout_alignStart="@+id/previous"
        android:layout_centerVertical="true"
        android:text="sub text"
        android:textColor="@color/black_transparent" />

    <ImageButton
        android:id="@+id/next"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/Play_pause"
        android:layout_marginRight="16dp"
        android:background="@color/transparent"
        android:baselineAlignBottom="false"
        android:src="@mipmap/ic_next_black" />

    <ImageView
        android:id="@+id/Play_pause"
        android:layout_width="27dp"
        android:layout_height="32dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_marginRight="24dp"
        android:layout_toLeftOf="@+id/next"
        android:background="@color/transparent"
        android:longClickable="true"
        android:src="@mipmap/play" />

    <ImageButton
        android:id="@+id/previous"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignTop="@+id/Play_pause"
        android:layout_marginRight="24dp"
        android:layout_toLeftOf="@+id/Play_pause"
        android:background="@color/transparent"
        android:src="@mipmap/ic_prev_black" />

    <TextView
        android:id="@+id/title_notification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/sub_title"
        android:layout_alignEnd="@+id/sub_title"
        android:layout_alignLeft="@+id/sub_title"
        android:layout_alignRight="@+id/sub_title"
        android:layout_alignStart="@+id/sub_title"
        android:layout_marginBottom="16dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black" />
</RelativeLayout>
    
asked by anonymous 08.06.2015 / 21:01

2 answers

0
        int NOTIFICATION_ID = 1;
        int icon = R.drawable.icon;
        long when = System.currentTimeMillis();

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
        //cria a sua notification
        Notification notification = new Notification(icon, getString(R.string.text), when);

        //infla o seu custom layout
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);

        contentView.setImageViewResource(R.id.notification_image, R.drawable.notification_image);
        contentView.setTextViewText(R.id.notification_title, "Meu texto");
        contentView.setTextViewText(R.id.notification_text, "Meu subtitulo");

        //set a custom view na sua notificacao
        notification.contentView = contentView;
    
09.06.2015 / 00:27
0

Create the notification as usual:

Notification.Builder builder = new Notification.Builder(this) 
        .....
        ....  
        .setAutoCancel(true) 
        .setContentIntent(pIntent);

Create a RemoteViews with your layout:

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.o_seu_layout);  

Assign the RemoteViews to the notification:

builder.setContent(remoteViews);  

Assign actions to each of the ImageButton

remoteViews.setOnClickPendingIntent(R.id.next, nextIntent);
remoteViews.setOnClickPendingIntent(R.id.previous, previousIntent);  

Then you just need to post the notification:

mNotificationManager.notify(0, builder.build());

Note:

You will need to create the PendingIntent pIntent , nextIntent and previousIntent

    
09.06.2015 / 16:29