Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change fragment's status bar color?

I have activity where I use five fragments on users touch events. I want to set status bar colours for 3 fragments and no statusbar for other two fragments.

If I use below code in one fragment's onCreateView method it changes activity's status bar and I get white color for all the fragments as I have used getActivity() method. So I am searching for solution for fragments.

getActivity().getWindow.setStatusBatColor(Color.White); 

I also tried setting my theme in style.xml and applying to fragemnts root layout in xml but its not working also.

styles.xml

<style name="StatusBarColor" parent="AppBaseTheme">
    <item name="android:statusBarColor">#F5F5F5
    </item>
</style>

fragment1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/StatusBarColor"
android:background="@color/home_bg_white"
tools:context="mycontext">

thank you in advance for the help.

like image 854
Aalap Patel Avatar asked May 13 '16 20:05

Aalap Patel


People also ask

How can I change the color of my status bar?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar. Step 3: In your MainActivity, add this code in your onCreate method.

How do you change the color of your toolbar on Android?

Just go to res/values/styles.edit the xml file to change the color of action bar.

How do I change the color of my status bar in jetpack compose?

Because it is a root composable in our app, we can use this place to specify the system bar colors. Here is how we can do that: @Composable fun BarColorsTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) { // ... val view = LocalView.

How do I change the color of my status bar to white in flutter?

Step 1: Locate the MaterialApp widget. Step 2: Inside the MaterialApp, add the theme parameter with ThemeData class assigned. Step 3: Inside the ThemeData add the appBarTheme parameter and then assign the AppBarTheme class. Step 4: Inside the AppBarTheme , specify the systemOverlayStyle parameter and set the color.


2 Answers

If you want to change the status bar color programmatically (and provided the device has Android 5.0) then you can use Window.setStatusBarColor(). It shouldn't make a difference whether the activity is derived from Activity or ActionBarActivity.

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>

Edit 2:-

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")
like image 191
Himanshu Shekher Jha Avatar answered Oct 24 '22 08:10

Himanshu Shekher Jha


This is the simplest thing you can do :

getActivity().getWindow().setStatusBarColor(getActivity().getColor(R.color.white));

like image 26
yash songara Avatar answered Oct 24 '22 06:10

yash songara