Skip to content Skip to sidebar Skip to footer

How To Add A Splash Screen To My Android Tv App ?

does anyone know how to add a splash screen to an Android TV App ? what is making it hard for me is that the main activity should have Theme.Leanback in order to be accepted in the

Solution 1:

You can customise Leanback's OnboardingFragment slightly to display it as splash screen. OnboardingFragment allows you to add on-boarding steps but if you don't need them you can just set setLogoResourceId inside onCreateView.

Note that it crashes if you keep page count to zero so keep page count to one and splash duration greater than LOGO_SPLASH_PAUSE_DURATION_MS = 1333 otherwise a page with "Get Started" button will be displayed.

The idea is to use onboarding fragment with just a splash screen initially and add on-boarding steps as your application grows.

OnboardingFragment

publicclassOnboardingFragmentextendsandroid.support.v17.leanback.app.OnboardingFragment {
    privatestatic final long SPLASH_DURATION_MS = 2000;

    @Nullable@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (view != null) {
            view.setBackgroundColor(Color.RED);
        }
        setLogoResourceId(R.drawable.logo);
        Handler handler = newHandler();
        Runnable runnable = newRunnable() {
            @Overridepublicvoidrun() {
                onFinishFragment();
            }
        };
        handler.postDelayed(runnable, SPLASH_DURATION_MS);
        return view;
    }

    @OverrideprotectedvoidonFinishFragment() {
        super.onFinishFragment();
        // Our onboarding is done// Let's go back to the MainActivitygetActivity().finish();
    }

    @Overrideprotected int getPageCount() {
        return1;
    }

    @OverrideprotectedStringgetPageTitle(int pageIndex) {
        returnnull;
    }

    @OverrideprotectedStringgetPageDescription(int pageIndex) {
        returnnull;
    }

    @Nullable@OverrideprotectedViewonCreateBackgroundView(LayoutInflater inflater, ViewGroup container) {
        returnnull;
    }

    @Nullable@OverrideprotectedViewonCreateContentView(LayoutInflater inflater, ViewGroup container) {
        returnnull;
    }
}

OnboardingActivity

/*
 * OnboardingActivity for OnboardingFragment
 */publicclassOnboardingActivityextendsActivity {

    /**
     * Called when the activity is first created.
     */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.onboarding);
    }
}

onboarding.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/onboarding_fragment"
    android:name="com.example.android.tvleanback.ui.OnboardingFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Declare OnboardingActivity inside AndroidManifest

<activityandroid:name=".ui.OnboardingActivity"android:enabled="true"android:exported="true"android:theme="@style/Theme.Leanback.Onboarding" />

Start OnboardingActivity from MainActivity'sonCreate

Solution 2:

Visit following site and copy code from there and make changes accorting to your activity name,img name.also make changes on your manifest to start app with spash screen

http://www.coderefer.com/android-splash-screen-example-tutorial/

Post a Comment for "How To Add A Splash Screen To My Android Tv App ?"