Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Kotlin library in Java project?

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 }
like image 629
Vergiliy Avatar asked Dec 13 '19 03:12

Vergiliy


People also ask

Can I use a Kotlin library in Java?

Android Studio provides full support for Kotlin, enabling you to add Kotlin files to your existing project and convert Java language code to Kotlin. You can then use all of Android Studio's existing tools with your Kotlin code, including autocomplete, lint checking, refactoring, debugging, and more.

Can I use Java and Kotlin in same project?

Adding Kotlin to an existing Java Android project is easy. Just include a few Gradle dependencies, apply a plugin, and you can start programming in the language.

Can I use Kotlin data class in Java?

As a quick refresher, Kotlin is a modern, statically typed language that compiles down for use on the JVM. It's often used wherever you'd reach for Java, including Android apps and backend servers (using Java Spring or Kotlin's own Ktor).

Can you compile Kotlin to Java?

Converting a Kotlin file to Java file involves two steps i.e. compiling the Kotlin code to the JVM bytecode and then decompile the bytecode to the Java code. Steps to convert your Kotlin source file to Java source file: Open your Kotlin project in the Android Studio.


1 Answers

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;
    }
});
like image 115
Tuan Luong Avatar answered Oct 01 '22 20:10

Tuan Luong