1

Gostaria de saber se é possivel criar uma notificação(OnGoing Notification) usando uma xml? se sim, poderiam me passar um basico com botoes e imagem?

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>
1
  • editei e coloquei o xml Commented 9/06/2015 às 12:42

2 Respostas 2

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

Crie a notificação da forma habitual:

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

Crie uma RemoteViews com o seu layout:

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

Atribua a RemoteViews à notificação:

builder.setContent(remoteViews);  

Atribua actions a cada uma das ImageButton

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

Depois só falta lançar a notificação:

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

Nota: Terá de criar os PendingIntent pIntent, nextIntent e previousIntent

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .