How We Can Pass UI Controls From One Activity To Another Using Kotlin In Android Studio

In this article, I will show you how to use UI Control and pass that from one activity to another Activity using Kotlin in Android Studio.

Introduction

 
Android is the most popular mobile platform available in the market. With over 85% market share worldwide, Android Operating System dominates the mobile platform market. Today, I will show you how to use UI Control to pass from one activity to another activity using Kotlin in Android Studio.
 
Requirements
Steps to be followed
 
Follow these steps to use UI Control to pass from one activity to another activity using Kotlin In Android Studio. I have included the source code in the attachment.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
How We Can Pass UI Controls From One Activity To Another Using Kotlin In Android Studio
 
Step 2
 
Now, add the activity and click the "Next" button.
 
How We Can Pass UI Controls From One Activity To Another Using Kotlin In Android Studio
 
Step 3
 
You can choose your application name and choose where your project is to be stored and choose Kotlin language for coding the project, Now, select the version of Android and select the target Android devices, and click the "Finish" button.
 
How We Can Pass UI Controls From One Activity To Another Using Kotlin In Android Studio
 
Step 4
 
Go to activity_main.xml. This XML file contains the designing code for your Android app.
 
How We Can Pass UI Controls From One Activity To Another Using Kotlin In Android Studio
 
The XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"    
  5.     
  6.       android:layout_width="match_parent"      
  7.       android:layout_height="match_parent"   
  8.       style="@style/LayoutFontStyle"      
  9.       tools:context=".MainActivity">  
  10.     <TextView android:layout_width="wrap_content"    
  11.          android:layout_height="wrap_content"     
  12.          android:text="@string/employee_database"     
  13.          android:layout_gravity="center"      
  14.          android:textSize="24sp"      
  15.          />  
  16.     <TableLayout      
  17.          android:layout_width="wrap_content"      
  18.          android:layout_height="wrap_content"      
  19.          >  
  20.         <TableRow>  
  21.             <TextView      
  22.                   android:text="@string/id_no"    
  23.                   android:layout_width="wrap_content"    
  24.                   android:layout_height="wrap_content"    
  25.                   android:layout_column="1" />  
  26.             <EditText    
  27.                android:id="@+id/id"      
  28.                android:layout_width="200dp"    
  29.                android:layout_height="wrap_content"    
  30.                style="@style/EditTextFontStyle"    
  31.                />  
  32.         </TableRow>  
  33.         <TableRow>  
  34.             <TextView    
  35.     
  36.                android:text="@string/name"    
  37.                android:layout_width="wrap_content"    
  38.                android:layout_height="wrap_content"    
  39.                android:layout_column="1" />  
  40.             <EditText    
  41.     
  42.                android:id="@+id/name"    
  43.                android:layout_width="wrap_content"    
  44.                android:layout_height="wrap_content"    
  45.                android:layout_column="2"    
  46.                style="@style/EditTextFontStyle"    
  47.                />  
  48.         </TableRow>  
  49.         <TableRow>  
  50.             <TextView    
  51.     
  52.                android:text="@string/address"    
  53.                android:layout_width="wrap_content"    
  54.                android:layout_height="wrap_content"    
  55.                android:layout_column="1" />  
  56.             <EditText    
  57.     
  58.                android:id="@+id/address"    
  59.                android:layout_width="wrap_content"    
  60.                android:layout_height="wrap_content"    
  61.                android:layout_column="2"    
  62.                style="@style/EditTextFontStyle"    
  63.                />  
  64.         </TableRow>  
  65.         <TableRow>  
  66.             <TextView    
  67.     
  68.                android:text="@string/email"    
  69.                android:layout_width="wrap_content"    
  70.                android:layout_height="wrap_content"    
  71.                android:layout_column="1" />  
  72.             <EditText    
  73.     
  74.                android:id="@+id/email"    
  75.                android:layout_width="wrap_content"    
  76.                android:layout_height="wrap_content"    
  77.                android:layout_column="2"    
  78.                style="@style/EditTextFontStyle"    
  79.                />  
  80.         </TableRow>  
  81.         <TableRow>  
  82.             <TextView    
  83.     
  84.                android:text="@string/phone"    
  85.                android:layout_width="wrap_content"    
  86.                android:layout_height="wrap_content"    
  87.                android:layout_column="1" />  
  88.             <EditText    
  89.     
  90.                android:id="@+id/Phone"    
  91.                android:layout_width="wrap_content"    
  92.                android:layout_height="wrap_content"    
  93.                android:layout_column="2"    
  94.                style="@style/EditTextFontStyle"    
  95.                />  
  96.         </TableRow>  
  97.     </TableLayout>  
  98.     <TableLayout    
  99.     
  100.          android:layout_width="wrap_content"    
  101.          android:layout_height="wrap_content">  
  102.         <TableRow>  
  103.             <TextView    
  104.     
  105.                android:text="@string/gender"    
  106.                android:layout_width="116dp"    
  107.                android:layout_height="40dp"    
  108.                android:layout_column="1"    
  109.                android:layout_gravity="center"    
  110.                android:width="200dp"    
  111.                />  
  112.             <RadioGroup    
  113.                android:layout_width="wrap_content"    
  114.                android:layout_height="wrap_content"    
  115.                android:id="@+id/gender"    
  116.                android:text="@string/gender">  
  117.                 <RadioButton    
  118.                   android:id="@+id/female"    
  119.                   android:layout_width="wrap_content"    
  120.                   android:layout_height="wrap_content"    
  121.                   and    
  122.                   roid:text="@string/female"    
  123.                   />  
  124.                 <RadioButton    
  125.                   android:id="@+id/male"    
  126.                   android:layout_width="wrap_content"    
  127.                   android:layout_height="wrap_content"    
  128.                   android:text="@string/male"    
  129.                   />  
  130.             </RadioGroup>  
  131.         </TableRow>  
  132.         <TableRow>  
  133.             <TextView    
  134.     
  135.                android:text="@string/category"    
  136.                android:layout_width="103dp"    
  137.                android:layout_height="wrap_content"    
  138.                android:layout_column="1"    
  139.                />  
  140.             <CheckBox    
  141.                android:id="@+id/software"    
  142.                android:layout_width="wrap_content"    
  143.                android:layout_height="wrap_content"    
  144.                android:layout_column="2"    
  145.                android:text="@string/software_tester"/>  
  146.         </TableRow>  
  147.         <TableRow>  
  148.             <CheckBox    
  149.     
  150.                android:id="@+id/developer"    
  151.                android:layout_width="wrap_content"    
  152.                android:layout_height="wrap_content"    
  153.                android:layout_column="2"    
  154.                android:text="@string/developer"    
  155. />  
  156.         </TableRow>  
  157.         <TableRow>  
  158.             <CheckBox    
  159.     
  160.                android:id="@+id/Support"    
  161.                android:layout_width="wrap_content"    
  162.                android:layout_height="wrap_content"    
  163.                android:layout_column="2"    
  164.                android:text="@string/support"    
  165. />  
  166.         </TableRow>  
  167.         <TableRow>  
  168.             <CheckBox    
  169.                android:id="@+id/marketing"    
  170.                android:layout_width="wrap_content"    
  171.                android:layout_height="wrap_content"    
  172.                android:layout_column="2"    
  173.                android:text="@string/marketing"/>  
  174.         </TableRow>  
  175.     </TableLayout>  
  176.     <LinearLayout    
  177.          android:orientation="horizontal"    
  178.          android:layout_width="wrap_content"    
  179.          android:layout_height="wrap_content">  
  180.         <Button    
  181.          android:id="@+id/button_submit"    
  182.          android:layout_width="match_parent"    
  183.          android:layout_height="match_parent"    
  184.          android:text="@string/submit"    
  185.    />  
  186.     </LinearLayout>  
  187. </LinearLayout>  
Step 5
 
Go to res->values->strings.xml. This XML file is used to contain the string value of the app.
 
The string XML code is given below.
  1. <resources>  
  2.     <string name="app_name">Ui Control</string>  
  3.     <string name="submit">submit</string>  
  4.     <string name="employee_database">Employee database</string>  
  5.     <string name="id_no">id no</string>  
  6.     <string name="name">name</string>  
  7.     <string name="address">address</string>  
  8.     <string name="email">email</string>  
  9.     <string name="phone">phone</string>  
  10.     <string name="software_tester">Software tester</string>  
  11.     <string name="male">male</string>  
  12.     <string name="female">female</string>  
  13.     <string name="gender">gender</string>  
  14.     <string name="category">category</string>  
  15.     <string name="developer">Developer</string>  
  16.     <string name="support">Support</string>  
  17.     <string name="marketing">Marketing</string>  
  18. </resources>   
Step 6
 
Go to res->values->styles.xml. This XML file contains the style value of the app.
 
The style XML code is given below.
  1. <resources>  
  2.     <!-- Base application theme. -->  
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.         <!-- Customize your theme here. -->  
  5.         <item name="colorPrimary">@color/colorPrimary</item>  
  6.         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>  
  7.         <item name="colorAccent">@color/colorAccent</item>  
  8.     </style>  
  9.     <style name="LayoutFontStyle">  
  10.         <item name="android:layout_marginBottom">10dp</item>  
  11.         <item name="android:layout_marginEnd">10dp</item>  
  12.         <item name="android:layout_marginTop">10dp</item>  
  13.         <item name="android:layout_marginStart">10dp</item>  
  14.         <item name="android:orientation">vertical</item>  
  15.     </style>  
  16.     <style name="EditTextFontStyle">  
  17.         <item name="android:layout_marginLeft">20sp</item>  
  18.         <item name="android:layout_marginStart">20sp</item>  
  19.         <item name="android:width">150dp</item>  
  20.     </style>  
  21.     <style name="DataTextStyle">  
  22.         <item name="android:textSize">30sp</item>  
  23.         <item name="android:fontFamily">casual</item>  
  24.     </style>  
  25. </resources>    
Step 7
 
Go to Main Activity.kt. This Kotlin program is the back-end language for your app.

How We Can Pass UI Controls From One Activity To Another Using Kotlin In Android Studio

The Kotlin code is given below.
  1. package com.abu.uicontrol  
  2. import android.content.Intent  
  3. import androidx.appcompat.app.AppCompatActivity  
  4. import android.os.Bundle  
  5. import android.view.Menu  
  6. import android.view.MenuItem  
  7. import android.view.View  
  8. import android.widget.*  
  9.     import kotlinx.android.synthetic.main.activity_main.*class MainActivity: AppCompatActivity() {  
  10.         lateinit  
  11.         var button_submit: Button  
  12.         lateinit  
  13.         var name: EditText  
  14.         lateinit  
  15.         var id: EditText  
  16.         lateinit  
  17.         var email: EditText  
  18.         lateinit  
  19.         var address: EditText  
  20.         lateinit  
  21.         var phone: EditText  
  22.         lateinit  
  23.         var male: RadioButton  
  24.         lateinit  
  25.         var female: RadioButton  
  26.         lateinit  
  27.         var software: CheckBox  
  28.         lateinit  
  29.         var developer: CheckBox  
  30.         lateinit  
  31.         var support: CheckBox  
  32.         lateinit  
  33.         var marketing: CheckBox  
  34.         override fun onCreate(savedInstanceState: Bundle ? ) {  
  35.             super.onCreate(savedInstanceState)  
  36.             setContentView(R.layout.activity_main)  
  37.             button_submit = findViewById(R.id.button_submit)  
  38.             id = findViewById(R.id.id)  
  39.             name = findViewById(R.id.name)  
  40.             address = findViewById(R.id.address)  
  41.             email = findViewById(R.id.email)  
  42.             phone = findViewById(R.id.Phone)  
  43.             male = findViewById(R.id.male)  
  44.             female = findViewById(R.id.female)  
  45.             software = findViewById(R.id.software)  
  46.             support = findViewById(R.id.Support)  
  47.             marketing = findViewById(R.id.marketing)  
  48.             developer = findViewById(R.id.developer)  
  49.             button_submit.setOnClickListener(View.OnClickListener {  
  50.                 val email = email.text.toString()  
  51.                 val id = id.text.toString()  
  52.                 val name = name.text.toString()  
  53.                 val address = address.text.toString()  
  54.                 val phone = phone.text.toString()  
  55.                 val female = female.isChecked  
  56.                 val male = male.isChecked  
  57.                 val software = software.isChecked  
  58.                 val support = Support.isChecked  
  59.                 val marketing = marketing.isChecked  
  60.                 val developer = developer.isChecked  
  61.                 var gender = ""  
  62.                 var softwares = ""  
  63.                 var developers = ""  
  64.                 var Supports = ""  
  65.                 var marketings = ""  
  66.                 if (male) {  
  67.                     gender = "male"  
  68.                 }  
  69.                 if (female) {  
  70.                     gender = "female"  
  71.                 }  
  72.                 if (software) {  
  73.                     softwares = " Software Tester"  
  74.                 }  
  75.                 if (support) {  
  76.                     Supports = " Support"  
  77.                 }  
  78.                 if (marketing) {  
  79.                     marketings = " Marketing"  
  80.                 }  
  81.                 if (developer) {  
  82.                     developers = " Developer"  
  83.                 }  
  84.                 Toast.makeText(applicationContext, "record save", Toast.LENGTH_LONG).show()  
  85.                 val intent = Intent(this, SubmitActivity::class.java)  
  86.                 // start your next activity    
  87.                 intent.putExtra("Name", name)  
  88.                 intent.putExtra("Email", email)  
  89.                 intent.putExtra("id", id)  
  90.                 intent.putExtra("address", address)  
  91.                 intent.putExtra("phone", phone)  
  92.                 intent.putExtra("gender", gender)  
  93.                 intent.putExtra("manual", softwares)  
  94.                 intent.putExtra("support", Supports)  
  95.                 intent.putExtra("marketing", marketings)  
  96.                 intent.putExtra("developer", developers)  
  97.                 startActivity(intent)  
  98.             })  
  99.         }  
  100.     }  
Step 8
 
Create a new Empty Activity and name it is as Submit Activity.
 
Submit Activity XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>      
  2. <LinearLayout      
  3.    xmlns:android="http://schemas.android.com/apk/res/android"      
  4.    xmlns:tools="http://schemas.android.com/tools"      
  5.    xmlns:app="http://schemas.android.com/apk/res-auto"      
  6.    android:layout_width="match_parent"      
  7.    android:layout_height="match_parent"      
  8.    android:orientation="vertical"      
  9.    tools:context=".SubmitActivity">      
  10. <TextView      
  11.    android:text="@string/employee_database"      
  12.    android:layout_width="wrap_content"      
  13.    android:layout_height="wrap_content"      
  14.    android:layout_gravity="center"      
  15.    android:textSize="25sp"      
  16.    />      
  17. <TextView      
  18.    android:id="@+id/text"      
  19.    android:layout_width="wrap_content"      
  20.    android:layout_height="wrap_content"      
  21.    android:textSize="25sp"      
  22.    />      
  23. </LinearLayout>    
The SubmitActivity.kt code is mentioned below.
  1. package com.abu.uicontrol      
  2. import androidx.appcompat.app.AppCompatActivity      
  3. import android.os.Bundle      
  4. import android.widget.TextView      
  5. class SubmitActivity : AppCompatActivity() {      
  6.    override fun onCreate(savedInstanceState: Bundle?) {      
  7.       super.onCreate(savedInstanceState)      
  8.       setContentView(R.layout.activity_submit)      
  9.       val text= findViewById<TextView>(R.id.text)    
  10.       val name =intent.getStringExtra("Name")      
  11.       val email = intent.getStringExtra("Email")     
  12.       val id = intent.getStringExtra("id")      
  13.       val address = intent.getStringExtra("address")     
  14.       val phone = intent.getStringExtra("phone")      
  15.       val gender = intent.getStringExtra("gender")    
  16.       val softwares=intent.getStringExtra("software")      
  17.       val developers=intent.getStringExtra("developer")    
  18.       val Supports = intent.getStringExtra("Support")      
  19.       val marketings = intent.getStringExtra("marketing")      
  20.       text.text = "\nId: "+id+"\nName: "+name+"\naddress: "+address+"\nEmail: "+email+"\nphone: "+phone+"\nGender: "+gender+"\nCategory: "+softwares +marketings +Supports +developers }    
  21. }   
Step 9
 
Then, click the "Run" button or press shift+f10 to finally run the project. And, choose the "virtual machine" option and click OK.
 

Conclusion

 
How We Can Pass UI Controls From One Activity To Another Using Kotlin In Android Studio
 
How We Can Pass UI Controls From One Activity To Another Using Kotlin In Android Studio
 
How We Can Pass UI Controls From One Activity To Another Using Kotlin In Android Studio