Change the color of the status bar of android

1

I want to change the color of the status bar of my app on android. I have this code but it does not work:

Log.e("Resultado", Build.VERSION.SDK_INT +">="+ Build.VERSION_CODES.LOLLIPOP);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window statusBar = getWindow();
    statusBar.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        statusBar.setStatusBarColor(getResources().getColor(colorElegido, getTheme()));

    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        statusBar.setStatusBarColor(getResources().getColor(colorElegido));
    }
}

The device on which I am testing it has android 5 .

The app closes me and does not show me what the error is.

The color id is: -15392107

I use this code to get the color id:

private Integer getParseColor(String color) {
    if (color != null && !color.equals("")  && !color.equals("null")) {
        return Color.parseColor(color);
    }
    return 0;
}

If I change the variable colorElegido for this, for example: R.color.white if it works.

The only exception I find is this:

06-22 13:09:27.639 3162-3177/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.huawei.lcagent.client.LogCollectManager.getUserType()' on a null object reference
06-22 13:09:27.639 3162-3177/? W/System.err:     at com.android.server.util.ReportTools.getUserType(ReportTools.java:86)
06-22 13:09:27.639 3162-3177/? W/System.err:     at com.android.server.util.ReportTools.isBetaUser(ReportTools.java:73)
06-22 13:09:27.639 3162-3177/? W/System.err:     at com.android.server.util.ReportTools.report(ReportTools.java:58)
06-22 13:09:27.639 3162-3177/? W/System.err:     at com.android.server.util.HwUserBehaviourRecord.appExitRecord(HwUserBehaviourRecord.java:65)
06-22 13:09:27.640 3162-3177/? W/System.err:     at com.android.server.am.ActivityManagerService$UiHandler.handleMessage(ActivityManagerService.java:1572)
06-22 13:09:27.640 3162-3177/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
06-22 13:09:27.640 3162-3177/? W/System.err:     at android.os.Looper.loop(Looper.java:150)
06-22 13:09:27.640 3162-3177/? W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:61)
06-22 13:09:27.640 3162-3177/? W/System.err:     at com.android.server.ServiceThread.run(ServiceThread.java:46)

Could it be the error because the variable colorElegido is negative?

After this line I have two logs but they do not show, so I guess it will be there:

statusBar.setStatusBarColor(getResources().getColor(colorElegido, getTheme()));
    
asked by 22.06.2017 в 12:48
source

1 answer

1

It is only possible to change the color of the status-bar from the Android L version to the front.

I use the following:

int myColor = Color.parseColor("#3F51B5");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    getWindow().setStatusBarColor(myColor);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().setStatusBarColor(myColor);
}
    
answered by 22.06.2017 / 13:06
source