Show a beautiful Intro to your users with ease.
You can download the latest sample app here.
Add this to your root build.gradle (usually in the root of your project):
repositories {
maven { url "https://jitpack.io" }
}And this to your module build.gradle (usually in the app directory):
dependencies {
implementation 'com.github.rubengees:introduction:2.0.0'
}If that doesn't work, look if there is a new version and the Readme was not updated yet.
If you want to use asynchronous image loading, introduced in the new version 1.1.0, you will need Glide or some other image loading library. If you want to use GIFs you will also need it.
Create an IntroductionBuilder like the following:
newIntroductionBuilder(this) // this is the Activity you want to start from.Then add some Slides to your Introduction:
newIntroductionBuilder(this).withSlides(generateSlides())privateList<Slide> generateSlides() {
List<Slide> result = newArrayList<>();
result.add(newSlide()
.withTitle("Some title")
.withDescription("Some description")
.withColorResource(R.color.green)
.withImage(R.drawable.myImage)
);
result.add(newSlide()
.withTitle("Another title")
.withDescription("Another description")
.withColorResource(R.color.indigo)
.withImage(R.drawable.myImage2)
);
returnresult;
}Finally introduce yourself!
newIntroductionBuilder(this).withSlides(generateSlides()).introduceMyself();That was easy right?
You can do many customizations, which will be covered by the following.
You can let the user make decisions, which you can use like settings. To do that you add an Option to your slide:
newSlide().withTitle("Feature is doing something")
.withOption(newOption("Enable the feature"))
.withColorResource(R.color.orange)
.withImage(R.drawable.image));When the user completes the intro, you will receive the selected Options in onActivityResult.
To read the result:
@OverrideprotectedvoidonActivityResult(intrequestCode, intresultCode, Intentdata) {
if (requestCode == IntroductionBuilder.INTRODUCTION_REQUEST_CODE && resultCode == RESULT_OK) {
Stringresult = "User chose: ";
for (Optionoption : data.<Option>getParcelableArrayListExtra(IntroductionActivity.OPTION_RESULT)) {
result += option.getPosition() + (option.isActivated() ? " enabled" : " disabled");
}
}
}The constant value of the request is 32142, so don't use that yourself.
It is possible that the user cancels the intro. If that happens, the resultCode is RESULT_CANCELLED and no Options are passed back.
This library supports GIFs. You need to load them asynchronously as the loading may take a while:
newIntroductionBuilder(this)
.withSlides(slides)
.withOnSlideListener(newOnSlideListener() {
@OverridepublicvoidonSlideInit(intposition, @NullableTextViewtitle, @NonNullImageViewimage,
@NullableTextViewdescription) {
if (position == 1) { // Assume we want to load the GIF at Slide 2 (index 1).Glide.with(image.getContext())
.load(R.drawable.image3)
.into(image);
}
}
}).introduceMyself();This will add the GIF, which will be automatically played when the users navigates to the Slide.
Android Marshmallow introduced Runtime Permissions, which can be requested easily with this lib. To do that, you can add a global listener like the following:
newIntroductionBuilder(this)
.withSlides(slides)
.withOnSlideListener(newOnSlideListener() {
@OverridepublicvoidonSlideChanged(intfrom, intto) {
if (from == 0 && to == 1) {
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 12);
}
}
}
}).introduceMyself();You can check if the permissions were granted like the following:
@OverridepublicvoidonRequestPermissionsResult(intrequestCode, @NonNullString[] permissions,
@NonNullint[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 12) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission was successfully granted!", Toast.LENGTH_LONG).show();
}
}
}You can use that listener for different things too, of course!
There are two available styles: Translucent and Fullscreen.
To apply one of those styles, do the following:
newIntroductionBuilder(this)
.withSlides(generateSlides())
.withStyle(newFullscreenStyle())
.introduceMyself();Translucent is the default style.
You can supply your own View to a Slide instead of just setting the title, image and description.
This is done like follows:
Create a class which implements CustomViewBuilder:
publicclassCustomViewBuilderImplimplementsSlide.CustomViewBuilder {
@NonNull@OverridepublicViewbuildView(@NonNullLayoutInflaterinflater, @NonNullViewGroupparent) {
returninflater.inflate(R.layout.layout_custom, parent, false);
}
}Then set it to your Slide:
newIntroductionBuilder(this)
.withSlides(newSlide()
.withCustomViewBuilder(newCustomViewBuilderImpl())
.withColorResource(R.color.cyan)
).introduceMyself();If you set a CustomViewBuilder to your Slide, all other values aside from the color are overridden. You have to manage all on your own.
A much more detailed explanation with all available APIs can be found in the Wiki.
Detailed Javadoc can be found here.
- This library now requires Java 8 (available in the new
Android Studio 3toolchain). - Behaviour change: Not passing a
title,descriptionoroptionhides the respective views. This way you can show fullscreen images.- The
titleanddescriptionparameters of theonSlideInitcallback are now@Nullable
- The
CustomViewBuilderis now in a different package and has been converted into aninterface.
SlideandOptionhave been moved into a different package. Just let Android Studio re-import them.- The
OnSlideListeneris now an interface on its own. RemoveIntroductionConfigbefore each and let Android Studio re-import. Furthermore@NotNullannotations have been added; You should add them to the signature.
- The
OnSlideInitmethod in theOnSlideListenernow comes without theFragment context. If you need aContext, callimage.getContext(). - There is now a class for Styles instead of an Integer. If you apply no Style, you have to do nothing, if you use one, change it to the following e.g.:
.withStyle(new FullscreenStyle()).
- The
OnSlideChangedListenerwas renamed toOnSlideListener. Just rename it and it's working again. - Asynchronous image loading is now available (and recommended!). See the Use GIFs as drawables section for more info. It applies for all types of images. GIFs won't work without asynchronous loading from now on!
A guide for contribution can be found here.
- @Akeshihiro for proper licencing and a small
Gradlerelated adjustment. - @cafedeaqua for a small code improvement
The images in the samples are taken from the following webpages (I do not own any of the images, all rights are reserved to their respective owners):
Some images and ideas are from this Repo: AppIntro by Paolo Rotolo
