Skip to content Skip to sidebar Skip to footer

Can't Run Showcaseview From Fragment In Android

I use the last version of ShowcaseView from here When I tried to use it with demo app and activity it works but when I tried to use it with fragments it crashed my app with no logc

Solution 1:

In my opinion the best solution is to place the code within the "post" callback of the view, this way it will be executed when everything is rendered.

@OverridepublicvoidonViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.post(newRunnable() {
        @Overridepublicvoidrun() {
            // your showcase initialisation code hereshowcase();
        }
    });
}

I tested it and worked like a charm.

Solution 2:

This appears to be a timing issue. If the call happens before the views are actually drawn on screen, it fails saying the width and height should be > 0. This is clear from the source code of ShowcaseView, in updateBitmap() function, where it fails:

bitmapBuffer = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);

where getMeasuredWidth() and getMeasuredHeight() will be zero before actually drawing.

The work around to this issue is to call this with a good delay. I currently use it with a 5s delay and call it from onResume(), with the help of a Handler

newHandler().postDelayed(newRunnable() {
    @Overridepublicvoidrun() {
        newShowcaseView.Builder(mActivity)
                            .setTarget(newViewTarget(saveMatchButton))
                            .setContentTitle("This is a demo")
                            .build();
    }
}, 5000);

Solution 3:

I think a better solution is to change the ShowcaseView class (Function updateBitmap()). Passing any non-zero dimension values to the createBitmap() function when getMeasure* returns 0 would ensure the bitmap is created with a valid size. Since updateBitmap() function will be called again as soon as the view gets is actual size, this poses no problem.

privatevoidupdateBitmap() 
{
    if (bitmapBuffer == null || haveBoundsChanged()) 
    {
        if(bitmapBuffer != null)
            bitmapBuffer.recycle();

        bitmapBuffer = Bitmap.createBitmap(getMeasuredWidth()  > 0 ? getMeasuredWidth()  : 1, 
                                           getMeasuredHeight() > 0 ? getMeasuredHeight() : 1, 
                                           Bitmap.Config.ARGB_8888);
    }
}

Solution 4:

According to this answer, the ShowcaseView should be built in onCreateView().

Solution 5:

Even you can write it down in RESUME() and make sure if you are writing in resume then you must have to maintain a flag which will be used to avoid issue of building more then one showcase one above on whenever OnResume() will be called.

if (!mShowCaseVisible){
ActionViewTargettarget=newActionViewTarget(getActivity(), ActionViewTarget.Type.HOME);
showcaseView = newShowcaseView.Builder(getActivity(), true)
.setTarget(target)
.setContentTitle("Home Screen")
.setOnClickListener(this)
.setContentText("Click on top left corner to seemenu.")
.doNotBlockTouches()`enter code here`
.hideOnTouchOutside()
.singleShot(42)
.build();
showcaseView.setButtonText("Next");
mShowCaseVisible = true;
}

Post a Comment for "Can't Run Showcaseview From Fragment In Android"