If statement problem?

Last Updated:

  1. SystemBomber

    SystemBomber New Member

    Hey there,
    I'm really confused right now as to why this isn't working.
    What I am trying to achieve is to allow the user to select either the Light theme or the Dark theme. Now I have got that bit sorted (Storing it in preferences). The problem is coming to applying the theme. I'm trying to apply the theme before the super.onCreate() method in the onCreate method.

    Code (Text):
    3. public static String mSettingTheme;
    5. protected void onCreate(Bundle savedInstanceState)
    6.  {
    8.  // Load the settings from Disk and set anythign up we need to before drawing.
    10. mSettingTheme = loadSettings();
    11. if( mSettingTheme == "Light" )
    12. {
    13.    setTheme(android.R.style.Theme_Light);
    14. }
    15. else if (mSettingTheme == "Dark")
    16. {
    17.     setTheme(android.R.style.Theme_Black);
    18. }
    20. super.onCreate(savedInstanceState);
    21.  // Load view    
    22.  setContentView(R.layout.settings);
    The loadSettings() method is returning a string with the name of the theme to load ("Light" or "Dark"). However neither of the if conditions are met and it fails to load either and I cannot understand why.

    Thanks in advance for any help :)

  2. KlaymenDK

    KlaymenDK Well-Known Member

    Object Oriented Programming 101: Your class must call super as the #1 thing.

    Having said that, why is it important to you to run that statement prior to super? Have you tried to run it between the super and your setContentView() ?
  3. SystemBomber

    SystemBomber New Member

    Well when setting a theme, it seems to only work before super. Which is a pain in the butt really. Does not work between super and setContentView. :confused:
  4. KlaymenDK

    KlaymenDK Well-Known Member

    Well, according to the canonical example it's supposed to work like this:
    Code (Text):
    1. protected void onCreate(Bundle savedInstanceState) {
    2.     super.onCreate(savedInstanceState);
    3.     ...
    4.     setTheme(android.R.style.Theme_Light);
    5.     setContentView(R.layout.linear_layout_3);
    6. }
    Maybe there's a bug in your theme XML?

Share This Page