Light/dark Status Bar based on image behind

Viewed 75

I have a screen in my app that loads an image behind a transparent status bar. Sometimes these images are light enough for the status bar icons and text to get lost in the image.

How do you set the status bar to light/dark based on the colors of the image underneath it? I'm trying to keep the status bar completely transparent and not add a background.

1 Answers

You can use this solution to change the statusbar color based on your background :

// Set the background and text colors of a toolbar given a
// bitmap image to match
public void setToolbarColor(Bitmap bitmap) {
    // Generate the palette and get the vibrant swatch
    // See the createPaletteSync() method
    // from the code snippet above
    Palette p = createPaletteSync(bitmap);
    Palette.Swatch vibrantSwatch = p.getVibrantSwatch();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    // Load default colors
    int backgroundColor = ContextCompat.getColor(getContext(),
        R.color.default_title_background);
    int textColor = ContextCompat.getColor(getContext(),
        R.color.default_title_color);

    // Check that the Vibrant swatch is available
    if(vibrantSwatch != null){
        backgroundColor = vibrantSwatch.getRgb();
        textColor = vibrantSwatch.getTitleTextColor();
    }

    // Set the toolbar background and text colors
    toolbar.setBackgroundColor(backgroundColor);
        toolbar.setTitleTextColor(textColor);
}

Now, simply generate bitmap from your background and then use it with this method.

For more info, visit the official developers site - Color palette API

Related