How to create notification with custom layout?

Asked

Viewed 330 times

1

I wonder if it is possible to create a notification (Ongoing Notification) using an xml? if yes, could pass me a basic with buttons and picture?

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>
  • edited and uploaded xml

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;

0

Create notification in the usual way:

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

Create a Remoteviews with its layout:

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

Attribute to Remoteviews to the notification:

builder.setContent(remoteViews);  

Attribute actions to each of the Imagebutton

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

Then all that remains is to launch the notification:

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

Note: You’ll have to create the Pendingintent pIntent, nextIntent and previousIntent

Browser other questions tagged

You are not signed in. Login or sign up in order to post.