Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android remoteviews in notification

I am developing a music player application. The player is in a service. When a song starts to play i want to show a custom notification with remoteViews.

What i designed for the layout is:

android studio screenshot

but unfortunately the textviews are gone when the notification is built:

device screenshot

the code for the layout is as follows:

notification_player.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:id="@+id/album_art" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/wrapper"
        android:layout_toRightOf="@+id/album_art"
        android:layout_alignBottom="@+id/album_art">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/player"
            android:layout_marginBottom="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/prev"
                android:layout_marginRight="5dp"
                android:src="@drawable/ic_fast_rewind"
                android:layout_gravity="center_vertical" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:background="#82c7c7c7"></LinearLayout>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/play"
                android:layout_marginRight="5dp"
                android:layout_marginLeft="5dp"
                android:layout_gravity="center_vertical"
                android:background="@drawable/ic_play_arrow" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:background="#82c7c7c7" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/next"
                android:layout_marginLeft="5dp"
                android:layout_gravity="center_vertical"
                android:background="@drawable/ic_fast_forward" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:id="@+id/divider_horizontal"
            android:layout_above="@+id/player"
            android:background="#82c7c7c7"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"></LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_above="@+id/divider_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/title"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="25dp"
                android:text="Title"
                style="@style/NotificationTitle"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="singer"
                android:id="@+id/singer"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                style="@style/NotificationText"/>
        </LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/close"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/ic_close" />

    </RelativeLayout>
    </RelativeLayout>

Code in service that is showing the notification:

NotificationCompat.Builder builder;
RemoteViews rv = new RemoteViews(getPackageName(),R.layout.notification_player);
        rv.setImageViewBitmap(R.id.album_art,aq.getCachedImage(item.getImage()));
rv.setTextViewText(R.id.title, item.getTitle());
rv.setTextViewText(R.id.singer, item.getSinger());
rv.setOnClickPendingIntent(R.id.close, delete);


builder = new NotificationCompat.Builder(getApplicationContext())
.setContent(rv)
.setSmallIcon(ir.farzamhabibi.moozik.R.drawable.ic_launcher);

startForeground(10, builder.build());

Any idea what the problem is?

EDIT:

I tried setting the LinearLayout height to wrap content and removing the margins

I even tried adding a textView in all the layouts even in the parent of all but it seems the notification is removing all the textviews.

like image 457
Saeed Entezari Avatar asked Oct 31 '22 09:10

Saeed Entezari


1 Answers

I found out what was wrong. In my case i was trying to show a layout which was greater than the default notification layout and as the result it was compressing my layout to fit the size of default notification.

I changed the views like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/album_art" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/wrapper"
        android:descendantFocusability="blocksDescendants"
        android:layout_toRightOf="@+id/album_art">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="false"
            android:gravity="center_vertical"
            android:orientation="vertical"
            android:id="@+id/info"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="title"
                android:textStyle="bold"
                />

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="singer"
                />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:descendantFocusability="blocksDescendants"
            android:layout_alignParentBottom="false"
            android:layout_centerHorizontal="true"
            android:id="@+id/player"
            android:layout_below="@+id/divider_horizontal"
            android:layout_marginTop="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/prev"
                android:layout_marginRight="20dp"
                android:src="@drawable/ic_fast_rewind"
                android:layout_gravity="center_vertical" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:background="#82c7c7c7"></LinearLayout>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/play"
                android:layout_marginRight="20dp"
                android:layout_marginLeft="20dp"
                android:layout_gravity="center_vertical"
                android:src="@drawable/ic_pause" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:background="#82c7c7c7" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/next"
                android:layout_marginLeft="20dp"
                android:layout_gravity="center_vertical"
                android:src="@drawable/ic_fast_forward" />

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:id="@+id/divider_horizontal"
            android:background="#82c7c7c7"
            android:layout_below="@+id/info"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"></LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/close"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/ic_close" />

    </RelativeLayout>

</RelativeLayout>

And in the notification method i added the following lines which solved the problem:

RemoteViews rv = new RemoteViews(getPackageName(), R.layout.notification_player);


        builder = new NotificationCompat.Builder(getApplicationContext())
                .setContent(rv)
                .setTicker("IranGrammy playing")
                .setAutoCancel(true)
                .setSmallIcon(ir.farzamhabibi.moozik.R.drawable.ic_launcher);

        rv.setImageViewBitmap(R.id.album_art, aq.getCachedImage(item.getImage()));
        rv.setTextViewText(R.id.title, item.getTitle());
        rv.setTextViewText(R.id.text, item.getSinger());
        rv.setOnClickPendingIntent(R.id.close, delete);
        rv.setOnClickPendingIntent(R.id.play, playPI);
        rv.setOnClickPendingIntent(R.id.next, nextPI);
        rv.setOnClickPendingIntent(R.id.prev, prevPI);
        Notification notif = builder.build();
        notif.bigContentView = rv;
        startForeground(10, notif);
like image 127
Saeed Entezari Avatar answered Nov 11 '22 11:11

Saeed Entezari