Android Tutorial Changing the theme at runtime

Discussion in 'Tutorials & Examples' started by Erel, Aug 16, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Not sure. Maybe @corwin42 will be able to find the cause of this.
     
  2. joneden

    joneden Active Member Licensed User

    I found a work around and can set the status bar colour manually. It's a roundabout way of doing it but it still gets the theme colour. If anyone else runs into the same issue use the code below.

    Code:
    Dim objAppCompat As AppCompat
        
    Dim jo As JavaObject
        jo.InitializeContext
        jo.RunMethodJO(
    "getWindow"Null).RunMethod("setStatusBarColor"Array(objAppCompat.GetThemeAttribute("colorPrimaryDark")))
     
    koaunglay and ArminKH like this.
  3. joneden

    joneden Active Member Licensed User

    Seems that the issue relates to SDK22 - 23 seems to work fine without any custom work around :)
     
    Erel likes this.
  4. joneden

    joneden Active Member Licensed User

    No changing the theme in the manifest editor changes it once, this thread discusses changing the active theme while the app is running.
     
  5. ArminKH

    ArminKH Well-Known Member

    can u (or every body)give me the java code of above code?following code doesn't works
    Code:
    import android.view.WindowManager;


    public class T8Extra {
       
        
    public void SetStatusBarColor(int Color) {
            
    if (android.os.Build.VERSION.SDK_INT >= 21) {
               
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                getWindow().setStatusBarColor(Color);
            
    }
        }
    }
    tnx
     
  6. jameslem

    jameslem New Member

    does the zipfile contains the code? i cant the see the java files
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    It contains the B4A code.
     
  8. fgrdovic21

    fgrdovic21 Member Licensed User

    Hi all

    Is it possible to programmatically change the colors of a theme (colorPrimary, colorPrimaryDark, colorAccent) or Toolbar/Statusbar to any color, not to a predefined theme? Or at least, set the opacity of a Statusbar to, perhaps, 10% so it darkens the control underneath.

    Thanks
     
  9. ArminKH

    ArminKH Well-Known Member

    post 22 is exactly what do you want to do
    just instead
    Array(objAppCompat.GetThemeAttribute("colorPrimaryDark"))
    you should use your color like colors.red
     
  10. fgrdovic21

    fgrdovic21 Member Licensed User

    Thanks ArminKH, that worked like a charm. I didn't know you can put colors there. Next time, maybe I should check what parameters that method uses :D
    I can also confirm you can put transparency and it will brighten or dimmer the statusBar (ARGB(20,0,0,0) for example).
     
  11. trueboss323

    trueboss323 Active Member Licensed User

    Is it possible to load a theme that is stored in my ...\Objects\res\values folder?
     
    Last edited: Jun 9, 2016
  12. Erel

    Erel Administrator Staff Member Licensed User

    Yes. However you don't need to load it at runtime. Load it from the manifest editor instead.
     
  13. trueboss323

    trueboss323 Active Member Licensed User

    I do not mean from the manifest. In my case, I have multiple themes that I want to use. I want to give users the option of what theme they want, and depending on their selection, would load the appropriate theme file.
     
    Last edited: Jun 9, 2016
  14. vbmundo

    vbmundo Well-Known Member Licensed User

    Ok,

    But if you only need to set a fixed Theme in your activity you can include only this

    Code:
    #if java
    public void _onCreate() {
        setTheme(16973931);
    }
    #end if
    16973931 = Holo Theme

    without finish and recreate the Activity

    This works for me..

    Regards
     
  15. trueboss323

    trueboss323 Active Member Licensed User

    Should I place the different xml files inside \Objects\res\values ? Then how would I load that specific file?
     
  16. vbmundo

    vbmundo Well-Known Member Licensed User

    I don't know, perhaps I'm not the experts that you need, I only simplify the Erel's code (because my APP don't allow users to change the Design) if you see the Erel's code, for every click in diferent buttons, the system retrieve the ID of every theme and apply it.

    I only put the ID directly on _onCreate().

    This Works for me...
     
  17. silpot

    silpot Member Licensed User

    This theme changer works perfect when I try to exit from app by using the phone's back button. I can change the theme from Theme.Holo.Light to Theme.Holo and vice versa.
    The problem is that when I kill (close) all running apps from my cell phone, the theme returns to the default one.

    Is there any solution to that problem ?
    Is there any other solution to let the user change the theme when he wants to ?
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    You will need to store the theme value and set it when FirstTime is true. This will cause the activity to be recreated.
     
  19. silpot

    silpot Member Licensed User

    Thanks Erel. I already did that. When the app is runing for the first time it reads a stored value and restarts to the desired theme.
    The whole process of restarting, creates bad layout transition between the dark and the Light theme

    Do you have a functional example to do that better ?
     
    Last edited: Jan 24, 2019
  20. Erel

    Erel Administrator Staff Member Licensed User

    Done. See the updated example.
     
    trueboss323 and DonManfred like this.
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