AndyCr15
AndyCr15

Reputation: 475

Programmatic Theme Change Not Working

My app loads in a bit of data to start, it takes a couple of seconds so I would like a Splash Screen to display for this time.

All I can see to make sure I do is use setTheme before the super.onCreate, which I am doing, but as it loads the MainActivity the background is the splash screen and the actionbar has the splash screen image squashed into it.

Here is my styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:background">@drawable/background_portrait</item>
    </style>

    <style name="SplashTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:background">@drawable/splashscreen</item>
    </style>

In my AndroidManifest.xml I have android:theme="@style/SplashTheme"

SplashScreen.java runs all the code that takes time, then switches to MainActivity.java where I have -

@Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

What am I missing? It also seems the splashscreen background is now a background for other things, like some TextViews and the action bar?

Thanks for any help offered.

EDIT Also, the code in the splash screen xml doesn't run. It just loads the background and on completion, loads the Main Activity?

The SplashScreen.java has -

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash_screen);

        Log.i("Splash Screen", "loading");

        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        // code that takes time

The activity_splash_screen.xml is -

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splashscreen"
    tools:context="com.androidandyuk.bikersbestfriend.SplashScreen">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_margin="32dp"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"></com.google.android.gms.ads.AdView>

</FrameLayout>

Upvotes: 0

Views: 527

Answers (1)

Eugen Pechanec
Eugen Pechanec

Reputation: 38223

Don't use android:background in a theme. Anything that doesn't have a background will inherit this one.

You meant to set android:windowBackground.

Upvotes: 1

Related Questions