amlcurran / ShowcaseView

[Archived] Highlight the best bits of your app to users quickly, simply, and cool...ly
5.6k stars 1.29k forks source link

I would share with you a new way to use showcaseview dynamically with recursive method #420

Open AmineZAMANI opened 7 years ago

AmineZAMANI commented 7 years ago

1) Create your ViewTargets, for example 4 targets (you can create n targets) and put targets into an array declared as a private attribute in your Activity class : private ViewTarget[] targets;

            final ViewTarget target1 = new ViewTarget(textview1);
        final ViewTarget target2 = new ViewTarget(textview2);
        final ViewTarget target3 = new ViewTarget(textview3);
        final ViewTarget target4 = new ViewTarget(textview4);
            targets = new ViewTarget[] { target1, target2,target3,target4};

2) Create the recursive method :


private void toStep(final String title, final String description,Target target, final int i) {
        final ShowcaseView.Builder showCaseBuilder = new ShowcaseView.Builder(ShowCaseActivity.this);
        showCaseBuilder.setTarget(targets[i]);
        showCaseBuilder.setContentTitle(title.replace("[i]", ""+i));
        showCaseBuilder.setContentText(description.replace("[i]", ""+i));
        showCaseBuilder.setShowcaseEventListener(new OnShowcaseEventListener() {
            @Override
            public void onShowcaseViewTouchBlocked(MotionEvent motionEvent) {
            }

            @Override
            public void onShowcaseViewShow(ShowcaseView showcaseView) {
            }

            @Override
            public void onShowcaseViewHide(ShowcaseView showcaseView) {
                try {
                    toStep(title, description, targets[i+1], i+1);
                } catch(Exception ex) {

                }
            }
            @Override
            public void onShowcaseViewDidHide(ShowcaseView showcaseView) {
            }
        });
        showCaseBuilder.build();
    }

3) Finally you can call the method that displays Showcases :

toStep("Title for showcase [i]", " Description for showcase [i] ",targets[0], 0);

I hope this will be helpful for you :)