0

I have only been able to find solutions for getting the status bar to change colour while the app is running. And I have successfully done it but I need it to remain the new colour when the app is put into the background (like when the user hits the home button). Similar to how the status bar changes colour when you call somebody and hit the home button.

In my OnCreate method I have:

    Window window = this.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    SystemBarTintManager tintManager = new SystemBarTintManager(this);
    tintManager.setStatusBarTintEnabled(true);
    tintManager.setStatusBarTintColor(Color.GREEN);
    tintManager.setStatusBarTintEnabled(true);

So the solutions I found heavily depend on the app being open. When the user hits the home button the colour for the status bar "shrinks" with the rest of the app.

  • what i understand is you want to change status bar colour and when your app goes to background you should have new changed colour for status bar – Dhiraj May 25 '16 at 16:24
  • correct me if i am wrong – Dhiraj May 25 '16 at 16:24
  • I think you're right. normally the status bar is black or transparent, but I need it set to be red while my app is in the background – BenSouthern May 25 '16 at 16:30
  • you can detect when your app goes to background then where you are facing problem?? – Dhiraj May 26 '16 at 07:15
  • I can easily execute the code I have when the user puts the app in the background using the onPause() method. But all of the ways to change the status bar's color I have found involve changing the color in the window which "shrinks" with the application when it is sent to background thus leaving the regular status bar when the app isn't in foreground. when I use onPause() it just changes the colour for a second, then shrinks the app including my newly coloured bar, leaving the normal status bar – BenSouthern May 26 '16 at 14:36

2 Answers2

0

In your activity add the following method :

@Override
protected void onPause() {
    super.onPause();

    // put your code here
}
BOUTERBIAT Oualid
  • 1,494
  • 13
  • 15
  • thank you for the help, but it didn't work. All of the solutions I found for changing the status bar rely on creating a window in the onCreate method for the activity – BenSouthern May 25 '16 at 16:21
-1

Try with this, work for my!

int statusBarHeight = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) statusBarHeight = getResources().getDimensionPixelSize(resourceId);

final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        statusBarHeight,
        WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,   // Allows the view to be on top of the StatusBar
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,    // Keeps the button presses from going to the background window and Draws over status bar
        PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.TOP | Gravity.CENTER;

LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.TRANSPARENT);
LinearLayout.LayoutParams layoutParameteres = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(layoutParameteres);

TextView tv = new TextView(this);
ViewGroup.LayoutParams tvParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
tv.setLayoutParams(tvParameters);
tv.setTextColor(Color.WHITE);
tv.setGravity(Gravity.CENTER);
tv.setText("123");
ll.addView(tv);

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(ll, parameters);

source : https://stackoverflow.com/a/45392031/13621167