Showcase View text gravity Showcase View text gravity android android

Showcase View text gravity


I just digged into the ShowcaseView source code and it seems there is no way built in to set the text position. So I added my own modification to the library. Here is the changes that I have made:

In class ShowcaseView added these fields:

private boolean hasManualPostion = false;private int xPosition, yPosition, width;

Then in the Builder class which is inner class in ShowcaseView class, add these methods:

        public Builder hasManualPosition(boolean hasManualPosition) {        showcaseView.hasManualPostion = hasManualPosition;        return this;    }    public Builder xPostion(int xPostion) {        showcaseView.xPosition = xPostion;        return this;    }    public Builder yPostion(int yPostion) {        showcaseView.yPosition = yPostion;        return this;    }

After that, add this method into the TextDrawer class:

public void setTestPostionManually(int xPosition, int yPosition) {    mBestTextPosition[0] = xPosition;    mBestTextPosition[1] = yPosition;}

and at the end change onPreDraw method in ShowcaseView to look like this:

    @Overridepublic boolean onPreDraw() {    boolean recalculatedCling = showcaseAreaCalculator.calculateShowcaseRect(showcaseX, showcaseY, showcaseDrawer);    boolean recalculateText = recalculatedCling || hasAlteredText;    if (recalculateText) {        textDrawer.calculateTextPosition(getMeasuredWidth(), getMeasuredHeight(), this, shouldCentreText);        if (hasManualPostion) {            textDrawer.setTestPostionManually(xPosition, yPosition);        }    }    hasAlteredText = false;    return true;}

Now you should create ShowcaseView like this:

sv = new ShowcaseView.Builder(this, true)                .setTarget(target)                .setContentTitle(R.string.showcase_main_title)                .setContentText(R.string.showcase_main_message)                .setStyle(R.style.CustomShowcaseTheme2)                .setShowcaseEventListener(this)                .hasManualPosition(true)                .xPostion(0)                .yPostion(240)                .build();

Enjoy!