Skip to content

A simple example to show Kotlin vs Java code in Android

Notifications You must be signed in to change notification settings

Bhanditz/kotlin-vs-java

 
 

Repository files navigation

kotlin-vs-java

#####A simple example to show Kotlin vs Java code in Android

See MainActivity vs MainActivityJava to compare funtionally equivalent implementations of an activity with some very basic animations. This example demonstrations how Kotlin extensions, higher-order functions, and Anko combine to provide really powerful features with concise syntax.

Provided are examples of View extensions, Animator extensions, and Context extensions. Additionally, there are examples of using single-line functions and higher-order functions.

Here's the content of the Kotlin MainActivity for convenience:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.backgroundCircle(R.color.colorAccent)

        runDelayed(2000) { button.alphaAnimator(0f, 1f, 500).start() }
        atleastLollipop { button.onClick { reveal() } }
    }

    fun reveal() {
        overlay.circularRevealAnimator(centerX(), centerY(), 300)
            .animationEnd { button.onClick { hide() } }
            .start()
    }

    fun hide() {
        overlay.circularHideAnimator(centerX(), centerY(), 300)
            .animationEnd { button.onClick { reveal() } }
            .start()
    }

    fun centerX() = overlay.width / 2

    fun centerY() = overlay.height / 2
}

About

A simple example to show Kotlin vs Java code in Android

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 56.6%
  • Java 43.4%