Set Gradient Color to Status Bar Fragment - android

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")

Related

Android Window flag FLAG_TRANSLUCENT_STATUS is deprecated now, How to fix this?

I an trying to update status bar dynamically as below but getting warning that flag FLAG_TRANSLUCENT_STATUS is depricated now, how i can fix this?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// clear FLAG_TRANSLUCENT_STATUS flag:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// finally change the color
getWindow().setStatusBarColor(ContextCompat.getColor(this, color));
}
I spent too much hours to get the proper answer, because i have an error with status bar color as well. I will put the answer here if someone has the same problem.
You need to set the status bar color in transparent as you do in the code using the last build version as "if" statement, thats for sure.
But, also, you need to set this layout attribute in the root view as well.
android:fitsSystemWindows="false"
Hope this will help you resolving the status bar error in Android 30.
if (Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= 19) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//Virtual keyboard is also transparent
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}

Android status bar color not changing

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);
}

How to set and reset status bar color to original based on some condition in android

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);
}
}
}

Lollipop make statusbar transparent on content frame of navigation drawer

i am trying to set status bar transparent on my content frame inside navigation drawer. I create the app using the template contain on android studio. I need make status bar transparent when navigation drawer is not open so my toolbar back ground is draw behind it.
This one:
This One What I mean
Not this one, because it already transparent:
Not this one
I actually had an issue related to this due to some custom rendering with parallax effects... Anyway, you can just set the status bar background color in onDrawerClosed(View drawerView) and then set it back to transparent when opening the drawer:
public class WindowHelper {
public static void setColoredStatusBar(Activity activity){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(R.color.secondary_color));
}
}
public static void setTranslucentStatusBar(Activity activity){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
}
So for you it'd something like:
#Override
public void onDrawerClosed(View drawerView) {
WindowHelper.setColoredStatusBar(activity);
}

Set status bar color dynamically in android

How to set status bar color dynamically for an application, am using view pager while swiping (horizontally) status bar color and title bar and button should change the color . as per my code title and button color changing perfectly ,but the issue is status bar color taking next color from array list. how to fix that issue can anyone help me. here is my code
private int[] colors = new int[]{0xffffd200, 0xff37beb7, 0xff00ccff, 0xff8585c1, 0xfff2a03c, 0xff2a80b9, 0xfff15972,
0xffe9776c, 0xff9dcc96,0xff76c069};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = ((Activity) context).getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
int coloring = position % colors.length;
int new_color = colors[coloring];
window.setStatusBarColor(new_color);
title_bar.setBackgroundColor(new_color);
set_share.setBackgroundColor(new_color);
}
else{
int color = position % colors.length;
itemView.setBackgroundColor(colors[color]);
title_bar.setBackgroundColor(colors[color]);
set_share.setBackgroundColor(colors[color]);
}
To change status bar color use setStatusBarColor(int color). According the javadoc, we also need set some flags on the window.
Working snippet of code:
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(activity.getResources().getColor(R.color.example_color));
This is taken from following reference:
How to change status bar color to match app in Lollipop? [Android]
coming to Status bar color, you can only add it to devices with API level more than 21. To those devices which satisfy this condition you can change the StatusBar color dynamically as shown below.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.Statusbar));
}
When I wanted to set the status bar color, I used https://github.com/jgilfelt/SystemBarTint
I used it like that :
public static void colorStatusBar(Window window, Activity activity) {
Log.v(Constants.LOG_TAG, "Start defining color bar status");
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
tintManager.setTintColor(activity.getResources().getColor(R.color.colorPrimaryDark));
}
}
But beware that setting status bar color is only possible if your app runs on a phone with API >= 19

Categories

Resources