Coloured status bar android

How to Change the Color of Status Bar in an Android App?

A Status Bar in Android is an eye-catching part of the screen, all of the notification indication, battery life, time, connection strength, and plenty of things shows here. An Android user may look at a status bar multiple times while using an Android application. It is a very essential part of the design that the color of the status bar should follow the color combination of the layout. You can look out to many android apps on your phone and can see how they changed it according to its primary colors. There can be multiple ways for changing the status bar color but we are going to tell you about the best hand-picked two methods which you can use either in Java or Kotlin.

Method 1: Creating a New Theme

You can follow this method in apps that are built with Kotlin or Java. It will work in both.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

Читайте также:  Андроид телевизор с камерой

Step 1: Open Android Studio and start a new project by selecting an empty activity. Give it a name of your choice, then select your language and API level. At last click on finish.

Step 2: Find an XML file called styles.xml by navigating res/values/styles.xml.

Step 3: Find another XML file by navigating res/values/colors.xml, and also add that color here which you want to change for the status bar.

Step 4: Now in the style.xml file, add the below code just before the tag and change the colors of it as your choice. ColorPrimaryDark is always going to be responsible for your status bar color.

You can do the same with android:statusBarColor but it will work only in above API Level 21. ColorPrimaryDark for the status bar will also not support in API Level 19. By default in most of the API Levels, ColorPrimaryDark will be the default color for statusBarColor, So it is good to go with changing ColorPrimaryDark.

Tip: You can create multiple themes and you can use them in any activity. In any theme, There is a set of colors that needs to be defined, you can also create new colors in the colors.xml file in the same directory and use it on the styles.xml file.

Step 6: Now go to the manifest/AndroidManifest.xml and here search the activity for which you want to apply that theme or change the color of the status bar. and add an attribute android:theme=”@style/DemoTheme”.

That’s done! Check your application by running it on an emulator or a physical device.

Читайте также:  Живые обои для андроида с часовым механизмом

Method 2: Using setStatusBarColor Method

This method can be only used in the above API Level 21. Officially status bar color is not supporting below API Level 21. Although, Here we added an if condition, because in case if you haven’t selected above or equal to API 21 then it will check the android API Version, and then it will execute the code. It will not change the color of the status bar is below API Level 21 but the rest code will work well.

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. Don’t forget to replace your desired color with colorName.

Источник

Android status bar color change | android:colorPrimaryDark

Changing the Android status bar color is very easy in Android Studio. Set color on android:colorPrimaryDark the attribute of the style you’re using for your app in styles.xml.

Android 5.0 Lollipop introduced Material Design theme which automatically colors the status bar based on the colorPrimaryDark value of the theme.

Code

On API level 21+ you can also use the Window.setStatusBarColor() method from code.

You can use this simple code in Kotlin:

Let’s build an Android status bar color change application

Step 1. Create an android project in the android studio (Follow this tutorial: Android First Program in Android Studio).

Step 2. Open the color.xml file.

Читайте также:  Нокиа андроид one zeiss

Step 3. Add your color in the color.xml file.

You can add status bar color in 2 ways, First clicking on left side color box-> it will open a color selector.

Another way direct write a color hex code.

Step 4. Now Run the application, in an emulator or on your Android device.

Output screenshot Android android:colorPrimaryDark example:

Q: How to change Action Bar and Status Bar color in Activity?

Answer: If you want to change the color of my action bar and status bar programmatically only on one activity. Then use this Java(for Kotlin you just copy past and chose change to Kotlin) code.

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in Android Studio 3.3.2. Tested on Android 9 ( Android-P), compile SDK version API 28: Android 9.0 (Pie)
MinSdkVersion=25″
TargetSdkVersion=28″Coding in Kotlin

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

Источник

Оцените статью