Android Code Snippet ActionBar and StatusBar colors programmatically with Edge to Edge

Code to add a panel behind the status bar and action bar.

1786341184741.png


1. Make the action bar transparent by adding this to the manifest editor (keep the CreateResourceFromFile(Macro, Themes.LightTheme) line):

B4X:
'change windowLightStatusBar to true for dark text in the status bar
CreateResource(values-v20, theme.xml,
<resources>
    <style
        name="LightTheme" parent="@android:style/Theme.Material.Light">
               <item name="android:actionMenuTextAppearance">@style/LowerCaseMenu</item>
            <item name="android:windowLightStatusBar">false</item>
            <item name="android:colorPrimary">@color/actionbar</item>
    </style>
     <style name="LowerCaseMenu" parent="android:TextAppearance.Material.Widget.ActionBar.Menu">
        <item name="android:textAllCaps">false</item>
    </style>
</resources>
)
CreateResource(values, colors.xml,
<resources>
    <color name="actionbar">@android:color/transparent</color>
</resources>
)

B4X:
Private Sub SetTopBarColorAndTitle(BackgroundColor As Int, TitleColor As String, Title As String)
    Dim top As B4XView = xui.CreatePanel("")
    top.Color = BackgroundColor
    Dim act As Activity = B4XPages.GetNativeParent(Me) 'Activity with non-B4XPages
    Dim r As Rect = ime.GetContentRect
    act.AddView(top, 0, 0, act.Width, r.Top)
    Dim cs As CSBuilder
    cs.Initialize.Color(TitleColor).Append(Title).Pop
    B4XPages.SetTitle(Me, cs) 'Activity.Title = cs with non-B4XPages
End Sub

Usage example:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    ime.Initialize("")
    SetTopBarColorAndTitle(0xFF005CE2, xui.Color_White, "title here")
    Root.LoadLayout("MainPage")
End Sub
 

Attachments

  • colors.zip
    13.3 KB · Views: 150
Last edited:

Alessandro71

Well-Known Member
Licensed User
Longtime User
does it require a minimum Android version, beside B4A 13.7 and #EdgeToEdgeOldDevices: True ?
 

GeoT

Active Member
Licensed User
Longtime User
If you want the three dots on the OverlayPanel to be white like the title, change this line
B4X:
<style name="LightTheme" parent="@android:style/Theme.Material.Light">
to this one
B4X:
<style name="LightTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">

It works for me with Android 16, with android:targetSdkVersion="36" or android:targetSdkVersion="35", and with B4A 13.70
 

GeoT

Active Member
Licensed User
Longtime User
On Android 11, if I don't include the #EdgeToEdgeOldDevices line, a blank space is left where the native B4XPages ActionBar would normally be.
 

GeoT

Active Member
Licensed User
Longtime User
Optionally, you can also set the navigation bar color like this:

B4X:
Dim ime As IME
SetNavigationBarColor(0xFF512DA8)

Private Sub SetNavigationBarColor(BackgroundColor As Int)
    Dim Content As Rect = ime.GetContentRect
    Dim navBar As B4XView = xui.CreatePanel("")
    navBar.Color = BackgroundColor
    Activity.AddView(navBar, 0, Content.Bottom, Activity.Width, Activity.Height - Content.Bottom)
End Sub
 
Last edited:
Top