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.
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.
Just go to res/values/styles.edit the xml file to change the color of action bar.
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.
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.
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")
This is the simplest thing you can do :
getActivity().getWindow().setStatusBarColor(getActivity().getColor(R.color.white));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With