BCA Sem. 6 – CS-31 | Android with Kotlin (July-2022)
android
Android Model Paper
BCA Sem. 6 – CS-31 | Android with Kotlin (March-2022)
Are Dumb phones the Smarter Choice?
Dumb phones are making a comeback. The year 2000 is calling and this time GenZ is picking up. What are these dumb phones? Why are they seeing a revival?
Android Kotlin Checkbox example
MainActivity.kt
package com.example.checkboxdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
// author : www.raviroza.com
// date : 2-Jan-2023, 9.00 am
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity()
{
var ch = mutableSetOf("")
fun setChoices()
{
var t = ""
for(tt in ch)
{
t = t + tt + " "
}
lblChoiceList.text = t.toString()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*chkCricket.setOnClickListener()
{
if(chkCricket.isChecked == true)
{
ch.add(chkCricket.text.toString())
}
else
{
ch.remove(chkCricket.text.toString())
}
lblChoiceList.text = ch.toString()
}
chkFootBall.setOnClickListener()
{
if(chkFootBall.isChecked == true)
{
ch.add(chkFootBall.text.toString())
}
else
{
ch.remove(chkFootBall.text.toString())
}
lblChoiceList.text = ch.toString()
}
chkKabbadi.setOnClickListener()
{
if(chkKabbadi.isChecked == true)
{
ch.add(chkKabbadi.text.toString())
}
else
{
ch.remove(chkKabbadi.text.toString())
}
lblChoiceList.text = ch.toString()
}
chkFootBall.setOnClickListener()
{
lblChoiceList.text = lblChoiceList.text.toString() + chkFootBall.text.toString()
}
chkKabbadi.setOnClickListener()
{
lblChoiceList.text = lblChoiceList.text.toString() + chkKabbadi.text.toString()
}*/
chkCricket.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked)
{
ch.add(buttonView.text.toString())
setChoices()
}
else
{
ch.remove(buttonView.text.toString())
setChoices()
}
}
chkFootBall.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked)
{
ch.add(buttonView.text.toString())
setChoices()
}
else
{
ch.remove(buttonView.text.toString())
setChoices()
}
}
chkKabbadi.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked)
{
ch.add(buttonView.text.toString())
setChoices()
}
else
{
ch.remove(buttonView.text.toString())
setChoices()
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:background="@color/MainColor"
tools:context=".MainActivity" >
<LinearLayout
android:background="@color/SubColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/lblHead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="#2196F3"
android:gravity="center"
android:text="Check Box Demo"
android:textColor="#232031"
android:textSize="30sp" />
<TextView
android:id="@+id/lblSubHead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center"
android:text="Select Your Game"
android:textSize="18sp" />
<CheckBox
android:id="@+id/chkCricket"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="@string/cricket"
android:textSize="18sp" />
<CheckBox
android:id="@+id/chkKabbadi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="@string/Kabbadi"
android:textSize="18sp" />
<CheckBox
android:id="@+id/chkFootBall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="@string/Football"
android:textSize="18sp" />
<TextView
android:id="@+id/lblHereAreYourChoices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center"
android:background="#4876a8"
android:text="Your Choices are "
android:textSize="18sp" />
<TextView
android:id="@+id/lblChoiceList"
android:layout_width="match_parent"
android:layout_height="301dp"
android:layout_margin="4dp"
android:gravity="center_horizontal"
android:text=".... "
android:textSize="18sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Android Kotlin SQLite Demo
Here is the example of Android with Kotlin SQLite database in Android Studio.
MainActivity.kt
package com.example.sqlitedemo
import android.content.Context
// Author : www.raviroza.com
// Date : 26-Dec-2022, 10.00 am
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
lateinit var DB : SQLiteDatabase
lateinit var QRY : String
lateinit var CURSOR : Cursor
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
DB = openOrCreateDatabase("MyDB", Context.MODE_PRIVATE,null)
Toast.makeText(this,DB.path.toString(),Toast.LENGTH_LONG).show()
QRY = "CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);"
DB.execSQL(QRY)
Toast.makeText(this,"Table Created !",Toast.LENGTH_LONG).show()
loadData()
btnAdd.setOnClickListener()
{
QRY = "insert into student values ( ${txtRollNo.text} , '${txtName.text}' , ${txtMarks.text} )"
DB.execSQL(QRY)
Toast.makeText(this,"Record Inserted !",Toast.LENGTH_LONG).show()
txtName.text = null
txtMarks.text = null
txtRollNo.text = null
loadData()
}
}
fun loadData()
{
QRY = "Select * from Student "
CURSOR = DB.rawQuery(QRY,null)
var data : String = ""
while (CURSOR.moveToNext())
{
data += CURSOR.getString(0) + "\n"
data += CURSOR.getString(1) + "\n"
data += "-------------------------------\n"
}
if(data.length==0) {
lblResult.text = "No Records Found"
}
else
{
lblResult.text = data
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="16dp"
tools:context=".MainActivity">
<!-- Author : www.raviroza.com-->
<!-- Date : 26-Dec-2022, 10.00 am-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DB App Demo - Student"
android:textSize="25dp"
android:gravity="center"
android:background="@color/colorAccent"
android:padding="8dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Roll No. :"
android:textSize="20dp"
/>
<EditText
android:id="@+id/txtRollNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:inputType="number"
android:textSize="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Name :"
android:textSize="20dp"
/>
<EditText
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Marks:"
android:textSize="20dp"
/>
<EditText
android:id="@+id/txtMarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:inputType="number"
/>
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Add Record"
android:textAllCaps="false"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/lblResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="student data"
android:textSize="18dp"
/>
</ScrollView>
</LinearLayout>
Getters and Setter in Kotlin
Getters and Setters is also known as custom property accessors.