In this tutorial you will learn about the Conversion Java to Kotlin and its application with practical example.
Intent & Conversion from Java to Kotlin
Intent: As we know that intent is used to redirect one activity to another activity.Intent Facilitate communication between components, It is Used to invoke components. Uses of Intent are: Starting an Activity, Starting a Service ,Delivering a Broadcast etc.
Types of Intent : Explicit Intents, Implicit Intents .
It is briefly discussed in Android tutorial, Please check Android tutorial for more details.
Android studio version 3.0 provide us facilities to convert our Java class to Kotlin class by following some easy steps. Lets see these steps.
Example: With this given example we get understand about conversion and Also we will learn use of Intent in Android with Kotlin. Follow below steps for Conversion.
Step 1st: In below screen I wrote code of activity in java which we used to do before Kotlin.
Step 2nd :Click on “Code” option in menu of Android Studio and go to “Convert Java File to Kotlin File” or simply use shortcut press “Ctrl+Alt+Shift+K” for converting Java file to Kotlin file. Like below Image.
# After Pressing this option it will be doing process for converting Java File to Kotlin.
Step 3rd: In step third the Java file is converted to Kotlin File, Just check below image.
So this was Java file to Kotlin file conversion. I have used two Activities here , First One is MainActivity and Second one is SecondActivity (Converted file).
#MainActivity.kt: The below code is used for Main Activity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.w3adda.helloapp import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Button import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.header.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) this.heading.setText("Main Activity") this.btn_fir.setOnClickListener { val i = Intent(this, SecondActivity::class.java) startActivity(i) } } } |
#SecondActivity.java:The below code is used for Second Activity(Before Conversion).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.infowind.myapplication; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Button btn_next=(Button)findViewById(R.id.btn_next); btn_next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i =new Intent(SecondActivity.this,MainActivity.class); startActivity(i); } }); } } |
SecondActivity.kt: After conversion of java file to Kotlin file will look like below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.w3adda.helloapp import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Button import kotlinx.android.synthetic.main.activity_second.* import kotlinx.android.synthetic.main.header.* class SecondActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_second) val btn_next=findViewById<View>(R.id.btn_next) as Button btn_next.setOnClickListener { val i = Intent(this, MainActivity::class.java) startActivity(i) } } } |
XML: There are three XML files are used in this project 1st is for Main Activity, 2nd is for Second Activity and 3rd is “Header” File which is used in both XML. Checkout all below.
#header.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="@color/header" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="40dp" android:text="Main Activity" android:textSize="18sp" android:layout_margin="10dp" android:id="@+id/heading" /> </RelativeLayout> |
#activity_main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?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" tools:context="com.w3adda.helloapp.MainActivity"> <include layout="@layout/header" android:id="@+id/heder"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_hello" android:textColor="#000" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <Button android:id="@+id/btn_fir" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click" android:layout_above="@+id/tv_hello" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp"/> </RelativeLayout> |
#activity_second:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.w3adda.helloapp.SecondActivity"> <include layout="@layout/header" android:id="@+id/header"/> <Button android:id="@+id/btn_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="107dp" android:text="Next" /> </RelativeLayout> |
So we are all set for this code. Guys do not forget to declare Second Activity in “Manifest.xml” file.
Run: Follow all steps and after completion Run your project, you will get Output like below images.
Hopefully this will help you to get conversion of Java file to Kotlin file and also Intent in Kotlin.