Interoperability Of Kotlin - Calling Java Fragment In A Kotlin Activity

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 
  1. package yourdomain.sampletest  
  2.   
  3. import android.os.Bundle  
  4. import android.support.v7.app.AppCompatActivity  
  5.   
  6.   
  7. class MainActivity : AppCompatActivity() {  
  8.   
  9.     override fun onCreate(savedInstanceState: Bundle?) {  
  10.         super.onCreate(savedInstanceState)  
  11.         setContentView(R.layout.activity_main)  
  12.   
  13.         val manager = supportFragmentManager  
  14.         val transaction = manager.beginTransaction()  
  15.         transaction.replace(R.id.content_pane, NewFragment.getInstance())  
  16.         transaction.commit()  
  17.   
  18.     }  
  19.   
  20.     override fun onResume() {  
  21.         super.onResume()  
  22.     }  
  23.         
  24. }  
Look at this code carefully; it is Kotlin. Now, we will create a fragment named "NewFragment.java". The code is given below. 
  1. package yourdomain.sampletest;  
  2.   
  3.   
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.support.v4.app.Fragment;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10.   
  11.   
  12. public class NewFragment extends Fragment {  
  13.   
  14.     public NewFragment() {  
  15.         // Required empty public constructor  
  16.     }  
  17.   
  18.     public static NewFragment getInstance() {  
  19.         NewFragment fragment = new NewFragment();  
  20.         Bundle args = new Bundle();  
  21.         fragment.setArguments(args);  
  22.         return fragment;  
  23.     }  
  24.   
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.     }  
  29.   
  30.     @Override  
  31.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  32.                              Bundle savedInstanceState) {  
  33.         return inflater.inflate(R.layout.fragment_blank, container, false);  
  34.     }  
  35.   
  36.     @Override  
  37.     public void onResume() {  
  38.         super.onResume();  
  39.     }  
  40.   
  41.     @Override  
  42.     public void onAttach(Context context) {  
  43.         super.onAttach(context);  
  44.     }  
  45. }  

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 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <FrameLayout  
  4.         android:id="@+id/content_pane"  
  5.         xmlns:android="http://schemas.android.com/apk/res/android"  
  6.         xmlns:tools="http://schemas.android.com/tools"  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent"  
  9.         tools:context=".MainActivity"/>  
fragment_blank.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.              xmlns:tools="http://schemas.android.com/tools"  
  4.              android:layout_width="match_parent"  
  5.              android:layout_height="match_parent"  
  6.             >  
  7.   
  8.     <TextView  
  9.             android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content"  
  11.             android:layout_centerInParent="true"  
  12.             android:text="Hello Java Fragment"/>  
  13.   
  14. </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.


Similar Articles