Ahmad ElMadi
Ahmad ElMadi

Reputation: 2617

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 google play , and to have a splash screen you need your own style/theme. So how to do this ?

Upvotes: 3

Views: 4009

Answers (2)

random
random

Reputation: 10309

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

public class OnboardingFragment extends android.support.v17.leanback.app.OnboardingFragment {
    private static final long SPLASH_DURATION_MS = 2000;

    @Nullable
    @Override
    public View onCreateView(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 = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                onFinishFragment();
            }
        };
        handler.postDelayed(runnable, SPLASH_DURATION_MS);
        return view;
    }

    @Override
    protected void onFinishFragment() {
        super.onFinishFragment();
        // Our onboarding is done
        // Let's go back to the MainActivity
        getActivity().finish();
    }

    @Override
    protected int getPageCount() {
        return 1;
    }

    @Override
    protected String getPageTitle(int pageIndex) {
        return null;
    }

    @Override
    protected String getPageDescription(int pageIndex) {
        return null;
    }

    @Nullable
    @Override
    protected View onCreateBackgroundView(LayoutInflater inflater, ViewGroup container) {
        return null;
    }

    @Nullable
    @Override
    protected View onCreateContentView(LayoutInflater inflater, ViewGroup container) {
        return null;
    }
}

OnboardingActivity

/*
 * OnboardingActivity for OnboardingFragment
 */
public class OnboardingActivity extends Activity {

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(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

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

Start OnboardingActivity from MainActivity's onCreate

Upvotes: 3

Prathviraj Patil
Prathviraj Patil

Reputation: 9

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/

Upvotes: 0

Related Questions