android convert java to kotlin, android, kotlin tutorial, How to convert java files to kotlin files in android studio

How to convert java files to kotlin files in android studio

Java has been the preferred language that has been used by android to develop android applications, but of late, kotlin has been introduced and adopted as one of the languages used to develop apps. Kotlin has had a major milestone since it is considered as the future language, therefore, giving java a major drawback

As we all know from our previous discussions, we have highlighted that java is still supported by the android studio but some features and libraries that are upcoming are being developed to fit the kotlin architecture.

There is a need to start familiarizing yourself well with the kotlin language if you want to continue developing android applications.

One thing that is an advantageous feature in android studio, you can convert java files to kotlin files easily and this is what will form our basis of discussion in this article.

We all know that writing a full piece of code is not as direct as it seems and having to write the same code with kotlin without losing meaning can also be tiresome and that’s why if you are using an updated version of android studio, you can convert your java files to kotlin files at a go.

One thing that android studio warns you while you are converting your java files to kotlin files is that the conversion is not 100% perfect and you will need to do some corrections to remove the errors

How to convert java files to kotlin files

  • First, you need to open android studio and open the project you wish to convert
  • Select the java file you want to convert to kotlin file

Please note that the design files/ XML files are not converted since the design is not affected whether it is java or kotlin

For this demonstration, the java code file used is a login code which is named MainActivity.java is as shown below

package com.example.solutionspaceapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;

import android.os.Bundle;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.os.Bundle;

import android.view.View;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

 

public class MainActivity extends AppCompatActivity {

    EditText username,password;

    Button login;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        username=(EditText)findViewById(R.id.username);

        password=(EditText)findViewById(R.id.password);

        login=(Button) findViewById(R.id.login);

 

        login.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                if(username.getText().toString().equals("")){

                    Toast.makeText(getApplicationContext(), "Username cannot be blank", Toast.LENGTH_SHORT).show();

                }else if(password.getText().toString().equals("")){

                    Toast.makeText(getApplicationContext(), "password cannot be blank", Toast.LENGTH_SHORT).show();

                }else{

                    Toast.makeText(getApplicationContext(), "login method to proceed", Toast.LENGTH_SHORT).show();

                }

            }

        });

 

    }

    }

  • Once the java file is selected, in the navigation bar at the top, click code and a pop up will appear
  • Scroll down to the last item named convert java file to kotlin file and click it

How to convert java files to kotlin files in android studio

  • An alert dialog will appear giving you the alerting you that you will need to configure kotlin

How to convert java files to kotlin files in android studio

  • Click okay to proceed and if you only need to configure a certain project select a single module but if you want to configure all modules select all. You can click select all
  • The Gradle files will start importing the necessary files required by kotlin. Wait for Gradle to complete

Note that this step will not be available if you have already configured kotlin

  • Once you have configured kotlin, go back and select the java file you are converting again
  • Click on code and select convert to kotlin
  • A modal will appear advising you that you will need to make some corrections since all the codes you have written may not be interpreted properly during conversion

How to convert java files to kotlin files in android studio

  • Click ok to proceed

Once done you will notice that the name of the file has changed its extension to .kt meaning it is a kotlin file and the code also will have changed also, see the code below and compare with the one above

package com.example.solutionspaceapp

import android.os.Bundle

import android.view.View

import android.widget.Button

import android.widget.EditText

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

 

class MainActivity : AppCompatActivity() {

    var username: EditText? = null

    var password: EditText? = null

    var login: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        username = findViewById<View>(R.id.username) as EditText

        password = findViewById<View>(R.id.password) as EditText

        login = findViewById<View>(R.id.login) as Button

        login!!.setOnClickListener {

            if (username!!.text.toString() == "") {

                Toast.makeText(applicationContext, "Username cannot be blank", Toast.LENGTH_SHORT).show()

            } else if (password!!.text.toString() == "") {

                Toast.makeText(applicationContext, "password cannot be blank", Toast.LENGTH_SHORT).show()

            } else {

                Toast.makeText(applicationContext, "login method to proceed", Toast.LENGTH_SHORT).show()

            }

        }

    }

}

Make corrections if need be.

That’s it for converting java file to kotlin file. Hope you have followed well. Happy coding