How to add a splash screen to my android tv app ?

倖福魔咒の 提交于 2019-12-23 21:26:25

问题


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 ?


回答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

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




回答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/



来源:https://stackoverflow.com/questions/39301584/how-to-add-a-splash-screen-to-my-android-tv-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!