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

Code: Copy
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
Code: Copy
Public f As funcs

when i call it from any activity not working :(
 
Your phone or emulator is using sdk version >21?
Is there any error messages?
 
Upvote 0
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
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
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
Code: Copy
Sub Process_Globals
    Public f As funcs
End Sub


in HomePage Activity
Code: Copy
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
If you don't need to change color during runtime you can also set it from manifest.
 
Upvote 0
OK i will solve the my problem like this,

statusBarColor function in my class
Code: Copy
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
Code: Copy
Sub Process_Globals
    Public f As funcs
End Sub

and calling in another activity
Code: Copy
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
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
Back
Top