How to make status bar transparent android

how to make fully Android Transparent Status bar

how to make fully Android Transparent Status bar

Hello readers, You are here because you are interested in making your Android app’s status bar fully transparent as title of this post says android transparent status bar. You can do this from styles.xml or you can do this in JAVA code within an activity.

Why making android transparent status bar from java?

We are doing this from java to avoid multiple changes in style.xml, color.xml etc. By this piece of code we will do everything in 1 place. If you have already tried doing changes in style.xml and got half transparent status bar then your problem will be solved by this method, because it will clear window flag and set new window flag to do its work.

This method will work on Android version above 21, means this will work on devices having Android lollipop and above. On android version 19 and 20 (Android kitkat) fully transparent status bar will not work but we will make status bar Translucent instead. Below is screenshot from 3 devices having android version Jelly Bean, Kitkat and Android N to show you difference.

So lets start :

Open class file of your activity, in my case I’m going to open LoginActivity.java file, and inside onCreate() method paste below code and run your app on Android version 21 or higher to see the changes.

below is java code, but if you are working on kotlin then scroll down to get kotlin code as well.

for kotlin developers

So here is how this method work :

First we will check android version is greater than 19 and less than 21 then we will set translusant status bar. and if android version is greater than 21 then we will set “android transparent status bar”. That’s it now you gave to do this in all your activites, or you can create a BaseActivity having code of “android transparent status bar” and extend all your activity with BaseActivity and you are done.

Источник

Android: Full Screen UI with Transparent Status Bar

Activities, the building block of any Android app. Something so simple, yet so complex. Here we are going to talk about something similar related to activities which looks very simple from the outset but gets complex pretty soon. We will build a full screen layout with transparent status bar. I’m not going to talk about why would you need a full screen layout and in what situations. That’s a topic for another discussion.

However, here’s a simple use-case. If you have ever seen any app with a map(like a ride-hailing app), you would see that the map occupies the space below the status bar as well. The content of the layout other than the map doesn’t overlap with the system bar icons. Doesn’t it look sweet?
So, we are just gonna recreate that UI. Something like these:

Читайте также:  Android emulator usb keyboard

Set a theme for the Activity
Let’s start with the basics(i assume you already have created an Activity with a map and some content) and so, let’s set a theme for our activity:

And then apply theme to the activity as usual:

Easy peasy!! Let’s see what we have got.

Make UI fullscreen
Now, let’s get down to the fun part. How to make the layout a full screen layout and set the status bar colour?

Let’s jump right into code, try it out on your device and then let’s get down to understanding what is actually happening:

For lollipop and above devices:

What is a Window?
When you open any standard app, you would see a status bar, a navigation bar, and the actual activity. Each of these components have a different window. Each of these components are given a window to draw themselves into. The activity is given a window where it draws the view hierarchy specified by us. The status bar is given a window where the system draws things like time, battery, notification icons etc. The navigation bar also has a different window where it draws the back button, the home button etc. All these windows on a single screen are managed by WindowManager.

What are these different flags you can apply to a window?
If FLAG_TRANSLUCENT_STATUS is enabled, translucent status bar will be shown which we don’t want. So, we first remove this flag.

FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag tells the system that our window is responsible for drawing the background for system bars.

What is systemUiVisibility?
Using this method, we can control the visibility of the system UI drawn by the system. System UI elements are elements like status bar, naviagtion bar etc. SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN helps keep the content from resizing when the system bars hide and show while going in and out of full screen mode.

I think setStatusBarColor() needs no explanation. However, this API is available on or above API 21.

Problem on marshmallow and above devices:
The system bar icons are all white which may not look good if the general colour scheme in your layout is also light.

How to fix it?
Again, setSystemUiVisibility to the rescue. SYSTEM_UI_FLAG_LIGHT_STATUS_BAR makes sure that the status bar icons are drawn in such a way so that they are fully visible in light mode.

Cool..so, now our status bar looks pretty good on most of the API levels. But but.. what’s up with the Floating Action Button. This guy is overlapping with the system bar icons and this kind of UI makes me as an user uncomfortable. So, let’s fix this:

Shift the content
The actual content of your app layout needs to be shifted down so that it doesn’t overlap with the status bar icons. Ok, what do we need to shift it down? Margin..a marginTop will do. Right?
But now comes the million dollar question. How do we know how much marginTop do we need to give to the content view? There are many ways to do that but i’m going to go with the most definitive approach which has worked for me on a variety of devices.

Читайте также:  Погода 4pda android apk

Window insets to the rescue….
What are window insets?
Window insets gives us the size of system view that we would need here. The system view we are talking of here is the status bar. So, the top window inset would give us the margin that we need to apply to our content.

How to get the insets of your current visible window?

Now, it’s just down to getting the view and setting a marginTop on it equal to the topInset of the window:

View Extensions for reuse
Usually in a large application, things like these are repeated. So, wouldn’t it be better to create a helpful kotlin extension on the Activity which we can call from any activity instead of repeating this code everywhere?

And then all we need to do in any Activity is:

A sample Android app using these concepts can be found here.

Источник

Translucent SystemBars the right way — across API levels and themes

To draw edge to edge with translucent status and navigation bars on v21+ with dark and light themes.

This article assumes you have already gone through Gesture Navigation- Going Edge to Edge (I)

Since we want the content to be drawn behind the system bars, it means that

  1. We need translucency
  2. We need either dark or light scrims for navigation bar and statusBar as per the current theme.

Capability

  • Fully configurable statusBarColor
    But we are limited to using darker colors as the statusBar icons remain white colored.
  • windowTranslucentStatus true will override statusBarColor and apply a fixed dark scrim that can’t be changed.
  • Fully configurable navigationBarColor
    But we are limited to using darker colors as the navigation buttons remain white colored.
  • windowTranslucentNavigation true will override navigation BarColor and apply a fixed dark scrim that can’t be changed.
  • Status bar icons & navigation bar buttons always remain light.
  • windowLightStatusBar
    Enables the statusBar icons to be dark
  • windowLightNavigationBar
    Enables the navigation bar buttons to be dark.

Solution

Note: We will be using a transparent statusBarColor. Because we will instead use AppBar to color the statusBar. Since the AppBar covers the statusBar’s area, it should be enough to set the relevant background to AppBar directly.

In your implementation, this can be any view that is on the top. If there is no view on the top of the screen , you can instead use statsuBarColor attribute. It will give the same results.

Base values:

AppBarLayout:

v21 — v22:

  • We ditch windowTranslucentStatus and windowTranslucentNavigation, as the system will apply a dark scrim if we use them.
  • statusBar and navigationBar icons remain white colored.
  • Means, we can only use dark backgrounds/scrims.

We got 2 options here:

1. Use different backgrounds for dark and light themes.

In case of dark theme the colorSurface will be dark/black. We can use a similar color for systemBars as well.
In case of light theme, the colorSurface will be light/white. We can’t use a similar color for systembars as the icons are white.

Читайте также:  Netflix android apk mod

Basically, we can’t handle light themed system bars very well on v21–22. So we are left with using dark backgrounds only .

2. [chosen]Use same background for dark and light themes.

We can work our way by using dark backgrounds for dark and light themes.

Источник

joinAero / TransparentStatusBarActivity.java

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

xml version = » 1.0 » encoding = » utf-8 » ?>
android .support.v4.widget.DrawerLayout
xmlns : android = » http://schemas.android.com/apk/res/android «
xmlns : app = » http://schemas.android.com/apk/res-auto «
xmlns : tools = » http://schemas.android.com/tools «
android : id = » @+id/drawer «
android : layout_width = » match_parent «
android : layout_height = » match_parent «
android : fitsSystemWindows = » true «
tools : context = » .ui.demo.TransparentStatusBarActivity «
tools : openDrawer = » end » >
android .support.design.widget.CoordinatorLayout
android : layout_width = » match_parent «
android : layout_height = » match_parent «
android : fitsSystemWindows = » true » >
include layout = » @layout/toolbar »/>
android .support.v7.widget.RecyclerView
android : id = » @+id/recycler «
android : layout_width = » match_parent «
android : layout_height = » match_parent «
android : scrollbars = » none «
app : layout_behavior = » @string/appbar_scrolling_view_behavior »/>
android .support.design.widget.CoordinatorLayout>
android .support.design.widget.NavigationView
android : id = » @+id/nav «
android : layout_width = » wrap_content «
android : layout_height = » match_parent «
android : layout_gravity = » end «
android : fitsSystemWindows = » true «
app : headerLayout = » @layout/nav_header_main «
app : menu = » @menu/nav_menu_main »/>
android .support.v4.widget.DrawerLayout>

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

Источник

joinAero / TransparentStatusBarActivity.java

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

xml version = » 1.0 » encoding = » utf-8 » ?>
android .support.v4.widget.DrawerLayout
xmlns : android = » http://schemas.android.com/apk/res/android «
xmlns : app = » http://schemas.android.com/apk/res-auto «
xmlns : tools = » http://schemas.android.com/tools «
android : id = » @+id/drawer «
android : layout_width = » match_parent «
android : layout_height = » match_parent «
android : fitsSystemWindows = » true «
tools : context = » .ui.demo.TransparentStatusBarActivity «
tools : openDrawer = » end » >
android .support.design.widget.CoordinatorLayout
android : layout_width = » match_parent «
android : layout_height = » match_parent «
android : fitsSystemWindows = » true » >
include layout = » @layout/toolbar »/>
android .support.v7.widget.RecyclerView
android : id = » @+id/recycler «
android : layout_width = » match_parent «
android : layout_height = » match_parent «
android : scrollbars = » none «
app : layout_behavior = » @string/appbar_scrolling_view_behavior »/>
android .support.design.widget.CoordinatorLayout>
android .support.design.widget.NavigationView
android : id = » @+id/nav «
android : layout_width = » wrap_content «
android : layout_height = » match_parent «
android : layout_gravity = » end «
android : fitsSystemWindows = » true «
app : headerLayout = » @layout/nav_header_main «
app : menu = » @menu/nav_menu_main »/>
android .support.v4.widget.DrawerLayout>

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

Источник

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