Reputation: 1527
I have an Activity and a Fragment in my project.In the Activity I set the layout take the entire screen and in fragment I try to change the status bar color programmatically. The status bar is shown over the layout but the color is not changed.
Activity:
if (Build.VERSION.SDK_INT > 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Fragment:
if (Build.VERSION.SDK_INT > 16) {
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
if (Build.VERSION.SDK_INT >= 21) {
Log.e("statusbarcolor","changing..");
getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(getContext(),R.color.statusBarColor));
}
}
Upvotes: 0
Views: 2117
Reputation: 75798
I guess , Problem is your Build.VERSION.SDK_INT section .
In my case (Activity)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor("#54d66a");
}
Upvotes: 4
Reputation: 921
Just try doing:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.BLUE);
}
Just tested this with ActionBarActivity and it works alright.
Note: Setting the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag programmatically is not necessary if your values-v21 styles file has it set already, via:
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
Create a method in your activity
public void updateStatusBarColor(String color){// Color must be in hexadecimal fromat
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor(color));
}
}
Call this method from your fragment
((ActivityName)getActivity()).updateStatusBarColor("#000000")
In given below using my application :
..................
switch (position) {
case 0 :
fragment = new SampleOneFragment();
color = R.color.ColorPrimaryDark;
break;
case 1:
fragment = new SampleOneFragment();
color = R.color.AlertRed_dark;
break;
case 2:
fragment = new SampleOneFragment();
color = R.color.ProgressBarPurple_dark;
break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(this, color));
}
replaceFragment(fragment);
}
Upvotes: 0
Reputation: 1030
User this code
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.my_statusbar_color));
}
Upvotes: 0
Reputation: 1527
I did it finally.I simply used the below code in my fragment:
getActivity().getWindow().clearFlags(WindowManager.FLAG_FULL_SCREEN);
Upvotes: 0