Search code examples
androidandroid-notificationsandroid-broadcastreceiverandroid-remoteview

Update the text of a button contained in a custom notification when clicked in Android


UPDATE

I modified my code by updating the notification as advised by Pawel and Amit K. which I thank both. In the 'alarmNotification' method I create the notification with id1 and, when the button clicks, I pass the variable (string) "on" to the 'onReceive' of the 'Buttonlistener' class where I manage the new notification. Now the problem is that the string variable "alarm_state" passed to the onReceive does not correctly compare to the If construct. Can you tell me why?

public void alarmNotification(String title, String message)
{
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
    notify.contentView=contentView;

    Intent button_intent=new Intent(context,ButtonListener.class);
    button_intent.putExtra("alarm_state","on");
    PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
    contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);

    nm.notify(1,notify);
}

public static class ButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {

        NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);

        String condition = intent.getStringExtra("alarm_state");

        if (condition=="on")  //here it's always false!!! Why??
        {                
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_on);
            contentView.setTextViewText(R.id.subtitle,"Cam enabled");
        }
        if (condition=="off")
        {
            contentView.setImageViewResource(R.id.switch1,R.drawable.button_off);
            contentView.setTextViewText(R.id.subtitle,"Cam disabled");
        }

        notify.contentView=contentView;
        nm.notify(1,notify);
    }
}

OLD

In my app i have a personalized notification with a button that when pressed must change its text from "Active" to "Disabled". The notification is launched by a service and the click event triggers a class "ButtonListener" contained in the service that should change the text of the button. The problem is that the text of the button is not changed!!!

public static class ButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        Toast.makeText(context, "Button clicked", Toast.LENGTH_SHORT).show();  // this ok
        try {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.custom_notification, null);                
            Button button = (Button) layout.findViewById(R.id.switch1);
            button.setText("Disabled");   // this not update!!!
        }
        catch (Exception ec)
        {

        }

    }
}

Solution

  • Update

     public void onReceive(final Context context, Intent intent) {
    
            NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
            RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
    
            String condition = intent.getStringExtra("alarm_state");
    
            if (condition.compareToIgnoreCase("on")==0)  //here it's always false!!! Why??
            {                
                 contentView.setImageViewResource(R.id.switch1,R.drawable.button_on);
                 contentView.setTextViewText(R.id.subtitle,"Cam enabled");
                 Intent button_intent=new Intent(context,ButtonListener.class);
                 button_intent.putExtra("alarm_state","off");
                 PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,FLAG_UPDATE_CURRENT);
                 contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);
            }
            if (condition.compareToIgnoreCase("off")==0)
            {
                contentView.setImageViewResource(R.id.switch1,R.drawable.button_off);
                contentView.setTextViewText(R.id.subtitle,"Cam disabled");
                Intent button_intent=new Intent(context,ButtonListener.class);
                button_intent.putExtra("alarm_state","on");
                PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
                contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);
            }
    
            notify.contentView=contentView;
            nm.notify(1,notify);
        }
    

    old

    You can not change the title of your action button after creating the notification already. But what you can do, you can set a background drawable which will be different based on state. something like this -

    Lets say you have a my_button.xml

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

    then -

    addAction(R.drawable.my_button, <title>, <intent>);
    

    Or you can do what @Pawel said, but thats creating another new notification replacing the old one.