Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showcase View text gravity

Is there any way to set the position of the Title and Message in a Showcase view? I see the library uses a function to find the best position but I want to override it.

like image 359
arayray Avatar asked Oct 05 '13 11:10

arayray


1 Answers

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:

    @Override
public 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!

like image 196
Sadegh Avatar answered Oct 08 '22 02:10

Sadegh