Change Status Bar to transparent in a Fragment within an Activity

Asked

Viewed 355 times

1

I have a Activity which has a theme for its Status Bar, but it has an influence on a Fragment I want another color in the Status Bar.

Is there any way I can change the color of the Status Bar just when that Fragment open, and return to normal when closed?

2 answers

2


You can change through the method setStatusBarColor():

context.getWindow().setStatusBarColor(Color.TRANSPARENT);

Remember that this function is only available from API 21 (Lollipop), then make a check before using it:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    context.getWindow().setStatusBarColor(Color.TRANSPARENT);
}

0

With Status Bar, you refer to Actionbar/Toolbar? If yes, you can change manually (code) with:

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#CCFF0000")));

That way, when Fragment is created you can take Activity, from Activity take Fragment and change the color of Actionbar by code.

EDIT:

I had misinterpreted his question. The answer from sicachester is correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.