Turn off button illumination

I am developing an Android application that can be used at night. Therefore, I need to turn off the button illumination. How can i do this? On my phone, the backlight turns off after a while, but on the Motorola Droid I don't think this is happening.

I use wakelock to keep the screen on. Should I use a different flag or how to do this?

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, WAKE_LOCK_TAG);
mWakeLock.acquire();

Many thanks!

// Kaloer

+3
source share
3 answers

AFAIK, there is no API for controlling button illumination - sorry!

0
source

There is a hack:

private void setDimButtons(boolean dimButtons) {
    Window window = getWindow();
    LayoutParams layoutParams = window.getAttributes();
    float val = dimButtons ? 0 : -1;
    try {
        Field buttonBrightness = layoutParams.getClass().getField(
                "buttonBrightness");
        buttonBrightness.set(layoutParams, val);
    } catch (Exception e) {
        e.printStackTrace();
    }
    window.setAttributes(layoutParams);
}
+4
source

, , , -, , .

It is built in with API 8. ( doc )

float android.view.WindowManager.LayoutParams.buttonBrightness



This is a slightly modified / simplified version of what I use in one of my applications (excluding irrelevant code). An inner class is needed to prevent a crash on launch on older platforms that do not support it.

private void nightMode() {
    Window win = getWindow();
    LayoutParams lp = win.getAttributes();
    if (prefs.getBoolean("Night", false))
        changeBtnBacklight(lp, LayoutParams.BRIGHTNESS_OVERRIDE_OFF);
    else changeBtnBacklight(lp, LayoutParams.BRIGHTNESS_OVERRIDE_NONE);
    win.setAttributes(lp);
}

private void changeBtnBacklight(LayoutParams lp, float value) {
    if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
        try {
            new BtnBrightness(lp, value);
        } catch (Exception e) {
            Log.w(TAG, "Error changing button brightness");
            e.printStackTrace();
        }
    }
}

private static class BtnBrightness {
    BtnBrightness(LayoutParams lp, float v) {
        lp.buttonBrightness = v;
    }
}
+1
source

Source: https://habr.com/ru/post/1726657/


All Articles