10

I found a library that fits my purpose, but it is written on Kotlin. Can i use it in Java project?

Framework: https://github.com/mpetlyuk/initial_tips

Usage on Kotlin:

// Create view for your tip
val inflater = LayoutInflater.from(Context)
val tipView = DataBindingUtil.inflate<ViewDataBinding>(inflater, R.layout.item_tooltip, null, false).getRoot()

// Create tip
val tip = Tooltip.Builder()
    .attachTooltipView(tipView)
    .withEnterAnimation(AnimationComposer(FadeInAnimator()).duration(ANIM_DURATION))
    .withExitAnimation(AnimationComposer(FadeOutAnimator()).duration(ANIM_DURATION))
    .withGravity(TipVerticalGravity.BOTTOM, TipHorizontalGravity.LEFT)
    .withAnchorView(/* anchor view */)
    .build()

// Create a queue of tips
val tooltipsQueue = LinkedBlockingQueue<Tip>(listOf(tip))

// Create a queue of tips
TipsManager.showTips(binding.root as ViewGroup, ContextCompat.getColor(this, 0 /* your resource color for dimming */)) { tooltipsQueue }
Vergiliy
  • 1,248
  • 1
  • 13
  • 22

2 Answers2

4

You first need to add support Kotlin in your app. Just create temp kotlin file, android studio will guide you how to do it.

TextView tipView = (TextView) LayoutInflater.from(this).inflate(R.layout.item_tooltip, null, false).getRootView();
Tooltip tip = new Tooltip.Builder()
        .attachTooltipView(tipView)
        .withEnterAnimation(new AnimationComposer<BaseViewAnimator>(new FadeInAnimator()).duration(500))
        .withExitAnimation(new AnimationComposer<BaseViewAnimator>(new FadeOutAnimator()).duration(500))
        .withGravity(TipVerticalGravity.BOTTOM, TipHorizontalGravity.LEFT)
        .withAnchorView(loginButton)
        .build();

List<Tip> tips = new ArrayList<>();
tips.add(tip);
final LinkedBlockingQueue<Tip> tooltipsQueue = new LinkedBlockingQueue<>(tips);
TipsManager.showTips(rootView, R.color.colorAccent, new Function0<Queue<Tip>>() {
    @Override
    public Queue<Tip> invoke() {
        return tooltipsQueue;
    }
});
Tuan Luong
  • 3,902
  • 1
  • 16
  • 18
  • 1
    You don't need to add Kotlin support or create Kotlin files, no. – Alexey Romanov Dec 13 '19 at 07:40
  • @AlexeyRomanov in the lib, `showTips` use `Function0` that is inside `kotlin.jvm.functions.Function0`. So for this to work, project must be support kotlin. Please correct if I am wrong – Tuan Luong Dec 13 '19 at 07:47
  • 1
    No, it just needs `kotlin-stdlib` as a dependency to use it from Java. But it will be picked up automatically because it's a dependency of the library Vergiliy actually wants to use. – Alexey Romanov Dec 13 '19 at 07:53
  • Thanks. Everything works perfectly. I still had to add Kotlin support, as without it, there was no `Function0` function. – Vergiliy Dec 13 '19 at 14:29
1

You add the library as a dependency just as if it was Java. Kotlin is designed so it can be used from Java as easily as possible, even if you end up with less pretty code (see interoperation documentation). To translate specific examples, see How to convert a kotlin source file to a java source file.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 1
    I would add to this point that a library itself must also be written with Java usage in mind, there are at least several ways in which to make it inaccessible from Java. – Marko Topolnik Dec 13 '19 at 08:45