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
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.
Step 2
Now, add the activity and click the "Next" button.
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.
Step 4
Go to activity_main.xml. This XML file contains the designing code for your Android app.
The XML code is given below.
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.
- <resources>
- <string name="app_name">Ui Control</string>
- <string name="submit">submit</string>
- <string name="employee_database">Employee database</string>
- <string name="id_no">id no</string>
- <string name="name">name</string>
- <string name="address">address</string>
- <string name="email">email</string>
- <string name="phone">phone</string>
- <string name="software_tester">Software tester</string>
- <string name="male">male</string>
- <string name="female">female</string>
- <string name="gender">gender</string>
- <string name="category">category</string>
- <string name="developer">Developer</string>
- <string name="support">Support</string>
- <string name="marketing">Marketing</string>
- </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.
- <resources>
- <!-- Base application theme. -->
- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
- <!-- Customize your theme here. -->
- <item name="colorPrimary">@color/colorPrimary</item>
- <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
- <item name="colorAccent">@color/colorAccent</item>
- </style>
- <style name="LayoutFontStyle">
- <item name="android:layout_marginBottom">10dp</item>
- <item name="android:layout_marginEnd">10dp</item>
- <item name="android:layout_marginTop">10dp</item>
- <item name="android:layout_marginStart">10dp</item>
- <item name="android:orientation">vertical</item>
- </style>
- <style name="EditTextFontStyle">
- <item name="android:layout_marginLeft">20sp</item>
- <item name="android:layout_marginStart">20sp</item>
- <item name="android:width">150dp</item>
- </style>
- <style name="DataTextStyle">
- <item name="android:textSize">30sp</item>
- <item name="android:fontFamily">casual</item>
- </style>
- </resources>
Step 7
Go to Main Activity.kt. This Kotlin program is the back-end language for your app.
The Kotlin code is given below.
- package com.abu.uicontrol
- import android.content.Intent
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.view.Menu
- import android.view.MenuItem
- import android.view.View
- import android.widget.*
- import kotlinx.android.synthetic.main.activity_main.*class MainActivity: AppCompatActivity() {
- lateinit
- var button_submit: Button
- lateinit
- var name: EditText
- lateinit
- var id: EditText
- lateinit
- var email: EditText
- lateinit
- var address: EditText
- lateinit
- var phone: EditText
- lateinit
- var male: RadioButton
- lateinit
- var female: RadioButton
- lateinit
- var software: CheckBox
- lateinit
- var developer: CheckBox
- lateinit
- var support: CheckBox
- lateinit
- var marketing: CheckBox
- override fun onCreate(savedInstanceState: Bundle ? ) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- button_submit = findViewById(R.id.button_submit)
- id = findViewById(R.id.id)
- name = findViewById(R.id.name)
- address = findViewById(R.id.address)
- email = findViewById(R.id.email)
- phone = findViewById(R.id.Phone)
- male = findViewById(R.id.male)
- female = findViewById(R.id.female)
- software = findViewById(R.id.software)
- support = findViewById(R.id.Support)
- marketing = findViewById(R.id.marketing)
- developer = findViewById(R.id.developer)
- button_submit.setOnClickListener(View.OnClickListener {
- val email = email.text.toString()
- val id = id.text.toString()
- val name = name.text.toString()
- val address = address.text.toString()
- val phone = phone.text.toString()
- val female = female.isChecked
- val male = male.isChecked
- val software = software.isChecked
- val support = Support.isChecked
- val marketing = marketing.isChecked
- val developer = developer.isChecked
- var gender = ""
- var softwares = ""
- var developers = ""
- var Supports = ""
- var marketings = ""
- if (male) {
- gender = "male"
- }
- if (female) {
- gender = "female"
- }
- if (software) {
- softwares = " Software Tester"
- }
- if (support) {
- Supports = " Support"
- }
- if (marketing) {
- marketings = " Marketing"
- }
- if (developer) {
- developers = " Developer"
- }
- Toast.makeText(applicationContext, "record save", Toast.LENGTH_LONG).show()
- val intent = Intent(this, SubmitActivity::class.java)
-
- intent.putExtra("Name", name)
- intent.putExtra("Email", email)
- intent.putExtra("id", id)
- intent.putExtra("address", address)
- intent.putExtra("phone", phone)
- intent.putExtra("gender", gender)
- intent.putExtra("manual", softwares)
- intent.putExtra("support", Supports)
- intent.putExtra("marketing", marketings)
- intent.putExtra("developer", developers)
- startActivity(intent)
- })
- }
- }
Step 8
Create a new Empty Activity and name it is as Submit Activity.
Submit Activity XML code is given below.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".SubmitActivity">
- <TextView
- android:text="@string/employee_database"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:textSize="25sp"
- />
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="25sp"
- />
- </LinearLayout>
The SubmitActivity.kt code is mentioned below.
- package com.abu.uicontrol
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.TextView
- class SubmitActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_submit)
- val text= findViewById<TextView>(R.id.text)
- val name =intent.getStringExtra("Name")
- val email = intent.getStringExtra("Email")
- val id = intent.getStringExtra("id")
- val address = intent.getStringExtra("address")
- val phone = intent.getStringExtra("phone")
- val gender = intent.getStringExtra("gender")
- val softwares=intent.getStringExtra("software")
- val developers=intent.getStringExtra("developer")
- val Supports = intent.getStringExtra("Support")
- val marketings = intent.getStringExtra("marketing")
- text.text = "\nId: "+id+"\nName: "+name+"\naddress: "+address+"\nEmail: "+email+"\nphone: "+phone+"\nGender: "+gender+"\nCategory: "+softwares +marketings +Supports +developers }
- }
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