Search code examples
androidviewgpsvisible

Android View.setKeepScreenOn() - does it apply when the view is GONE?


I have a View with some GPS info. It is made VISIBLE/GONE with a GPS on/off switch inside the app. I want to call setKeepScreenOn(true) on this view so that it keeps screen on but ONLY when it is VISIBLE and NOT when it is GONE.


Solution

  • I would say it also applies when the view is gone, but I don't know. It shouldn't be hard to test though.

    But you can just use View.setKeepScreenOn(false) whenever you're setting its visibility to GONE and View.setKeepScreenOn(true) whenever you're setting its visibility to VISIBLE.

    View view = ...; // I guess something with findViewById() happens here
    view.setVisibility(View.GONE);
    view.setKeepScreenOn(false);
    ...
    view.setVisibility(View.VISIBLE);
    view.setKeepScreenOn(true);
    

    Or that you don't have to keep that in mind, you could define a method for that:

    private void mySetVisibility(View v, int visibility) {
        v.setVisibility(visibility);
        if (visibility == View.GONE) {
            v.setKeepScreenOn(false);
        } else {
            v.setKeepScreenOn(true);
        }
    }
    

    and then just use mySetVisibility(view, View.GONE) and mySetVisibility(view, View.VISIBLE).

    Pretty straightforward.

    Hope it helps.