0% found this document useful (0 votes)
2 views5 pages

Untitled Document

The document contains code for an Android application with a registration form, including UI components defined in XML and Kotlin files. The registration form allows users to input their name, select gender, choose languages, and select a course from a dropdown menu. It features buttons for submitting the form and canceling, redirecting users back to the main activity upon cancellation.

Uploaded by

monikarnayak4444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Untitled Document

The document contains code for an Android application with a registration form, including UI components defined in XML and Kotlin files. The registration form allows users to input their name, select gender, choose languages, and select a course from a dropdown menu. It features buttons for submitting the form and canceling, redirecting users back to the main activity upon cancellation.

Uploaded by

monikarnayak4444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

String.

xml

<resources>
<string name="app_name">UIComponents</string>

<string-array name="course">
<item>BSC IT</item>
<item>BSc CS</item>
<item>BCA</item>
<item>BE</item>
</string-array>
</resources>

Register.kt
package com.example.ui_components

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*

class Register : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // Make sure this matches your XML filename

// Reference UI Components
val cancel = findViewById<Button>(R.id.btncancel)
val submit = findViewById<Button>(R.id.btnsubmit)
val nm = findViewById<EditText>(R.id.txtname)
val rdgen = findViewById<RadioGroup>(R.id.rdgender)
val chkeng = findViewById<CheckBox>(R.id.chkeng)
val chkmar = findViewById<CheckBox>(R.id.chkmarathi)
val chkhin = findViewById<CheckBox>(R.id.chkhindi)
val data = findViewById<TextView>(R.id.txtop)
val mycourse = findViewById<Spinner>(R.id.spncourse)

// Load spinner values from strings.xml


val course = resources.getStringArray(R.array.course)
mycourse.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item,
course)

var selcourse = ""


mycourse.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id:
Long) {
selcourse = course[position]
}

override fun onNothingSelected(parent: AdapterView<*>?) {


selcourse = "Course not selected"
}
}

// Cancel Button - Redirect to MainActivity


cancel.setOnClickListener {
startActivity(Intent(this, MainActivity::class.java))
finish()
}

// Submit Button - Collect user input & display


submit.setOnClickListener {
val all = StringBuilder("Welcome ${nm.text}\n")

// Retrieve selected gender


val selectedId = rdgen.checkedRadioButtonId
if (selectedId != -1) {
val g = findViewById<RadioButton>(selectedId)
all.append("You are ${g.text}\n")
} else {
all.append("Gender not selected\n")
}

// Retrieve selected languages


val languages = mutableListOf<String>()
if (chkeng.isChecked) languages.add("English")
if (chkmar.isChecked) languages.add("Marathi")
if (chkhin.isChecked) languages.add("Hindi")

all.append("Languages Known: ${if (languages.isNotEmpty()) languages.joinToString(",


") else "None"}\n")
all.append("Selected Course: $selcourse")

// Display collected data


data.text = all.toString()
}
}
}

MainActivity.kt
package com.example.ui_components

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val btnRegister = findViewById<Button>(R.id.btnRegister)


btnRegister.setOnClickListener {
val intent = Intent(this, Register::class.java)
startActivity(intent)
}
}
}

Activity _register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/screen3"
android:orientation="vertical"
android:padding="15dp">

<!-- Title TextView -->


<TextView
android:id="@+id/txtv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Registration Form"
android:textAlignment="center"
android:textSize="34sp"
android:textStyle="bold"/>

<!-- Name Input Field -->


<EditText
android:id="@+id/txtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:textSize="24sp"/>

<!-- Gender Selection -->


<RadioGroup
android:id="@+id/rdgender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rdmale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>
<RadioButton
android:id="@+id/rdfemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
</RadioGroup>

<!-- Language Selection -->


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/chkeng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English"/>
<CheckBox
android:id="@+id/chkmarathi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Marathi"/>
<CheckBox
android:id="@+id/chkhindi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hindi"/>
</LinearLayout>

<!-- Course Selection -->


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Course:"/>
<Spinner
android:id="@+id/spncourse"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>

<!-- Buttons (Submit & Cancel) -->


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/btnsubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
<Button
android:id="@+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
</LinearLayout>

<!-- Output TextView -->


<TextView
android:id="@+id/txtop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

You might also like