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));
}
}
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");
}
I did it finally.I simply used the below code in my fragment:
getActivity().getWindow().clearFlags(WindowManager.FLAG_FULL_SCREEN);
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));
}
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);
}
Related
I tried changing the color of the status bar it work
if (Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(getResources().getColor(R.color.black));
}
but for the nocth diplay it does not work.
Try this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.colorPrimary));
}
Just remove: window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
I searched for a solution to set a drawable for status bar and I found this :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
This code works fine but it makes another problem too!
If user's android device has bottom navigation buttons (software buttons) my layout goes behind it! of course this problem happens for top of my layouts too.
You can see it here
Is there a way to have a customized status bar while there is no problem with other items overlapping my layout?
This code solved my problem!
#Override
protected void onCreate(Bundle savedInstanceState) {
...
//make full transparent statusBar
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
}
if (Build.VERSION.SDK_INT >= 19) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
if (Build.VERSION.SDK_INT >= 21) {
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
}
public static void setWindowFlag(Activity activity, final int bits, boolean on) {
Window win = activity.getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
In onCreate I do this:
this.setTheme(getTheme());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
w.setFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS , WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS );
w.setStatusBarColor(Color.GREEN);
}
What I want to achieve is after applying my usual theme I want to set the Status bar green. But that doesn't happen. The line is executed, but the color doesn't change. Anything I could be doing wrong here?
I think there is something wrong with the flags. I use this code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.primary_dark));
}
I am trying to set Gradient Color to status bar in My android application. For Activity, i placed below code in BaseActivity. It is working fine.
public void setStatusBarColor()
{
try
{
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Drawable background = this.getResources().getDrawable(R.drawable.gradient);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(this.getResources().getColor(R.color.transparent));
window.setNavigationBarColor(this.getResources().getColor(R.color.transparent));
window.setBackgroundDrawable(background);
}
}
catch (Exception ex)
{
GSLogger.e(ex);
}
}
Now, I would like to apply same gradient color to Fragment. I have a navigation drawer where i have nearly 8 fragments. I would like to apply this gradient color to status bar there. The same above code applied there in BaseFragment. It is not affecting any color. But, when i apply single color code, it is working fine. Here is the below code.
public void setStatusBarColor()
{
try
{
Window window = getActivity().getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(getResources().getColor(R.color.black));
}
}
catch (Exception ex)
{
GSLogger.e(ex);
}
}
What could be the Issue here? I am not able to apply gradient color to Fragment. But the same code is working fine for Activity.
Help would be appreciated.
Your fragment is must be the part of the activity. You can write this code in activity and call it in the fragment.
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")
I want to change the color of status bar,and did the same as specified here . I have the following doubts.
1) but i want to reset status bar color to original based on some condition, how will i do that.
I have tried like this.
public class StoreDisableHelper {
public static void changeStatusBarColor(boolean status,Activity activity){
Window window = activity.getWindow();
if(status){
// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// finally change the color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(activity.getResources().getColor(R.color.theme_red));
}
}
else{
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
}
}
when the condition is false, i am resetting
This this:
https://gist.github.com/MrThiago/4897424c3c773aec57081325f273311a
private static int defaultStatusBarColor;
public static void changeStatusBarColor(Activity context, boolean change, int color){
if (Build.VERSION.SDK_INT >= 21)
{
Window window = context.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if(change){
defaultStatusBarColor = window.getStatusBarColor();
window.setStatusBarColor(color);
}
else{
// reset
window.setStatusBarColor(defaultStatusBarColor);
}
}
}