简体   繁体   中英

change background color of image button in custom notification when clicked

I have a custom notification layout that contains four image buttons.

I need to change the background color of the image buttons when user click on them.

I need to use

ImageButton imgButton = (ImageButton) findViewById(R.id.recentAppButt);
imgButton.setBackgroundColor(Color.BLUE);

but this cause a nullpointerexception error because compiler can't find the recentAppButt image button because these image buttons are in the custom notification layout.

this is some code from the mainActivity.class in which i create or remove the notification

private void createNotificationIcons() {
    String ns = Context.NOTIFICATION_SERVICE;
    notificationManager = (NotificationManager) getSystemService(ns);

    notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.home)
            .setContentTitle("My notification")
            .setContentText("All Buttons!")
            .setOngoing(true) /** notification will appear as ongoing notification*/
            .build();                      
    /** set a custom layout to the notification in notification drawer  */
    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification);
    notification.contentView = notificationView;

    Intent recentAppIntent = new Intent(Context.NOTIFICATION_SERVICE);
    recentAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Bundle recentAppBundle = new Bundle();   
    recentAppBundle.putInt("userAnswer", 1);
    recentAppIntent.putExtras(recentAppBundle);
    PendingIntent pendingrecentAppIntent = PendingIntent.getBroadcast(getBaseContext(), 1, recentAppIntent, 0);
    notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);

    notificationManager.notify(1, notification);
}


public class SwitchButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle answerBundle = intent.getExtras();
        int userAnswer = answerBundle.getInt("userAnswer");
        if(userAnswer == 1) {
            ImageButton imgButton = (ImageButton) findViewById(R.id.recentAppButt);
            imgButton.setBackgroundColor(Color.BLUE);
            Toast.makeText(context, "Recent Apps", Toast.LENGTH_SHORT).show();
            Log.d("Recent", "recent app butt clicked");
            openRecentApps();
        }
    }
}

public class BackgroundService extends Service {

    MainActivity mainActivity = new MainActivity();
    MainActivity.SwitchButtonListener switchButtonListener = mainActivity.new   SwitchButtonListener();

    /** Called when the service is being created. */
    @Override
    public void onCreate() {
        super.onCreate();
    }

    /** The service is starting, due to a call to startService() */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerReceiver(switchButtonListener, new     IntentFilter(Context.NOTIFICATION_SERVICE));
    return START_STICKY;
}

    /** Called when The service is no longer used and is being destroyed */
    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(switchButtonListener);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

and this the notification.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@android:style/TextAppearance.StatusBar.EventContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >

<!-- create home button in notification drawer -->

<ImageButton
    android:id="@+id/homeButt"
    android:layout_width="79dp"
    android:layout_height="79dp"
    android:layout_marginLeft="1dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/discription"
    android:src="@drawable/home" />

<!-- create recent apps button in notification drawer -->

<ImageButton
    android:id="@+id/recentAppButt"
    android:layout_width="79dp"
    android:layout_height="79dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/discription"
    android:src="@drawable/recent" />

<!-- create lock screen button in notification drawer -->

<ImageButton
    android:id="@+id/lockScreenButt"
    android:layout_width="79dp"
    android:layout_height="79dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/discription"
    android:src="@drawable/lock" />

<!-- create allButtons button in notification drawer -->

<ImageButton
    android:id="@+id/allButtonsButt"
    android:layout_width="79dp"
    android:layout_height="79dp"
    android:background="#FF990000"
    android:contentDescription="@string/discription"
    android:src="@drawable/all_buttons" />

Thanks in advance.

Check if in your layout file, your ImageView has parameters for height and width which are set to wrap_parent .

if any of those (layout_height or layout_width) are set to wrap_content , and you have don't have an actual image set on the ImageView, it's height is set to 0. In other words, you should change the wrap_content to another value such as 100dp for example and see if the color changes.

findViewById is a method of the Activity class and will not work on RemoteViews and notifications. You are getting a null point exception because R.id.recentAppButt doesn't exist in your activity view.

You can achieve what you want by creating two icons with different background colours in your drawables folder. You can then switch between the two icons with the setImageViewResource method of the RemoteViews class as follows

notificationView.setImageViewResource(R.id.recentAppButt, R.drawable.icon_with_blue_background)

You then need to rebuild your notification and re- notify it through the NotificationManager so that the background colour is updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM