Android Question Change StatusColor from class

Discussion in 'Android Questions' started by Yunus ÖZ, Aug 19, 2018.

Similar threads

B4A Code Snippet Theme Colors
B4A Code Snippet Change CheckBox colors
B4A Tutorial Cards list with CustomListView
B4A Code Snippet Change EditText Colors
B4A Code Snippet [B4X] [XUI] Faster* DrawPolygon Method
B4A Code Snippet Custom Toast Message
  1. Yunus ÖZ

    Yunus ÖZ Member Licensed User

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

    Code:
    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:
    Public f As funcs
    when i call it from any activity not working :(
     
  2. npsonic

    npsonic Active Member Licensed User

    Your phone or emulator is using sdk version >21?
    Is there any error messages?
     
  3. Yunus ÖZ

    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
     
  4. npsonic

    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.
     
  5. Yunus ÖZ

    Yunus ÖZ Member Licensed User

    Dear @npsonic
    I'm not exactly sure, maybe we can use reflection library to solve it???????
     
  6. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    Attached Files:

  7. Yunus ÖZ

    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
    Code:
    Sub Process_Globals
        
    Public f As funcs
    End Sub

    in HomePage Activity
    Code:
    Sub Activity_Create(FirstTime As Boolean)
        Main.f.statusBarColor(
    Colors.RGB(12783124))
    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)
     
  8. npsonic

    npsonic Active Member Licensed User

    If you don't need to change color during runtime you can also set it from manifest.
     
  9. Yunus ÖZ

    Yunus ÖZ Member Licensed User

    OK i will solve the my problem like this,

    statusBarColor function in my class
    Code:
    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:
    Sub Process_Globals
        
    Public f As funcs
    End Sub
    and calling in another activity
    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    Dim jObj As JavaObject
        jObj.InitializeContext
        Main.f.statusBarColor(jObj,
    Colors.RGB(12783124))
    End Sub
    Because class cant understand the current context, i'm sending :)
     
  10. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    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).
     
  11. Yunus ÖZ

    Yunus ÖZ Member Licensed User

    I initialized it in Main activity too, and the others activities run
    i think there is no need to initialize again and again, whatever it is running now
    thank you for all,
    Best regards ;)
     
  12. Yunus ÖZ

    Yunus ÖZ Member Licensed User

    you're absolutely right dear @Semen Matusovskiy
    and i understood it with this app :) if the class uses context, it has only general functions except statusBarColor

    Thank you again for all
    Sincerely :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice