TakuSemba / Spotlight

Android Library that lights items for tutorials or walk-throughs etc...
Apache License 2.0
3.63k stars 363 forks source link

Spotlight starts at corner in every phone #98

Closed XiXiongMaoXiong closed 4 years ago

XiXiongMaoXiong commented 4 years ago
 val firstRoot = LinearLayout(this)
        val first = layoutInflater.inflate(R.layout.spotlight_tour, firstRoot)
        val firstTarget = com.takusemba.spotlight.Target.Builder()
            .setAnchor(btnLoginWithEmail)
            .setEffect(RippleEffect(100f, 200f, argb(30, 124, 255, 90)))
            .setShape(RoundedRectangle(200f,400f,100f))
            .setOverlay(first)
            .build()
        val spotlight = Spotlight.Builder(this)
            .setTargets(firstTarget)
            .setBackgroundColor(R.color.spotlight_bg_color)
            .setDuration(1000L)
            .setAnimation(DecelerateInterpolator(2f))
            .build()

        btnLoginWithEmail.doOnPreDraw {
            spotlight.start()
        }
// Even tried btnLoginWithEmail.post({}) but did not help

By the way I am using Material design UI elements the button class is com.google.android.material.button.MaterialButton. Though I checked with regular Button class but it didn't work. In the corner again...

TakuSemba commented 4 years ago

It's probably because you haven't waited until the views are inflated.

Can you try this below?

https://github.com/TakuSemba/Spotlight/blob/master/README.md

If you want to show Spotlight immediately, you have to wait until views are laid out.

// with core-ktx method.
view.doOnPreDraw { Spotlight.Builder(this)...start() }
TakuSemba commented 4 years ago

I'm closing this because this will be resolved by the suggestion above.

iporat08 commented 4 years ago

Hey, i'm experiencing the same problem... BTW, I think NotNotMarshall did as instructed here.

XiXiongMaoXiong commented 4 years ago

It's probably because you haven't waited until the views are inflated.

Can you try this below?

https://github.com/TakuSemba/Spotlight/blob/master/README.md

If you want to show Spotlight immediately, you have to wait until views are laid out.

// with core-ktx method.
view.doOnPreDraw { Spotlight.Builder(this)...start() }

I am already doing this, forgot to include it in my question. But still it is in the corner... I tried both methods: onPreDraw and post{}

But no use.

iporat08 commented 4 years ago

I used the solution mentioned here: https://stackoverflow.com/questions/32400211/view-getx-and-gety-return-0-0-after-they-have-been-added-to-the-activity

just put the code building the target and spotlight inside this code (assuming view is the view which is the anchor of your spotlight):

view.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // Layout has happened here.

      // put target and spotlight code here

        // Don't forget to remove your listener when you are done with it.
        view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});