Both the Kotlin and Java languages are used for Android development. Sometimes, we need to use some Java code snippet in our Kotlin code. So, let us see how to achieve that.
Creating an Android project
First of all, we are creating an Android project using Kotlin. This initially looks as shown in the picture below.
Click on "Next" and don't forget to check the last checkbox which states "include Kotlin support". Now, click "Next" and you will see the next screen as shown below. Continue clicking the "Next" button. The following screen will be shown.
Continue with "Next".
After this, we are selecting your activity. You can select any of the given activities but for the sake of simplicity, we are taking an empty activity and naming it as MainActivity.
MainActivity.kt
- package yourdomain.sampletest
-
- import android.os.Bundle
- import android.support.v7.app.AppCompatActivity
-
-
- class MainActivity : AppCompatActivity() {
-
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
-
- val manager = supportFragmentManager
- val transaction = manager.beginTransaction()
- transaction.replace(R.id.content_pane, NewFragment.getInstance())
- transaction.commit()
-
- }
-
- override fun onResume() {
- super.onResume()
- }
-
- }
Look at this code carefully; it is Kotlin. Now, we will create a fragment named "NewFragment.java". The code is given below.
- package yourdomain.sampletest;
-
-
- import android.content.Context;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
-
- public class NewFragment extends Fragment {
-
- public NewFragment() {
-
- }
-
- public static NewFragment getInstance() {
- NewFragment fragment = new NewFragment();
- Bundle args = new Bundle();
- fragment.setArguments(args);
- return fragment;
- }
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_blank, container, false);
- }
-
- @Override
- public void onResume() {
- super.onResume();
- }
-
- @Override
- public void onAttach(Context context) {
- super.onAttach(context);
- }
- }
Here, we can see clearly that MainActivity is written in Kotlin and NewFragment is written in Java. We are calling this fragment from the activity. I am attaching the image of my Android Studio to support the statement described above.
We can see here that the first tab is of Kotlin as the extension can be seen is "MainActivity.kt" and the other one is NewFragment.java and we are calling it from this activity.
activtiy_main.xml
- <?xml version="1.0" encoding="utf-8"?>
-
- <FrameLayout
- android:id="@+id/content_pane"
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"/>
fragment_blank.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="Hello Java Fragment"/>
-
- </RelativeLayout>
Output
Conclusion
From the above example, we can conclude that the Java code can be used in a Kotlin class as well. We have seen that both the files are stored in the same package. This implies that Kotlin is fully interoperable with Java especially because both languages share the same compiler, i.e, JVM.