Alba Ram
Alba Ram

Reputation: 35

Change launch activity style programmatically

I have an activity (MainActivity.java) that is used as the launch screen while the main fragment is being loaded and other functionality takes place in background. This launch screen shows a brown tile background and an icon always. What I want is to show that backgound (in R.style.AppTheme_NoActionBar_LauncherNight) only when the variable dayMode is false (variable in Constants.java). Otherwise, the background should be the one in R.style.AppTheme_NoActionBar_LauncherDay (a white background and the same icon).

If I specify one or another background in the android:theme part of my Manifest, it is shown nicely. But what I want is to set one theme or another programmatically, depending on the value of dayMode, on the onCreate method of the activity. This is what is not working.

I tried using setTheme before calling super.onCreate or setContentView, as I read in other answers, but it is not working. I only find answers explaining the order in which you should call setTheme and setContentView, but they do not solve this problem.

My styles:

 <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="autoCompleteTextViewStyle">@style/cursorColor</item>
        <item name="android:textColorSecondary">@color/yellow_light</item>
 </style>

 <style name="AppTheme.NoActionBar.LauncherNight">
        <item name="android:windowBackground">@drawable/launch_screen</item>
 </style>

 <style name="AppTheme.NoActionBar.LauncherDay">
        <item name="android:windowBackground">@drawable/launch_screen_day</item>
 </style>

My Manifest:

    <activity
            android:name="com.AlbaRam.myApp.MainActivity"
            android:configChanges="keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar.LauncherNight"
            android:launchMode="singleInstance"
            android:windowSoftInputMode="stateAlwaysHidden">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

My ActivityMain:

@Override
    protected void
    onCreate(Bundle savedInstanceState) {
        //This is not working
        if (Constants.dayMode){
            super.setTheme(R.style.AppTheme_NoActionBar_LauncherDay);
        } else {
            setTheme(R.style.AppTheme_NoActionBar_LauncherNight);
        }

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //rest of functionality

    }

Upvotes: 1

Views: 3403

Answers (3)

Alba Ram
Alba Ram

Reputation: 35

After some research I've discovered that this is not possible. Launch screens are used when we want to show something while our main functionality is loading, so we include the drawable we want to show in the Manifest to have this appearing fast while our main activity loads. This launch screen, as it is in the Manifest, appears before anything else, so if we could change the theme of the launch screen dinamically, we would lose that fast appearing while everything else loads.

Upvotes: 1

jins joseph
jins joseph

Reputation: 301

public void onCreate(Bundle savedInstanceState) {

 if (Constants.dayMode){

           setTheme(android.R.style.yourTheme);

        } else {

           setTheme(android.R.style.yourTheme);

        }
    super.onCreate(savedInstanceState);

    setContentView(R.layout.actvity);
}

Upvotes: 0

Mahmoud Waked
Mahmoud Waked

Reputation: 405

Try this code

theme.xml

<resources>
<style name="AppThemeLight" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
</style>
<style name="AppThemeDark" parent="Theme.AppCompat">
    <!-- Customize your theme here. -->
    <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
</style>
<!-- This will set the fade in animation on all your activities by default -->
<style name="WindowAnimationTransition">
    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
    <item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>

activity

  @Override
      protected void onCreate(Bundle savedInstanceState) {
        AppSettings settings = AppSettings.getInstance(this);
        setTheme(settings.getBoolean(AppSettings.Key.USE_DARK_THEME) ? R.style.AppThemeDark : R.style.AppThemeLight);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transition_theme);

        //

        }

Upvotes: 0

Related Questions