Android Question How to call getwindow for an activity from Code Module

KZero

Active Member
Licensed User
Hi,
i use this code in all my activities to the change status bar color

B4X:
Sub SetStatusBarColor(clr As Int)
    Dim p As Phone
    If p.SdkVersion >= 21 Then
        Dim jo As JavaObject
        jo.InitializeContext
        Dim window As JavaObject = jo.RunMethodJO("getWindow", Null) '<<<< how to call it from outside the activity
        window.RunMethod("addFlags", Array (0x80000000))
        window.RunMethod("clearFlags", Array (0x04000000))
        window.RunMethod("setStatusBarColor", Array(clr))
    End If
      If p.SdkVersion >= 23 Then
       jo = Activity
        jo.RunMethod("setSystemUiVisibility", Array(8192))
      End If
End Sub
is it possible to put this function in Code Module and pass activity reference in the function instead ?
 

LucaMs

Expert
Licensed User
I used your routine after placing it in a code module and it worked, passing Activity to it.

Anyway, I think you could place your routine inside a class and create an instance of this in each Activity.

Or try:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Activity.LoadLayout("Layout1")
    Dim jo As JavaObject
    jo.InitializeContext
    ModProjUtils.SetStatusBarColor(jo, Activity, Colors.Yellow)
End Sub
B4X:
' ModProjUtils - Code module.

Public Sub SetStatusBarColor(Context As Object, Act As Activity, clr As Int)
    Dim p As Phone
    If p.SdkVersion >= 21 Then
        Dim jo As JavaObject = Context
        Dim window As JavaObject = jo.RunMethodJO("getWindow", Null) '<<<< how to call it from outside the activity
        window.RunMethod("addFlags", Array (0x80000000))
        window.RunMethod("clearFlags", Array (0x04000000))
        window.RunMethod("setStatusBarColor", Array(clr))
    End If
    If p.SdkVersion >= 23 Then
        jo = Act
        jo.RunMethod("setSystemUiVisibility", Array(8192))
    End If
End Sub
 
Last edited:

LucaMs

Expert
Licensed User
because jo.InitializeContext Initializa the object with the current context, actually, current activity.
I used your routine after placing it in a code module and it worked, passing Activity to it.
I was surprised that it worked, but instead it's obvious, a code module doesn't have its own context (which is why it doesn't support events, for example).
 

KZero

Active Member
Licensed User
I used your routine after placing it in a code module and it worked, passing Activity to it.

Anyway, I think you could place your routine inside a class and create an instance of this in each Activity.

Or try:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Activity.LoadLayout("Layout1")
    Dim jo As JavaObject
    jo.InitializeContext
    ModProjUtils.SetStatusBarColor(jo, Activity, Colors.Yellow)
End Sub
B4X:
' ModProjUtils - Code module.

Public Sub SetStatusBarColor(Context As Object, Act As Activity, clr As Int)
    Dim p As Phone
    If p.SdkVersion >= 21 Then
        Dim jo As JavaObject = Context
        Dim window As JavaObject = jo.RunMethodJO("getWindow", Null) '<<<< how to call it from outside the activity
        window.RunMethod("addFlags", Array (0x80000000))
        window.RunMethod("clearFlags", Array (0x04000000))
        window.RunMethod("setStatusBarColor", Array(clr))
    End If
    If p.SdkVersion >= 23 Then
        jo = Act
        jo.RunMethod("setSystemUiVisibility", Array(8192))
    End If
End Sub
i was looking for another way to get the activity context from the code module and just passing the activity in the function
but for now this code much better than placing the whole function in every module 🙌

Yes, you can put in static code module.


You don't need do so. Just Call, say Common.SetStatusBarColor(yourColor) in Activity_Resume or other place, because jo.InitializeContext Initializa the object with the current context, actually, current activity.

View attachment 89232
I tried it , and it always throw exception
java.lang.RuntimeException: Method: getContext not found in: b4a.example.asettings
 

Jorge M A

Active Member
Licensed User
This is my code, (please note that is only one parameter: color)
Common Static Module:
Sub SetStatusBarColor(clr As Int)
    Dim p As Phone
    If p.SdkVersion >= 21 Then
        Dim jo As JavaObject
        jo.InitializeContext
        Dim window As JavaObject = jo.RunMethodJO("getWindow", Null)
        window.RunMethod("addFlags", Array (0x80000000))
        window.RunMethod("clearFlags", Array (0x04000000))
        window.RunMethod("setStatusBarColor", Array(clr))
    End If
End Sub
Example:
Code for Activity1:
Sub Activity_Click
    System.SetStatusBarColor(Rnd(0xFF000000, 0))
End Sub
Activity2:
Sub Activity_Resume
    System.SetStatusBarColor(Rnd(0xFF000000, 0))
End Sub
 

KZero

Active Member
Licensed User
This is my code, (please note that is only one parameter: color)
Common Static Module:
Sub SetStatusBarColor(clr As Int)
    Dim p As Phone
    If p.SdkVersion >= 21 Then
        Dim jo As JavaObject
        jo.InitializeContext
        Dim window As JavaObject = jo.RunMethodJO("getWindow", Null)
        window.RunMethod("addFlags", Array (0x80000000))
        window.RunMethod("clearFlags", Array (0x04000000))
        window.RunMethod("setStatusBarColor", Array(clr))
    End If
End Sub
Example:
Code for Activity1:
Sub Activity_Click
    System.SetStatusBarColor(Rnd(0xFF000000, 0))
End Sub
Activity2:
Sub Activity_Resume
    System.SetStatusBarColor(Rnd(0xFF000000, 0))
End Sub
this is working in Activity_Resume🤩,i was trying in Activity_Create , don't know why it's not working from Activity_Create but both do the job
Thanks ✌
 
Top