0% found this document useful (0 votes)
9 views7 pages

Android Pratical

basic pratical

Uploaded by

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

Android Pratical

basic pratical

Uploaded by

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

Android pratical

1.write a program using kotlin to implement control structure and loops

fun main() {
// if-else control structure
val number = 42
if (number % 2 == 0) {
println("$number is even")
} else {
println("$number is odd")
}

// when control structure


val grade = 'B'
when (grade) {
'A' -> println("Excellent!")
'B' -> println("Good!")
'C' -> println("Fair!")
'D' -> println("Poor!")
else -> println("Invalid grade")
}

// for loop
val numbers = arrayOf(1, 2, 3, 4, 5)
for (number in numbers) {
println(number)
}
// while loop
var i = 1
while (i <= 5) {
println(i)
i++
}

// do-while loop
var j = 1
do {
println(j)
j++
} while (j <= 5)
}

2. write a program to implement inheritance in kotlin


// parent class
open class Vehicle(val brand: String, val model: String) {
fun drive() {
println("$brand $model is driving")
}
}

// child class
class Car(brand: String, model: String, val numDoors: Int) : Vehicle(brand,
model) {
fun honk() {
println("$brand $model is honking")
}
}

// main function
fun main() {
// create a car object
val car = Car("Toyota", "Camry", 4)

// call methods from parent class


car.drive()

// call methods from child class


car.honk()

// print car details


println("${car.brand} ${car.model} has ${car.numDoors} doors")
}

3. create a android application for to create a registration form and login form
and use of share preferences

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/register_title"
android:text="Registration Form"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/register_name"
android:hint="Enter your name"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/register_email"
android:hint="Enter your email"
android:inputType="textEmailAddress"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/register_password"
android:hint="Enter your password"
android:inputType="textPassword"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/register_button"
android:text="Register"
android:textSize="16sp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

class RegisterActivity : AppCompatActivity() {

private lateinit var sharedPreferences: SharedPreferences

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)

sharedPreferences = getSharedPreferences("MyAppPreferences",
Context.MODE_PRIVATE)

val nameEditText = findViewById<EditText>(R.id.register_name)


val emailEditText = findViewById<EditText>(R.id.register_email)
val passwordEditText = findViewById<EditText>(R.id.register_password)
val registerButton = findViewById<Button>(R.id.register_button)

registerButton.setOnClickListener {
val name = nameEditText.text.toString()
val email = emailEditText.text.toString()
val password = passwordEditText.text.toString()

// Store the user data in the SharedPreferences file


val editor = sharedPreferences.edit()
editor.putString("name", name)
editor.putString("email", email)
editor.putString("password", password)
editor.apply()

Toast.makeText(this, "Registration Successful!",


Toast.LENGTH_SHORT).show()

// Redirect to the login form


startActivity(Intent(this, LoginActivity::class.java))
}
}
}

You might also like