Converting your iOS App to Android Using Kotlin

In this tutorial, you’ll see first-hand how similar these languages are and how simple it is to convert Swift to Kotlin by porting an iOS app to Android. By Lisa Luo.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Implementing the BearActivity

Open app ▸ java ▸ com.raywenderlich.pokethebear ▸ BearActivity.kt — your soon-to-be Kotlin version of BearViewController.swift. You'll start modifying this Activity by implementing the helper functions, bearAttack and reset. You’ll see in the Swift file that bearAttack is responsible for setting the UI state, hiding the Poke button for five seconds, and then resetting the screen:

private func bearAttack() {
  self.bearImageView.image = imageLiteral(resourceName: "bear5")
  self.view.backgroundColor = .red
  self.pokeButton.isHidden = true
  DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(5), 
    execute: { self.reset() })
}

Copy the function from the iOS project, paste it into the body of the bearAttack function in the Android project, and then make some minor syntax changes. Make the remaining changes so the body of the Kotlin bearAttack function looks like this:

private fun bearAttack() {
  this.bear_image_view.setImageDrawable(getDrawable(R.drawable.bear5))
  this.bear_container.setBackgroundColor(getColor(R.color.red))
  this.poke_button.visibility = View.INVISIBLE
  this.bear_container.postDelayed({ reset() }, TimeUnit.SECONDS.toMillis(5))
}

The changes you made include:

  1. Call the setImageDrawable function to set the image resource for the bear_image_view to the bear5.png drawable resource, which is already included in the project in the app ▸ res ▸ drawable folder.
  2. Then call the setBackgroundColor function to set the bear_container view’s background to your predefined color resource, R.color.red.
  3. Change the isHidden property to be visibility instead to toggle your button’s visibility to View.INVISIBLE.
  4. Perhaps the least intuitive change to the code you made is re-writing the DispatchQueue action, but have no fear! Android’s sister to asyncAfter is a simple postDelayed action that you set on the view bear_container.

Getting close! There is another function to translate from Swift to Kotlin. Repeat the conversion process by copying the body of the Swift reset function and pasting it into the Android project's BearActivity class reset:

self.tapCount = 0
self.bearImageView.image = imageLiteral(resourceName: "bear1")
self.view.backgroundColor = .white
self.pokeButton.isHidden = false

Then make similar changes resulting in:

this.tapCount = 0
this.bear_image_view.setImageDrawable(getDrawable(R.drawable.bear1))
this.bear_container.setBackgroundColor(getColor(R.color.white))
this.poke_button.visibility = View.VISIBLE

The last step to is to copy the body of the pokeButtonTapped function from the iOS project and paste it into the Android project. This if/else statement will require very little Android-specific changes, thanks to the similarities between Swift and Kotlin. Make sure the body of your Kotlin pokeButtonClicked looks like this:

this.tapCount = this.tapCount + 1
if (this.tapCount == 3) {
  this.bear_image_view.setImageDrawable(getDrawable(R.drawable.bear2))
} else if (this.tapCount == 7) {
  this.bear_image_view.setImageDrawable(getDrawable(R.drawable.bear3))
} else if (this.tapCount == 12) {
  this.bear_image_view.setImageDrawable(getDrawable(R.drawable.bear4))
} else if (this.tapCount == 20) {
  this.bearAttack()
}

Bonus: This if/else ladder can easily be replaced with more expressive control-flow statements, like a switch, or when in Kotlin.

Try it out if you'd like to simplify the logic.

Now all the functions have been ported from the iOS app and translated from Swift to Kotlin. Build and Run the app on a device or in the emulator to give it a go and you can now visit your furry friend behind the Login screen.

Android screen with bear and poke button

Congratulations — you have translated an iOS app into a brand new Android app by converting Swift to Kotlin. You’ve crossed platforms by moving Swift code from an iOS project in Xcode into an Android app in Android Studio, changing Swift to Kotlin! Not many people and developers can say that and that’s pretty neat. :]

Where to Go From Here

Download the fully finished projects using the Download materials button at the top or bottom of this tutorial to see how it went.

If you’re a Swift developer or new to Kotlin in Android, check out the official Kotlin docs to go more in depth with the languages. You already know how to run the Kotlin playground to try out snippets, and there are runnable code widgets written right into the docs. If you are already a Kotlin developer, try writing an app in Swift.

If you enjoyed the side-by-side comparisons of Swift and Kotlin, check out more of them in this article. Your faithful author also gave a quick talk about Swift and Kotlin with an iOS colleague at UIConf, which you can watch here. :]

We hope that you have enjoyed this tutorial on how to convert your iOS app written in Swift to Kotlin creating a whole new Android app. We also hope you continue to explore both languages and both platforms. If you have any questions or comments, please join us the forum discussion below!