Step1

Create new Java Android Studio Project

Create Android Studio Project by checking the checkBox Include Kotlin support.

To create new  Android Studio Project, you can find more details here.

Step 2

Convert Java file to Kotlin

A new android studio project, is still coded in java and contains a main java file named MainActivity.java. In this tutorial, we'll see how to convert this file to kotlin to get a MainActivity.kt kotlin file. Here is the original code of MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

To convert this file to kotlin, just click on the code menu and choose convert java file to kotlin

after converting your java file to kotlin, you get a new Kotlin MainActivity.kt file whose code is:

class MainActivity : AppCompatActivity() {

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

Note : You can also use the online tool to convert java file to Kotlin https://try.kotlinlang.org.

To convert your java code to kotlin with this tool, click on the "Convert from Java" button

Now copy and past your code in the left editor :

and then click on the "Convert to Kotlin" button

Younes Derfoufi 

Leave a Reply