1

So i have:

AudioPlayerBroadcastReceiver Receiver = new AudioPlayerBroadcastReceiver();
         IntentFilter filter = new IntentFilter();
         filter.addCategory(Intent.CATEGORY_DEFAULT);              
         filter.addAction("aClass.ACTION_PLAY");
         filter.addAction("aClass.ACTION_PAUSE");
        registerReceiver(Receiver,filter);

Intent PlayIntent = new Intent("aClass.ACTION_PLAY");
        PendingIntent PlaypendingIntent = PendingIntent.getBroadcast(this, 100, PlayIntent, 0);

ImageView playb = (ImageView)this.findViewById(R.id.play_button);    
        notificationView.setOnClickPendingIntent(R.id.play_button, PlaypendingIntent);

the same for stop;

  RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.media_player_in_notificationBar);

how can i add buttons for play and stop(ImageViews), or how can i use the onClick event ? so if i click play(white button)- the stop goes grey and remain this color until it's pressed so the play goes grey.

Vlad.mir
  • 685
  • 1
  • 10
  • 28
  • Do you mean a Notification as given [here](http://developer.android.com/guide/topics/ui/notifiers/notifications.html)? If so, you may follow that guide or (if you want custom Notification) follow see my question [here](http://stackoverflow.com/questions/21237495/create-custom-big-notifications/21283668) and you may handle clicks as given in first link. – MalaKa Jun 20 '14 at 10:35
  • Hei, yes, a Notification as in the link you gave me, but i've just done the notification(when the app goes in background) with normal buttons(play and stop) that work, now i just need to know how to work with image button to add effects to them when pressed, as i described it. (the notification bar is a relative layout) – Vlad.mir Jun 20 '14 at 11:11

1 Answers1

0

In my project I do it like this:

  • I define a manual layout for the notification
  • In the layout, I add ImageButtons
  • I define a selector for background of the ImageButtons (would be possible for the source, too)
  • Finally, I manually set the layout of the Notification as given here
  • Of course, I also add a PendingIntent but as you already do that, I don't detail it here

The xml-layout-file contains ImageButtons:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp" <!-- This is where I manually define the height -->
    android:orientation="horizontal" >

        <ImageButton 
            android:id="@+id/big_notification_cancel_button"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:background="@drawable/notification_button_background_selector"
            android:src="@drawable/some_image_with_transparent_background"
            />
    <!-- some more elements.. --> 
</LinearLayout>

The notification_button_background_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:state_pressed="true"
    android:drawable="@drawable/white_semi_opaque_background"/> 

<item
    android:drawable="@drawable/transparent_background"/>

</selector>

I'm not sure if this is really the answer you're looking for. Hope it helps, though.

Edit: I have worked with the change of Image for ImageView, too. You have to update the View after you receive a click. In the class where I update the notification, I do this:

RemoteViews smallViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout_big);

if(playing){
    smallViews.setImageViewResource(R.id.big_notification_button_play_pause, R.drawable.notification_small_pause_button_selector);
}else{
    smallViews.setImageViewResource(R.id.big_notification_button_play_pause, R.drawable.notification_small_play_button_selector);
}
// and some more stuff
Community
  • 1
  • 1
MalaKa
  • 3,734
  • 2
  • 18
  • 31
  • Thanks, i've tried this too, but it doesn't really get my needs, i need to find a solution that if i press the 'play, the 'stop button turns grey, and if i press 'stop, the 'play button turns grey. Or, if i press the 'play button to turn into 'stop and if i tap 'stop again it turns 'play. you feel me ? – Vlad.mir Jun 20 '14 at 12:28
  • I want to have more control over the ImageView's buttons, but i cant call the onClick method because isn't working in the onPause() class. (where i have my notification builder) – Vlad.mir Jun 20 '14 at 12:32
  • @Vladimir Well you have to somehow change the image-source of the `ImageButton` when the button was clicked. I do this as given in my edit. I always update the Notification when I receive a click and recheck the state. – MalaKa Jun 20 '14 at 12:38
  • so i used you're ideea: if(BackgroundSound.player != null) { notificationView.setImageViewResource(R.id.stop_button, R.drawable.ic_stop_notification_pressed); notificationView.setImageViewResource(R.id.play_button, R.drawable.ic_play_notification); } else { notificationView.setImageViewResource(R.id.play_button, R.drawable.ic_play_notification_pressed); notificationView.setImageViewResource(R.id.stop_button, R.drawable.ic_stop_notification); } i need somehow to update from the class in background for colors to change. (they only change when i play/stop from cla – Vlad.mir Jun 23 '14 at 11:15
  • It's partialy a good answer, but it's complicated, i have to refresh the pendingIntent, the best way after all is to use the Builder and the buttons from addAction(). – Vlad.mir Jun 23 '14 at 12:45
  • Yes, the Builder might be a cleaner solution, but then you can only use the buttons with the text. – MalaKa Jun 23 '14 at 13:19
  • and i use notification.priority = Notification.PRIORITY_MAX; so that the notification bar is expanded by default – Vlad.mir Jul 03 '14 at 13:23