OnboardingFragment
abstract classOnboardingFragment: Fragment
kotlin.Any | ||
↳ | android.app.Fragment | |
↳ | androidx.leanback.app.OnboardingFragment |
An OnboardingFragment provides a common and simple way to build onboarding screen for applications.
Building the screen
The view structure of onboarding screen is composed of the common parts and custom parts. The common parts are composed of icon, title, description and page navigator and the custom parts are composed of background, contents and foreground.To build the screen views, the inherited class should override:
onCreateBackgroundView
to provide the background view. Background view has the same size as the screen and the lowest z-order.onCreateContentView
to provide the contents view. The content view is located in the content area at the center of the screen.onCreateForegroundView
to provide the foreground view. Foreground view has the same size as the screen and the highest z-order
Each of these methods can return null
if the application doesn't want to provide it.
Page information
The onboarding screen may have several pages which explain the functionality of the application. The inherited class should provide the page information by overriding the methods:
getPageCount
to provide the number of pages.getPageTitle
to provide the title of the page.getPageDescription
to provide the description of the page.
Note that the information is used in onCreateView
, so should be initialized before calling super.onCreateView
.
Animation
Onboarding screen has three kinds of animations:
Logo Splash Animation
When onboarding screen appears, the logo splash animation is played by default. The animation fades in the logo image, pauses in a few seconds and fades it out.In most cases, the logo animation needs to be customized because the logo images of applications are different from each other, or some applications may want to show their own animations.
The logo animation can be customized in two ways:
- The simplest way is to provide the logo image by calling
setLogoResourceId
to show the default logo animation. This method should be called inFragment#onCreateView
. - If the logo animation is complex, then override
onCreateLogoAnimation
and return theAnimator
object to run.
If the inherited class provides neither the logo image nor the animation, the logo animation will be omitted.
Page enter animation
After logo animation finishes, page enter animation starts, which causes the header section - title and description views to fade and slide in. Users can override the default fade + slide animation by overridingonCreateTitleAnimator()
& onCreateDescriptionAnimator()
. By default we don't animate the custom views but users can provide animation by overriding onCreateEnterAnimation
.
Page change animation
When the page changes, the default animations of the title and description are played. The inherited class can overrideonPageChanged
to start the custom animations.
Finishing the screen
If the user finishes the onboarding screen after navigating all the pages, onFinishFragment
is called. The inherited class can override this method to show another fragment or activity, or just remove this fragment.
Theming
OnboardingFragment must have access to an appropriate theme. Specifically, the fragment must receive R.style#Theme_Leanback_Onboarding, or a theme whose parent is set to that theme. Themes can be provided in one of three ways:
- The simplest way is to set the theme for the host Activity to the Onboarding theme or a theme that derives from it.
- If the Activity already has a theme and setting its parent theme is inconvenient, the existing Activity theme can have an entry added for the attribute R.styleable#LeanbackOnboardingTheme_onboardingTheme. If present, this theme will be used by OnboardingFragment as an overlay to the Activity's theme.
- Finally, custom subclasses of OnboardingFragment may provide a theme through the
onProvideTheme
method. This can be useful if a subclass is used across multiple Activities.
If the theme is provided in multiple ways, the onProvideTheme override has priority, followed by the Activity's theme. (Themes whose parent theme is already set to the onboarding theme do not need to set the onboardingTheme attribute; if set, it will be ignored.) R.attr#onboardingTheme R.attr#onboardingHeaderStyle R.attr#onboardingTitleStyle R.attr#onboardingDescriptionStyle R.attr#onboardingNavigatorContainerStyle R.attr#onboardingPageIndicatorStyle R.attr#onboardingStartButtonStyle R.attr#onboardingLogoStyle
Summary
Public constructors |
|
---|---|
<init>() An OnboardingFragment provides a common and simple way to build onboarding screen for applications. |
Public methods |
|
---|---|
Int |
Returns the background color of the arrow if it's set through |
Int |
Returns the color of the arrow if it's set through |
Int |
Returns the text color of DescriptionView if it's set through |
Int |
Returns the background color of the dot if it's set through |
Int |
Returns the resource id of the main icon. |
Int |
Returns the resource ID of the splash logo image. |
CharSequence! |
Returns the start button text if it's set through |
Int |
Returns the text color of TitleView if it's set through |
open View? |
onCreateView(inflater: LayoutInflater!, container: ViewGroup!, savedInstanceState: Bundle!) |
open Int |
Returns the theme used for styling the fragment. |
open Unit |
onSaveInstanceState(outState: Bundle!) |
open Unit |
onViewCreated(@NonNull view: View, @Nullable savedInstanceState: Bundle?) |
open Unit |
setArrowBackgroundColor(color: Int) Sets the background color of the arrow. |
open Unit |
setArrowColor(color: Int) Sets the color of the arrow. |
open Unit |
setDescriptionViewTextColor(color: Int) Sets the text color for DescriptionView. |
open Unit |
setDotBackgroundColor(color: Int) Sets the background color of the dots. |
Unit |
setIconResouceId(resourceId: Int) Sets the resource id for the main icon. |
Unit |
setLogoResourceId(id: Int) Sets the resource ID of the splash logo image. |
open Unit |
setStartButtonText(text: CharSequence!) Sets the text on the start button text. |
open Unit |
setTitleViewTextColor(color: Int) Sets the text color for TitleView. |
Protected methods |
|
---|---|
Int |
Returns the index of the current page. |
abstract Int |
Returns the page count. |
abstract CharSequence! |
getPageDescription(pageIndex: Int) Returns the description of the given page. |
abstract CharSequence! |
getPageTitle(pageIndex: Int) Returns the title of the given page. |
Boolean |
Returns whether the logo enter animation is finished. |
open Unit |
Navigates to the next page. |
open Unit |
Navigates to the previous page. |
abstract View? |
onCreateBackgroundView(inflater: LayoutInflater!, container: ViewGroup!) Called to have the inherited class create background view. |
abstract View? |
onCreateContentView(inflater: LayoutInflater!, container: ViewGroup!) Called to have the inherited class create content view. |
open Animator! |
Provides the entry animation for description view. |
open Animator? |
Called to have the inherited class create its enter animation. |
abstract View? |
onCreateForegroundView(inflater: LayoutInflater!, container: ViewGroup!) Called to have the inherited class create foreground view. |
open Animator? |
Called to have the inherited class create its own logo animation. |
open Animator! |
Provides the entry animation for title view. |
open Unit |
Called when the onboarding flow finishes. |
open Unit |
Called immediately after the logo animation is complete or no logo animation is specified. |
open Unit |
onPageChanged(newPage: Int, previousPage: Int) Called when the page has been changed. |
Unit |
startEnterAnimation(force: Boolean) Called to start entrance transition. |
Public constructors
<init>
OnboardingFragment()
Deprecated: use OnboardingSupportFragment
An OnboardingFragment provides a common and simple way to build onboarding screen for applications.
Building the screen
The view structure of onboarding screen is composed of the common parts and custom parts. The common parts are composed of icon, title, description and page navigator and the custom parts are composed of background, contents and foreground.To build the screen views, the inherited class should override:
onCreateBackgroundView
to provide the background view. Background view has the same size as the screen and the lowest z-order.onCreateContentView
to provide the contents view. The content view is located in the content area at the center of the screen.onCreateForegroundView
to provide the foreground view. Foreground view has the same size as the screen and the highest z-order
Each of these methods can return null
if the application doesn't want to provide it.
Page information
The onboarding screen may have several pages which explain the functionality of the application. The inherited class should provide the page information by overriding the methods:
getPageCount
to provide the number of pages.getPageTitle
to provide the title of the page.getPageDescription
to provide the description of the page.
Note that the information is used in onCreateView
, so should be initialized before calling super.onCreateView
.
Animation
Onboarding screen has three kinds of animations:
Logo Splash Animation
When onboarding screen appears, the logo splash animation is played by default. The animation fades in the logo image, pauses in a few seconds and fades it out.In most cases, the logo animation needs to be customized because the logo images of applications are different from each other, or some applications may want to show their own animations.
The logo animation can be customized in two ways:
- The simplest way is to provide the logo image by calling
setLogoResourceId
to show the default logo animation. This method should be called inFragment#onCreateView
. - If the logo animation is complex, then override
onCreateLogoAnimation
and return theAnimator
object to run.
If the inherited class provides neither the logo image nor the animation, the logo animation will be omitted.
Page enter animation
After logo animation finishes, page enter animation starts, which causes the header section - title and description views to fade and slide in. Users can override the default fade + slide animation by overridingonCreateTitleAnimator()
& onCreateDescriptionAnimator()
. By default we don't animate the custom views but users can provide animation by overriding onCreateEnterAnimation
.
Page change animation
When the page changes, the default animations of the title and description are played. The inherited class can overrideonPageChanged
to start the custom animations.
Finishing the screen
If the user finishes the onboarding screen after navigating all the pages, onFinishFragment
is called. The inherited class can override this method to show another fragment or activity, or just remove this fragment.
Theming
OnboardingFragment must have access to an appropriate theme. Specifically, the fragment must receive R.style#Theme_Leanback_Onboarding, or a theme whose parent is set to that theme. Themes can be provided in one of three ways:
- The simplest way is to set the theme for the host Activity to the Onboarding theme or a theme that derives from it.
- If the Activity already has a theme and setting its parent theme is inconvenient, the existing Activity theme can have an entry added for the attribute R.styleable#LeanbackOnboardingTheme_onboardingTheme. If present, this theme will be used by OnboardingFragment as an overlay to the Activity's theme.
- Finally, custom subclasses of OnboardingFragment may provide a theme through the
onProvideTheme
method. This can be useful if a subclass is used across multiple Activities.
If the theme is provided in multiple ways, the onProvideTheme override has priority, followed by the Activity's theme. (Themes whose parent theme is already set to the onboarding theme do not need to set the onboardingTheme attribute; if set, it will be ignored.) R.attr#onboardingTheme R.attr#onboardingHeaderStyle R.attr#onboardingTitleStyle R.attr#onboardingDescriptionStyle R.attr#onboardingNavigatorContainerStyle R.attr#onboardingPageIndicatorStyle R.attr#onboardingStartButtonStyle R.attr#onboardingLogoStyle
Public methods
getArrowBackgroundColor
fun getArrowBackgroundColor(): Int
Returns the background color of the arrow if it's set through setArrowBackgroundColor(int)
. If no color was set, transparent is returned.
getArrowColor
fun getArrowColor(): Int
Returns the color of the arrow if it's set through setArrowColor(int)
. If no color was set, transparent is returned.
getDescriptionViewTextColor
fun getDescriptionViewTextColor(): Int
Returns the text color of DescriptionView if it's set through setDescriptionViewTextColor(int)
. If no color was set, transparent is returned.
getDotBackgroundColor
fun getDotBackgroundColor(): Int
Returns the background color of the dot if it's set through setDotBackgroundColor(int)
. If no color was set, transparent is returned.
getLogoResourceId
fun getLogoResourceId(): Int
Returns the resource ID of the splash logo image.
Return | |
---|---|
Int: The resource ID of the splash logo image. |
getStartButtonText
fun getStartButtonText(): CharSequence!
Returns the start button text if it's set through setStartButtonText(CharSequence)
}. If no string was set, null is returned.
getTitleViewTextColor
fun getTitleViewTextColor(): Int
Returns the text color of TitleView if it's set through setTitleViewTextColor(int)
. If no color was set, transparent is returned.
onCreateView
@Nullable open fun onCreateView(inflater: LayoutInflater!, container: ViewGroup!, savedInstanceState: Bundle!): View?
onProvideTheme
open fun onProvideTheme(): Int
Returns the theme used for styling the fragment. The default returns -1, indicating that the host Activity's theme should be used.
Return | |
---|---|
Int: The theme resource ID of the theme to use in this fragment, or -1 to use the host Activity's theme. |
onViewCreated
open fun onViewCreated(@NonNull view: View, @Nullable savedInstanceState: Bundle?): Unit
setArrowBackgroundColor
open fun setArrowBackgroundColor(color: Int): Unit
Sets the background color of the arrow. If not set, the default color from attr R.styleable#PagingIndicator_arrowBgColor in the theme will be used.
Parameters | |
---|---|
color |
Int: the color to use for arrow background |
setArrowColor
open fun setArrowColor(color: Int): Unit
Sets the color of the arrow. This color will supersede the color set in the theme attribute R.styleable#PagingIndicator_arrowColor if provided. If none of these two are set, the arrow will have its original bitmap color.
Parameters | |
---|---|
color |
Int: the color to use for arrow background |
setDescriptionViewTextColor
open fun setDescriptionViewTextColor(color: Int): Unit
Sets the text color for DescriptionView. If not set, the default textColor set in style referenced by attr R.attr#onboardingDescriptionStyle will be used.
Parameters | |
---|---|
color |
Int: the color to use as the text color for DescriptionView |
setDotBackgroundColor
open fun setDotBackgroundColor(color: Int): Unit
Sets the background color of the dots. If not set, the default color from attr R.styleable#PagingIndicator_dotBgColor in the theme will be used.
Parameters | |
---|---|
color |
Int: the color to use for dot backgrounds |
setIconResouceId
fun setIconResouceId(resourceId: Int): Unit
Sets the resource id for the main icon.
setLogoResourceId
fun setLogoResourceId(id: Int): Unit
Sets the resource ID of the splash logo image. If the logo resource id set, the default logo splash animation will be played.
Parameters | |
---|---|
id |
Int: The resource ID of the logo image. |
setStartButtonText
open fun setStartButtonText(text: CharSequence!): Unit
Sets the text on the start button text. If not set, the default text set in R.styleable#LeanbackOnboardingTheme_onboardingStartButtonStyle will be used.
Parameters | |
---|---|
text |
CharSequence!: the start button text |
setTitleViewTextColor
open fun setTitleViewTextColor(color: Int): Unit
Sets the text color for TitleView. If not set, the default textColor set in style referenced by attr R.attr#onboardingTitleStyle will be used.
Parameters | |
---|---|
color |
Int: the color to use as the text color for TitleView |
Protected methods
getCurrentPageIndex
protected fun getCurrentPageIndex(): Int
Returns the index of the current page.
Return | |
---|---|
Int: The index of the current page. |
getPageCount
protected abstract fun getPageCount(): Int
Returns the page count.
Return | |
---|---|
Int: The page count. |
getPageDescription
protected abstract fun getPageDescription(pageIndex: Int): CharSequence!
Returns the description of the given page.
Parameters | |
---|---|
pageIndex |
Int: The page index. |
Return | |
---|---|
CharSequence!: The description of the page. |
getPageTitle
protected abstract fun getPageTitle(pageIndex: Int): CharSequence!
Returns the title of the given page.
Parameters | |
---|---|
pageIndex |
Int: The page index. |
Return | |
---|---|
CharSequence!: The title of the page. |
isLogoAnimationFinished
protected fun isLogoAnimationFinished(): Boolean
Returns whether the logo enter animation is finished.
Return | |
---|---|
Boolean: true if the logo enter transition is finished, false otherwise |
onCreateBackgroundView
@Nullable protected abstract fun onCreateBackgroundView(inflater: LayoutInflater!, container: ViewGroup!): View?
Called to have the inherited class create background view. This is optional and the fragment which doesn't have the background view can return null
. This is called inside onCreateView
.
Parameters | |
---|---|
inflater |
LayoutInflater!: The LayoutInflater object that can be used to inflate the views, |
container |
LayoutInflater!: The parent view that the additional views are attached to.The fragment should not add the view by itself. |
Return | |
---|---|
View?: The background view for the onboarding screen, or null . |
onCreateContentView
@Nullable protected abstract fun onCreateContentView(inflater: LayoutInflater!, container: ViewGroup!): View?
Called to have the inherited class create content view. This is optional and the fragment which doesn't have the content view can return null
. This is called inside onCreateView
.
The content view would be located at the center of the screen.
Parameters | |
---|---|
inflater |
LayoutInflater!: The LayoutInflater object that can be used to inflate the views, |
container |
LayoutInflater!: The parent view that the additional views are attached to.The fragment should not add the view by itself. |
Return | |
---|---|
View?: The content view for the onboarding screen, or null . |
onCreateDescriptionAnimator
protected open fun onCreateDescriptionAnimator(): Animator!
Provides the entry animation for description view. This allows users to override the default fade and slide animation. Returning null will disable the animation.
onCreateEnterAnimation
@Nullable protected open fun onCreateEnterAnimation(): Animator?
Called to have the inherited class create its enter animation. The start animation runs after logo animation ends.
Return | |
---|---|
Animator?: The Animator object which runs the page enter animation. |
onCreateForegroundView
@Nullable protected abstract fun onCreateForegroundView(inflater: LayoutInflater!, container: ViewGroup!): View?
Called to have the inherited class create foreground view. This is optional and the fragment which doesn't need the foreground view can return null
. This is called inside onCreateView
.
This foreground view would have the highest z-order.
Parameters | |
---|---|
inflater |
LayoutInflater!: The LayoutInflater object that can be used to inflate the views, |
container |
LayoutInflater!: The parent view that the additional views are attached to.The fragment should not add the view by itself. |
Return | |
---|---|
View?: The foreground view for the onboarding screen, or null . |
onCreateLogoAnimation
@Nullable protected open fun onCreateLogoAnimation(): Animator?
Called to have the inherited class create its own logo animation.
This is called only if the logo image resource ID is not set by setLogoResourceId
. If this returns null
, the logo animation is skipped.
Return | |
---|---|
Animator?: The Animator object which runs the logo animation. |
onCreateTitleAnimator
protected open fun onCreateTitleAnimator(): Animator!
Provides the entry animation for title view. This allows users to override the default fade and slide animation. Returning null will disable the animation.
onFinishFragment
protected open fun onFinishFragment(): Unit
Called when the onboarding flow finishes.
onLogoAnimationFinished
protected open fun onLogoAnimationFinished(): Unit
Called immediately after the logo animation is complete or no logo animation is specified. This method can also be called when the activity is recreated, i.e. when no logo animation are performed. By default, this method will hide the logo view and start the entrance animation for this fragment. Overriding subclasses can provide their own data loading logic as to when the entrance animation should be executed.
onPageChanged
protected open fun onPageChanged(newPage: Int, previousPage: Int): Unit
Called when the page has been changed.
Parameters | |
---|---|
newPage |
Int: The new page. |
previousPage |
Int: The previous page. |
startEnterAnimation
protected fun startEnterAnimation(force: Boolean): Unit
Called to start entrance transition. This can be called by subclasses when the logo animation and data loading is complete. If force flag is set to false, it will only start the animation if it's not already done yet. Otherwise, it will always start the enter animation. In both cases, the logo view will hide and the rest of fragment views become visible after this call.
Parameters | |
---|---|
force |
Boolean: true if enter animation has to be performed regardless of whether it's been done in the past, false otherwise |