diff --git a/README.md b/README.md index b075de7..dd024e9 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,12 @@ Overlay background color, text color and border color are specified globally. `withBorderColor(int borderColorResourceId)` +#### Handling clicks + +It is possible to set your own click listener to the onboarding overlay by using `setOnClickListener(View.OnClickListener onClickListener)`, + +For closing the onboarding overlay on click, use `clearOnClick(boolean animate)`. + #### Onboarding elements The following onboarding element types are available. diff --git a/lib/src/main/java/se/mattec/onboardinglayout/OnboardingScreen.java b/lib/src/main/java/se/mattec/onboardinglayout/OnboardingScreen.java index 3fc553f..a84b838 100644 --- a/lib/src/main/java/se/mattec/onboardinglayout/OnboardingScreen.java +++ b/lib/src/main/java/se/mattec/onboardinglayout/OnboardingScreen.java @@ -27,10 +27,12 @@ public class OnboardingScreen private List onboardingElements; private Context context; - private int backgroundColorResourceId = -1; + private int backgroundColorResourceId = android.R.color.transparent; private int textColorResourceId = -1; private int borderColorResourceId = -1; + private View.OnClickListener onClickListener; + private View backgroundView; public OnboardingScreen(OnboardingLayout onboardingLayout) @@ -100,32 +102,56 @@ public OnboardingScreen withBorderColor(int borderColorResourceId) return this; } - public OnboardingScreen show(boolean animate) + public OnboardingScreen setOnClickListener(View.OnClickListener onClickListener) + { + this.onClickListener = onClickListener; + return this; + } + + public OnboardingScreen clearOnClick(final boolean animate) { - if (backgroundColorResourceId != -1) + setOnClickListener(new View.OnClickListener() { - List holeSpecs = new ArrayList<>(); - for (OnboardingElement element : onboardingElements) + + @Override + public void onClick(View view) { - if (element instanceof HoleOnboardingElement) - { - holeSpecs.add(((HoleOnboardingElement) element).getHoleSpec()); - } + OnboardingScreen.this.clear(animate); } - BackgroundView backgroundView = new BackgroundView(context); - backgroundView.setBackgroundColor(backgroundColorResourceId); - backgroundView.setHoleSpecs(holeSpecs); - backgroundView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); - onboardingLayout.addView(backgroundView); - this.backgroundView = backgroundView; + }); + return this; + } - if (animate) + public OnboardingScreen clearOnClick() + { + return clearOnClick(false); + } + + public OnboardingScreen show(boolean animate) + { + List holeSpecs = new ArrayList<>(); + for (OnboardingElement element : onboardingElements) + { + if (element instanceof HoleOnboardingElement) { - AnimationUtils.fadeIn(backgroundView); + holeSpecs.add(((HoleOnboardingElement) element).getHoleSpec()); } } + BackgroundView backgroundView = new BackgroundView(context); + backgroundView.setBackgroundResource(backgroundColorResourceId); + backgroundView.setHoleSpecs(holeSpecs); + backgroundView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + backgroundView.setOnClickListener(onClickListener); + onboardingLayout.addView(backgroundView); + this.backgroundView = backgroundView; + + if (animate) + { + AnimationUtils.fadeIn(backgroundView); + } + for (OnboardingElement element : onboardingElements) { View view = element.create(); diff --git a/lib/src/main/java/se/mattec/onboardinglayout/views/BackgroundView.java b/lib/src/main/java/se/mattec/onboardinglayout/views/BackgroundView.java index a4b41ce..d1a33c3 100644 --- a/lib/src/main/java/se/mattec/onboardinglayout/views/BackgroundView.java +++ b/lib/src/main/java/se/mattec/onboardinglayout/views/BackgroundView.java @@ -96,7 +96,8 @@ protected void onDraw(Canvas canvas) canvas.drawBitmap(bitmap, 0, 0, paint); } - public void setBackgroundColor(int backgroundResourceId) + @Override + public void setBackgroundResource(int backgroundResourceId) { backgroundPaint.setColor(ContextCompat.getColor(getContext(), backgroundResourceId)); } diff --git a/sample/src/main/java/se/mattec/onboardinglayout/sample/MainActivity.java b/sample/src/main/java/se/mattec/onboardinglayout/sample/MainActivity.java index 8bd1251..0637a8f 100644 --- a/sample/src/main/java/se/mattec/onboardinglayout/sample/MainActivity.java +++ b/sample/src/main/java/se/mattec/onboardinglayout/sample/MainActivity.java @@ -117,6 +117,7 @@ private void openTextOnboarding() onboardingScreen = Onboard.in(onboardingLayout) .withOverlayColor(R.color.black_trans) .withTextColor(R.color.white) + .clearOnClick(true) .withText("Above the center").above(centerView) .withText("Below the center").below(centerView) .withText("To left of the center\nTwo lines").toLeftOf(centerView) @@ -137,6 +138,7 @@ private void openArrowOnboarding() onboardingScreen = Onboard.in(onboardingLayout) .withOverlayColor(R.color.black_trans) .withTextColor(R.color.white) + .clearOnClick(true) .withTextAndArrow("Above the center", ArrowLocation.RIGHT).above(centerView) .withTextAndArrow("Below the center", ArrowLocation.LEFT).below(centerView) .withTextAndArrow("To left of the center\nTwo lines", ArrowLocation.ABOVE).toLeftOf(centerView) @@ -157,6 +159,7 @@ private void openBorderOnboarding() onboardingScreen = Onboard.in(onboardingLayout) .withOverlayColor(R.color.black_trans) .withBorderColor(R.color.white) + .clearOnClick(true) .withBorder(true).around(topLeftView) .withBorder(false).around(topRightView) .withDashedBorder(false).around(bottomRightView) @@ -168,6 +171,7 @@ private void openHoleOnboarding() { onboardingScreen = Onboard.in(onboardingLayout) .withOverlayColor(R.color.black_trans) + .clearOnClick(true) .withHole(true).around(topLeftView) .withHole(false).around(topRightView) .withHole(true).around(bottomLeftView) @@ -179,6 +183,7 @@ private void openImageOnboarding() { onboardingScreen = Onboard.in(onboardingLayout) .withOverlayColor(R.color.black_trans) + .clearOnClick(true) .withImage(R.mipmap.ic_launcher).atop(centerView) .withImage(R.mipmap.ic_launcher).above(centerView) .withImage(R.mipmap.ic_launcher).below(centerView)