Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Status Bar Always Hidden on Android

Discussion in 'Android' started by lloydg, Oct 22, 2015.

  1. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    I don't think so, unless there's a special style for that.
     
  2. Lex

    Lex

    Joined:
    Dec 2, 2009
    Posts:
    42
    Actually it works, see my edit.
     
    Last edited: Jan 11, 2021
  3. Conferno

    Conferno

    Joined:
    Feb 27, 2014
    Posts:
    49
    Is it possible to dynamically hide the status bar and return it when I need?
     
  4. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    Does toggling Screen.fullScreen work for you?
     
  5. unity_o9r7XZsufSwv-g

    unity_o9r7XZsufSwv-g

    Joined:
    Jun 30, 2019
    Posts:
    1
    Hello Yury, I've been having the same issue about the status bar. I've tried your plugin in github and it's worked, the status bar is showing up again. The thing is I can't get the status bar to be translucent. It always show up in opaque even-though I don't change anything from the github (it's still the default translucent one). However I don't really understand how do I apply the plugin into my project, I get that I have to copy the 'Plugins' folder from the github to 'Assets' folder in my project. But for the 'src' folder and the rest file, I don't really know what I have to do with them, do I copy and paste it in temp folder? (which is temporary and deleted when the editor is closed). Or do I take the unity-classes.jar file in put it in separate folder from my project and then run the gradlew.bat? I'm sorry if it's kinda basic question, I'm pretty new to mobile apps development especially using Unity.

    edit 1: I don't really mind if it's opaque, but I don't want it to only be able in black color. Is it possible to add opaque color setting in your plugin? Or can you help me figuring out how to change status bar color in Unity?

    edit 2: I tried to do the gradlew assemble, and it's throwing me error stated that it didn't found the com.unity3d.player.UnityPlayerActivity; in the build process. Which one Unity's classes.jar that I need to put inside the libs folder? Where can I get it?
     
    Last edited: Jul 9, 2019
    MasterZuh and justdizzy like this.
  6. gableonardo

    gableonardo

    Joined:
    Jan 24, 2016
    Posts:
    1
    I'm developing an Android app and I'm trying to implement the Plugin on it, what I want is the status and nav bars to be shown by runtime and my whole app's UI to be resized because of the bars displacements. My issue is as follows:

    Both status bar and nav bar are properly showing (thus the status bar is tinted in grey color, don't really know why) after dropping the Plugin and adding the Screen.fullScreen = false and building the app for Android (I'm working on Unity 2019). But the app itself isn't resizing, the status and nav bars are right over the app's interface. Any ideas?

    I think it has something to do with the "How to build" steps, because I really didn't get them :(
     
  7. realragnvaldr

    realragnvaldr

    Joined:
    Jul 21, 2019
    Posts:
    40
    @Yury-Habets It's 4 years now since your brilliant suggestion to remove the "hide status bar" feature from Unity and you're still answering forums posts about workarounds ... Perhaps it's time to undo your suggestion? >.<

    I don't understand what the problem would be with a check box "Hide status bar" in the build settings (if there are any technological issues, they can be solved behind the scenes? Why bother developers with them?)

    Anyway, thanks for the great product. I really enjoy Unity :)
     
    UnrealTati likes this.
  8. XazeRekt

    XazeRekt

    Joined:
    Aug 21, 2016
    Posts:
    18
    There is much simplier solution that does not require you to fight with other plugins and manifest file.
    And I don't understand why it cannot be part of Unity when it's so simple to implement and there is no need to override unity activity.


    Code (CSharp):
    1.     public void SetupAndroidTheme(int primaryARGB, int darkARGB, string label = null)
    2.     {
    3. #if UNITY_ANDROID && !UNITY_EDITOR
    4.     label = label ?? Application.productName;
    5.     Screen.fullScreen = false;
    6.     AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    7.     activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
    8.     {
    9.         AndroidJavaClass layoutParamsClass = new AndroidJavaClass("android.view.WindowManager$LayoutParams");
    10.         int flagFullscreen = layoutParamsClass.GetStatic<int>("FLAG_FULLSCREEN");
    11.         int flagNotFullscreen = layoutParamsClass.GetStatic<int>("FLAG_FORCE_NOT_FULLSCREEN");
    12.         int flagDrawsSystemBarBackgrounds = layoutParamsClass.GetStatic<int>("FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
    13.         AndroidJavaObject windowObject = activity.Call<AndroidJavaObject>("getWindow");
    14.         windowObject.Call("clearFlags", flagFullscreen);
    15.         windowObject.Call("addFlags", flagNotFullscreen);
    16.         windowObject.Call("addFlags", flagDrawsSystemBarBackgrounds);
    17.         int sdkInt = new AndroidJavaClass("android.os.Build$VERSION").GetStatic<int>("SDK_INT");
    18.         int lollipop = 21;
    19.         if (sdkInt > lollipop)
    20.         {
    21.             windowObject.Call("setStatusBarColor", darkARGB);
    22.             string myName = activity.Call<string>("getPackageName");
    23.             AndroidJavaObject packageManager = activity.Call<AndroidJavaObject>("getPackageManager");
    24.             AndroidJavaObject drawable = packageManager.Call<AndroidJavaObject>("getApplicationIcon", myName);
    25.             AndroidJavaObject taskDescription = new AndroidJavaObject("android.app.ActivityManager$TaskDescription", label, drawable.Call<AndroidJavaObject>("getBitmap"), primaryARGB);
    26.             activity.Call("setTaskDescription", taskDescription);
    27.         }
    28.     }));
    29. #endif
    30.     }
    31.  
    32.     public int ToARGB(Color color)
    33.     {
    34.         Color32 c = (Color32)color;
    35.         byte[] b = new byte[] { c.b, c.g, c.r, c.a };
    36.         return System.BitConverter.ToInt32(b, 0);
    37.     }
    Use like this:

    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         Screen.fullScreen = false;
    4.         SetupAndroidTheme(ApplicationChrome.ToARGB(Color.black), ApplicationChrome.ToARGB(Color.black));
    5.     }
    Works on Unity 2020.1.11f1
     
    xharkx, Incole, prawn-star and 2 others like this.
  9. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    @XazeRekt I think your solution will return back to fullscreen on pause/resume (home and back to the app) and is not applied to the splash screen. The system UI listener within Unity will overwrite the flags once it gets the callback.

    I'd love to have an easy one-function solution, but unfortunately it doesn't work this way in Android.
     
  10. XazeRekt

    XazeRekt

    Joined:
    Aug 21, 2016
    Posts:
    18
    @Yury-Habets It does not return to full screen after pause/resume (home and back to the app). The only problem is that the splash screen is always full screen and there is no really workaround.
     
  11. sarynth

    sarynth

    Joined:
    May 16, 2017
    Posts:
    98
    XazeRekt's SetupAndroidTheme seems to be working well. (Had to remove "ApplicationChrome" as I included the function in the same class as Start.)

    The rest seems to be working on my device. I have the latest android though, and did not test on an earlier version.
     
  12. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    @Yury-Habets Hey, where can I find classes.jar?
    Something to note: I am also using Firebase.
     
  13. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    What is the classes.jar you are looking for?
     
  14. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    Hey, the jar for building your plugin.
    I am trying to get the plugin working, but I am a bit confused.
    After following the steps in the README and also combining the Firebase and status bar manifests.
    I am trying to build it to get it working.
    Did I miss something?
     

    Attached Files:

    • 1.PNG
      1.PNG
      File size:
      20.2 KB
      Views:
      316
  15. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    To find unity's classes.jar, locate your Unity install directory and grab something like this

    "c:\Program Files\Unity\Hub\Editor\2019.4.28f1\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Development\Classes\classes.jar"

    This is il2cpp dev variant, should be identical to mono just in case. non-development jars do differ.


    On merging the manifests, you likely got to do it manually. If you have a custom one in Assets/Plugins/Android/AndroidManfest.xml, then you have to modify it according to the instructions
    You also need to set `android:theme="@StyLe/UnityTransparentStatusBarTheme"` in the application tag.

    Of course, if your custom main manifest is very custom and has a custom activity, you'd have to do a bit more - can't help you out there.


    One more thing to consider: with Unity as a library, the manifest structure has changed... so I got to update the instructions for my project. Will do it once I have some free time.
     
    itsnottme and Lex like this.
  16. lucaswarner111

    lucaswarner111

    Joined:
    Feb 21, 2019
    Posts:
    4

    Thank you this worked for me. I have always bypassed the splash screens and just made my own. So this is totally ideal for me.
     
  17. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    60
    In order to show the status bar only and not the navigation bar, to make the most of the free space around the cutouts, I alway used this code in my fullscreen app:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. class StatusBarChanger
    4. {
    5.     private static int newStatusBarValue;
    6.  
    7.     public static void Show()
    8.     {
    9. #if UNITY_ANDROID
    10.         setStatusBarValue(2048); // WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
    11. #endif
    12.     }
    13.  
    14.     private static void setStatusBarValue(int value)
    15.     {
    16.         newStatusBarValue = value;
    17.         using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    18.         {
    19.             using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
    20.             {
    21.                 activity.Call("runOnUiThread", new AndroidJavaRunnable(setStatusBarValueInThread));
    22.             }
    23.         }
    24.     }
    25.  
    26.     private static void setStatusBarValueInThread()
    27.     {
    28. #if UNITY_ANDROID
    29.         using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    30.         {
    31.             using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
    32.             {
    33.                 using (var window = activity.Call<AndroidJavaObject>("getWindow"))
    34.                 {
    35.                     window.Call("setFlags", newStatusBarValue, newStatusBarValue);
    36.                 }
    37.             }
    38.         }
    39. #endif
    40.     }
    41.  
    42.  
    43.  
    44.  
    45.  
    46. }
    47.  
    But this code is not working anymore with Android 11 (R).

    In Android Docs you can read: "This constant was deprecated in API level 30. This value became API "by accident", and shouldn't be used by 3rd party applications."

    Searching around it seems to be replaced by WindowInsets.Type.statusBars() but I don't know how to modify the native code in C#.
    Could you help me?
     
  18. Incole

    Incole

    Joined:
    May 5, 2018
    Posts:
    10
    Hello, XazeRekt!

    Thank you for the solution! It works good even with other android workaround, like Advanced Input Field.
    I have one question: how make a bar white and icons - black?

    UPDATE

    Sorry for bothering, I found a solution :)
    Based on https://stackoverflow.com/questions/30075827/android-statusbar-icons-color (second answer)
    Just add these lines to original XazeRekt solution and it make you status bar icons black.

    Code (CSharp):
    1. AndroidJavaObject decor =  windowObject.Call<AndroidJavaObject>("getDecorView");
    2.         AndroidJavaClass viewClass = new AndroidJavaClass("android.view.View");
    3.        
    4.         int tintFlag = viewClass.GetStatic<int>("SYSTEM_UI_FLAG_LIGHT_STATUS_BAR");
    5.         decor.Call("setSystemUiVisibility", tintFlag);
     
    Last edited: Sep 25, 2021
    dingqun2020 likes this.
  19. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    Hey, were you able to find a solution for this?
     
  20. Incole

    Incole

    Joined:
    May 5, 2018
    Posts:
    10
    Hey, everyone!

    I found one more way to handle Status Bar on Android.
    This is a solution for full screen app with transparent status bar. You may change color of icons too.

    Code (CSharp):
    1.    
    2. public void SetupAndroidTheme(int primaryARGB, int darkARGB, bool iconsIsWhite = true, string label = null)
    3.     {
    4. #if UNITY_ANDROID && !UNITY_EDITOR
    5.         label = label ?? Application.productName;
    6.         Screen.fullScreen = false;
    7.         AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    8.         activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
    9.         {
    10.             AndroidJavaClass layoutParamsClass = new AndroidJavaClass("android.view.WindowManager$LayoutParams");
    11.            
    12.             int flagFullscreen = layoutParamsClass.GetStatic<int>("FLAG_FULLSCREEN");
    13.             int flagNotFullscreen = layoutParamsClass.GetStatic<int>("FLAG_FORCE_NOT_FULLSCREEN");
    14.             int flagTransluet = layoutParamsClass.GetStatic<int>("FLAG_TRANSLUCENT_STATUS");
    15.             int flagNoLimits = layoutParamsClass.GetStatic<int>("FLAG_LAYOUT_NO_LIMITS");
    16.            
    17.             AndroidJavaObject windowObject = activity.Call<AndroidJavaObject>("getWindow");
    18.            
    19.             windowObject.Call("clearFlags", flagFullscreen);
    20.             windowObject.Call("clearFlags", flagNotFullscreen);
    21.  
    22.             windowObject.Call("addFlags", flagTransluet);
    23.             windowObject.Call("addFlags", flagNoLimits);
    24.            
    25.             int sdkInt = new AndroidJavaClass("android.os.Build$VERSION").GetStatic<int>("SDK_INT");
    26.             int lollipop = 21;
    27.            
    28.             if (sdkInt > lollipop)
    29.             {
    30.                 windowObject.Call("setStatusBarColor", darkARGB);
    31.                 string myName = activity.Call<string>("getPackageName");
    32.                 AndroidJavaObject packageManager = activity.Call<AndroidJavaObject>("getPackageManager");
    33.                 AndroidJavaObject drawable = packageManager.Call<AndroidJavaObject>("getApplicationIcon", myName);
    34.                 AndroidJavaObject taskDescription = new AndroidJavaObject("android.app.ActivityManager$TaskDescription", label, drawable.Call<AndroidJavaObject>("getBitmap"), primaryARGB);
    35.                 activity.Call("setTaskDescription", taskDescription);
    36.             }
    37.  
    38.             if (iconsIsWhite)
    39.                return;
    40.  
    41.             AndroidJavaObject decor =  windowObject.Call<AndroidJavaObject>("getDecorView");
    42.             AndroidJavaClass viewClass = new AndroidJavaClass("android.view.View");
    43.        
    44.             int decorFlag = viewClass.GetStatic<int>("SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN");
    45.             decor.Call("setSystemUiVisibility", decorFlag);
    46.  
    47.             decorFlag = viewClass.GetStatic<int>("SYSTEM_UI_FLAG_LIGHT_STATUS_BAR");
    48.             decor.Call("setSystemUiVisibility", decorFlag);
    49.         }));
    50. #endif
    51.     }
     
    dingqun2020 likes this.
  21. jesenzhang

    jesenzhang

    Joined:
    Jul 18, 2017
    Posts:
    14
    As you said "The system UI listener within Unity will overwrite the flags once it gets the callback.", is there any way to overwrite the callback? I want to hide the navigation bar when it appears.
     
  22. Goa

    Goa

    Joined:
    Dec 4, 2012
    Posts:
    9
    I found a solution based on this: https://forum.unity.com/threads/how...ime-icons-in-mobile-app.1204675/#post-7698436

    I am using this code to show only the Status bar:
    Code (CSharp):
    1.         using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
    2.             using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
    3.                 using (var window = activity.Call<AndroidJavaObject>("getWindow")) {
    4.                     window.Call("setStatusBarColor", unchecked((int)0x00005700)); //for transparent status bar
    5.                     using (var Decor = window.Call<AndroidJavaObject>("getDecorView")) {
    6.                         using (var controller = Decor.Call<AndroidJavaObject>("getWindowInsetsController")) {
    7.                             using (var type = new AndroidJavaClass("android.view.WindowInsets$Type")) {
    8.                                 controller.Call("show", type.CallStatic<int>("statusBars"));
    9.                             }
    10.                         }
    11.                     }
    12.                 }
    13.             }
    14.         }
     
    PrisedRabbit likes this.
  23. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
  24. jesenzhang

    jesenzhang

    Joined:
    Jul 18, 2017
    Posts:
    14
  25. denravonska

    denravonska

    Joined:
    Nov 20, 2020
    Posts:
    12
    Why isn't this built into Unity again? Why do we have to go through hoops to just show the status bar?
     
  26. mickfcna

    mickfcna

    Joined:
    May 13, 2019
    Posts:
    46
    Seems it doesn't work anymore in Unity 2021.3 :-(
     
  27. UnrealTati

    UnrealTati

    Joined:
    Aug 18, 2017
    Posts:
    5
    I really don't wanna touch the UnityPlayer activity. I wish there was a way to show the status bar. I even did Screen.fullScreen = false, nah. It's not showing. System status bar is an important feature. It has lots of stuff, namely notifications, that i don't want to handle by myself. I wish there was really an easy way to show it.
     
    Danny9421 likes this.
  28. Danny9421

    Danny9421

    Joined:
    Mar 21, 2022
    Posts:
    32
    There really should exist a better option to enable the status bar. I dont want to mess with the androidmanifest as it changes while coding (requesting phone permissions) and unity does a great job at generate it... or changing other parts. The risk is high to break things on future builds.
    The status bar is an essential feature and i see it enabled in a lot of popular portrait mode games.
    Please readd a toggle to enable it.
     
    Last edited: Aug 15, 2022
  29. Spellbook

    Spellbook

    Joined:
    May 21, 2015
    Posts:
    29
  30. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    If it happens in Unity without the plugin linked, then you should report the issue through the Editor in a normal way.

    If it happens with the status bar visible plugin, please file an issue on github and I'll look into it once time permits.

    Thanks!
     
  31. dungmv

    dungmv

    Joined:
    Apr 9, 2015
    Posts:
    6
    so, after 7 years. we still cannot show the status bar on android