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.
Asked
Active
Viewed 3,669 times
15
-
1Please elaborate what are you trying to say, show your code and then explain the issue you are getting. – Chintan Soni Oct 05 '13 at 11:41
-
updated op. Still haven't found a way to do this. – arayray Nov 18 '13 at 03:22
-
Submitted issue on Showcase View github. https://github.com/Espiandev/ShowcaseView/issues/113 – arayray Nov 20 '13 at 19:10
1 Answers
8
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!

Sadegh
- 2,669
- 1
- 23
- 26
-
-
1
-
-
2you cannot pass this to onPreDraw...how do you write something like that without testing it and just give it to the asker – Odaym Dec 29 '14 at 09:18
-
1@Odaym maybe when he wrote the code there was no `CalculateTextOnPreDraw` class (which now contains `onPreDraw` method). Now, you just have to replace `this` with `ShowcaseView.this` and it will work perfectly. – Ashish Tanna Mar 19 '15 at 22:38
-
that was great! that helped but how to center text in screen i dont want to provide coordinates by hard code – Adnan Apr 21 '16 at 13:21