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. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. style="@style/LayoutFontStyle"
  8. tools:context=".MainActivity">
  9. <TextView android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="@string/employee_database"
  12. android:layout_gravity="center"
  13. android:textSize="24sp"
  14. />
  15. <TableLayout
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. >
  19. <TableRow>
  20. <TextView
  21. android:text="@string/id_no"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_column="1" />
  25. <EditText
  26. android:id="@+id/id"
  27. android:layout_width="200dp"
  28. android:layout_height="wrap_content"
  29. style="@style/EditTextFontStyle"
  30. />
  31. </TableRow>
  32. <TableRow>
  33. <TextView
  34. android:text="@string/name"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_column="1" />
  38. <EditText
  39. android:id="@+id/name"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_column="2"
  43. style="@style/EditTextFontStyle"
  44. />
  45. </TableRow>
  46. <TableRow>
  47. <TextView
  48. android:text="@string/address"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_column="1" />
  52. <EditText
  53. android:id="@+id/address"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:layout_column="2"
  57. style="@style/EditTextFontStyle"
  58. />
  59. </TableRow>
  60. <TableRow>
  61. <TextView
  62. android:text="@string/email"
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:layout_column="1" />
  66. <EditText
  67. android:id="@+id/email"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:layout_column="2"
  71. style="@style/EditTextFontStyle"
  72. />
  73. </TableRow>
  74. <TableRow>
  75. <TextView
  76. android:text="@string/phone"
  77. android:layout_width="wrap_content"
  78. android:layout_height="wrap_content"
  79. android:layout_column="1" />
  80. <EditText
  81. android:id="@+id/Phone"
  82. android:layout_width="wrap_content"
  83. android:layout_height="wrap_content"
  84. android:layout_column="2"
  85. style="@style/EditTextFontStyle"
  86. />
  87. </TableRow>
  88. </TableLayout>
  89. <TableLayout
  90. android:layout_width="wrap_content"
  91. android:layout_height="wrap_content">
  92. <TableRow>
  93. <TextView
  94. android:text="@string/gender"
  95. android:layout_width="116dp"
  96. android:layout_height="40dp"
  97. android:layout_column="1"
  98. android:layout_gravity="center"
  99. android:width="200dp"
  100. />
  101. <RadioGroup
  102. android:layout_width="wrap_content"
  103. android:layout_height="wrap_content"
  104. android:id="@+id/gender"
  105. android:text="@string/gender">
  106. <RadioButton
  107. android:id="@+id/female"
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"
  110. and
  111. roid:text="@string/female"
  112. />
  113. <RadioButton
  114. android:id="@+id/male"
  115. android:layout_width="wrap_content"
  116. android:layout_height="wrap_content"
  117. android:text="@string/male"
  118. />
  119. </RadioGroup>
  120. </TableRow>
  121. <TableRow>
  122. <TextView
  123. android:text="@string/category"
  124. android:layout_width="103dp"
  125. android:layout_height="wrap_content"
  126. android:layout_column="1"
  127. />
  128. <CheckBox
  129. android:id="@+id/software"
  130. android:layout_width="wrap_content"
  131. android:layout_height="wrap_content"
  132. android:layout_column="2"
  133. android:text="@string/software_tester"/>
  134. </TableRow>
  135. <TableRow>
  136. <CheckBox
  137. android:id="@+id/developer"
  138. android:layout_width="wrap_content"
  139. android:layout_height="wrap_content"
  140. android:layout_column="2"
  141. android:text="@string/developer"
  142. />
  143. </TableRow>
  144. <TableRow>
  145. <CheckBox
  146. android:id="@+id/Support"
  147. android:layout_width="wrap_content"
  148. android:layout_height="wrap_content"
  149. android:layout_column="2"
  150. android:text="@string/support"
  151. />
  152. </TableRow>
  153. <TableRow>
  154. <CheckBox
  155. android:id="@+id/marketing"
  156. android:layout_width="wrap_content"
  157. android:layout_height="wrap_content"
  158. android:layout_column="2"
  159. android:text="@string/marketing"/>
  160. </TableRow>
  161. </TableLayout>
  162. <LinearLayout
  163. android:orientation="horizontal"
  164. android:layout_width="wrap_content"
  165. android:layout_height="wrap_content">
  166. <Button
  167. android:id="@+id/button_submit"
  168. android:layout_width="match_parent"
  169. android:layout_height="match_parent"
  170. android:text="@string/submit"
  171. />
  172. </LinearLayout>
  173. </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