Android Question Change StatusColor from class

Yunus ÖZ

Member
Licensed User
I have a class file name funcs, and there is a code like this

B4X:
Public Sub statusBarColor(color As Int)
    If ph.SdkVersion >= 21 Then
        Dim jObj As JavaObject
        jObj.InitializeContext
        Dim window As JavaObject = jObj.RunMethodJO("getWindow", Null)
        window.RunMethod("addFlags", Array (0x80000000))
        window.RunMethod("clearFlags", Array (0x04000000))
        window.RunMethod("setStatusBarColor", Array(color))
    End If
End Sub

and i defined it to main activity in process_globall
B4X:
Public f As funcs

when i call it from any activity not working :(
 

npsonic

Active Member
Licensed User
Your phone or emulator is using sdk version >21?
Is there any error messages?
 
Upvote 0

Yunus ÖZ

Member
Licensed User
The class was defined in all activities, and there was no problem.
When i decide to remove all of them (class definitons) and calling from in main activity, problem was occured.
Javaobject knows only "main activity" not the others
 
Upvote 0

npsonic

Active Member
Licensed User
Yeah, it doesn't work like that. What you could do is to create new Code Module and add all necessary code into it.
You can change color from every Activity by using StatusBarColor method in Code Module. That's how different Activities can share same code.
 
Upvote 0

Yunus ÖZ

Member
Licensed User
If I define that class in every activity, yes.
but if I define it in the main activity and call it from other activities as below, no.

in Main Activity
B4X:
Sub Process_Globals
    Public f As funcs
End Sub


in HomePage Activity
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Main.f.statusBarColor(Colors.RGB(127, 83, 124))
End Sub

statusBarColor works as if it were called from the main activity. cant get the current activity.
and i'm asking is there any way (maybe reflection)
 
Upvote 0

npsonic

Active Member
Licensed User
If you don't need to change color during runtime you can also set it from manifest.
 
Upvote 0

Yunus ÖZ

Member
Licensed User
OK i will solve the my problem like this,

statusBarColor function in my class
B4X:
Public Sub statusBarColor(jObj As JavaObject,color As Int)
   If ph.SdkVersion >= 21 Then
       Dim window As JavaObject = jObj.RunMethodJO("getWindow", Null)
       window.RunMethod("addFlags", Array (0x80000000))
       window.RunMethod("clearFlags", Array (0x04000000))
       window.RunMethod("setStatusBarColor", Array(color))
   End If
End Sub

definition in Main activity
B4X:
Sub Process_Globals
    Public f As funcs
End Sub

and calling in another activity
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim jObj As JavaObject
    jObj.InitializeContext
    Main.f.statusBarColor(jObj,Colors.RGB(127, 83, 124))
End Sub

Because class cant understand the current context, i'm sending :)
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Because class cant understand the current context

Class understands current context very well.
Something talks me that you didn't initialize class object

Meanwhile https://www.b4x.com/android/forum/threads/classes-tutorial.18626/ talks
Initialize - A class object should be initialized before you can call any other sub. Initializing an object is done by calling the Initialize sub. When you call Initialize you set the object's context (the parent activity or service).
 
Upvote 0
Top