MAD LAB PROGRAMS
[Link] a “Hello world” Application.
[Link] code
package [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
val t = TextView(this)
[Link] = "Hello World"
[Link] = 26f
setContentView(t)
}
}
2. Creating an application that displays messages based on the screen orientation.
[Link] code
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
val t = findViewById<TextView>([Link])
if ([Link] == Configuration.ORIENTATION_LANDSCAPE)
[Link] = "Landscape Screen"
else
[Link] = "Portrait Screen"
}
}
xml_code
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="[Link]
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Message"
android:textSize="24sp"/>
3. Create an application to develop a Login window using UI controls.
[Link] code
package [Link]
import [Link]
import [Link].*
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val user = findViewById<EditText>([Link])
val pass = findViewById<EditText>([Link])
val btn = findViewById<Button>([Link])
val result = findViewById<TextView>([Link])
[Link] {
if([Link]()=="admin" && [Link]()=="1234"){
[Link]="Login Successful"
}
else{
[Link](this,"Login Failed",Toast.LENGTH_SHORT).show()
}
}
}
}
xml_code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/username"
android:hint="Enter Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/password"
android:hint="Enter Password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/loginBtn"
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/result"
android:text=""
android:textSize="18sp"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
4 Create an application to implement new activity using explicit intent, implicit intent, and content
provider.
For_implicit_intent
[Link] code
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
findViewById<Button>([Link].b).setOnClickListener {
val i = Intent(Intent.ACTION_VIEW,
[Link]("[Link]
startActivity(i)
}
}
}
xml_code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/b"
android:text="Open Google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
For_explicit_intent
[Link] code
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
findViewById<Button>([Link].b).setOnClickListener {
startActivity(Intent(this, SecondActivity::[Link]))
}
}
}
xml_code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/b"
android:text="Open New Screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
[Link] code
Note: steps to create a Second Activity in Android Studio(1. Open your project in Android Studio, 2.
In the Project panel (left side) Go to: app-> java-> your package name([Link]),
3. Right click on your package name select: New → Activity → Empty Views Activity , then A
window will open Fill like this:
Activity Name: SecondActivity
Layout Name: activity_second
Then click Finish.
[Link] code
package [Link]
import [Link]
import [Link]
import [Link]
class SecondActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
val t = TextView(this)
[Link] = "Welcome to Second Activity"
[Link] = 24f
setContentView(t)
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/b"
android:text="Open Second Activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
[Link] an application that displays custom custom-designed Opening Screen.
[Link] code
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
val t = findViewById<TextView>([Link].t)
Handler([Link]()).postDelayed({
[Link] = "Welcome"
}, 3000)
}
}
xml_code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/t"
android:text="MY APP"
android:textSize="30sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
[Link] a UI with all views.
[Link] code
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
val rating = findViewById<RatingBar>([Link])
val percent = findViewById<TextView>([Link])
val submit = findViewById<Button>([Link])
[Link] { _, ratingValue, _ ->
val p = (ratingValue / [Link]) * 100
[Link] = "Rating: $p %"
}
[Link] {
[Link](this,"Submitted",Toast.LENGTH_SHORT).show()
}
}
}
xml_code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/name"
android:hint="Enter Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="Gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:text="Male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:text="Female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
<TextView
android:text="Rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RatingBar
android:id="@+id/rating"
android:numStars="5"
android:stepSize="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/percent"
android:text="Rating Percentage"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:text="I Agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/submit"
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
7. Create a menu in the Application
To create a menu in an Android application, we use a Menu Resource file and inflate it in
MainActivity
Step1: Create Menu Folder
In Android Studio:
res → right click → New → Android Resource Directory → select directory_name as "menu" &
resourcetypye also “menu”.
Step2: menu_main.xml
Create inside res/menu
Menu_main.Xml_code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="[Link]
<item
android:id="@+id/m1"
android:title="Option1"/>
<item
android:id="@+id/m2"
android:title="Option2"/>
</menu>
[Link] code
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
[Link]([Link].menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
[Link](this,"Selected",Toast.LENGTH_SHORT).show()
return true
}
}
Xml_code
<TextView xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Menu Example"
android:gravity="center"
android:textSize="25sp"/>
Note:(go to res → values → [Link] click on it and find this line “<style name="[Link]"
parent="[Link]">” and then from this line remove .NoActionbar so
it will become “<style name="[Link]" parent="[Link]">” like this)
8. Read/ write the Local data.
[Link] code
package [Link]
import [Link]
import [Link].*
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
val t = findViewById<EditText>([Link])
val r = findViewById<TextView>([Link])
findViewById<Button>([Link]).setOnClickListener {
openFileOutput("[Link]", MODE_APPEND)
.write(([Link]()+"\n").toByteArray())
[Link](this,"Saved",Toast.LENGTH_SHORT).show()
[Link]("")
}
findViewById<Button>([Link]).setOnClickListener {
[Link] = openFileInput("[Link]").bufferedReader().readText()
}
}
}
xml_code
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/txt"
android:hint="Enter text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/save"
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/read"
android:text="Read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/result"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
9. Create / Read / Write data with a database (SQLite).
[Link] code
package [Link]
import [Link]
import [Link].*
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
val db = openOrCreateDatabase("db", MODE_PRIVATE, null)
[Link]("CREATE TABLE IF NOT EXISTS s(id INTEGER PRIMARY KEY AUTOINCREMENT,name
TEXT,age TEXT)")
val n = findViewById<EditText>([Link].n)
val a = findViewById<EditText>([Link].a)
val t = findViewById<TableLayout>([Link].t)
findViewById<Button>([Link].b).setOnClickListener {
[Link]("INSERT INTO s(name,age) VALUES('${[Link]}','${[Link]}')")
[Link]()
val c = [Link]("SELECT * FROM s", null)
val h = TableRow(this)
[Link](TextView(this).apply { text="ID" })
[Link](TextView(this).apply { text="Name" })
[Link](TextView(this).apply { text="Age" })
[Link](h)
while ([Link]()) {
val r = TableRow(this)
[Link](TextView(this).apply { text=[Link](0) })
[Link](TextView(this).apply { text=[Link](1) })
[Link](TextView(this).apply { text=[Link](2) })
[Link](r)
}
}
}
}
xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/n"
android:hint="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/a"
android:hint="Age"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/b"
android:text="Add"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TableLayout
android:id="@+id/t"
android:stretchColumns="*"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
10. Create an application to send SMS and receive SMS
[Link] code
package [Link]
import [Link]
import [Link]
import [Link]
import [Link].*
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
val num = findViewById<EditText>([Link])
val msg = findViewById<EditText>([Link])
findViewById<Button>([Link]).setOnClickListener {
val i = Intent(Intent.ACTION_VIEW)
[Link] = [Link]("[Link]
[Link]("sms_body", [Link]())
startActivity(i)
}
}
}
xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/num"
android:hint="Enter Phone Number"
android:inputType="phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/msg"
android:hint="Enter Message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/send"
android:text="Send SMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
11. Create an application to send an e-mail.
[Link] code
package [Link]
import [Link]
import [Link]
import [Link].*
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
val e = findViewById<EditText>([Link])
val s = findViewById<EditText>([Link])
val m = findViewById<EditText>([Link])
findViewById<Button>([Link]).setOnClickListener {
val i = Intent(Intent.ACTION_SENDTO)
[Link] = [Link]("[Link]
[Link](Intent.EXTRA_SUBJECT, [Link]())
[Link](Intent.EXTRA_TEXT, [Link]())
startActivity(i)
}
}
}
xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/email"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/sub"
android:hint="Subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/msg"
android:hint="Message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/send"
android:text="Send Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
12. Display Map based on the Current/given location.
[Link] code
package [Link]
import [Link]
import [Link]
import [Link]
import [Link].*
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
val loc = findViewById<EditText>([Link])
findViewById<Button>([Link]).setOnClickListener {
val gmmIntentUri =
[Link]("geo:0,0?q=${[Link]}")
val mapIntent =
Intent(Intent.ACTION_VIEW, gmmIntentUri)
[Link]("[Link]")
startActivity(mapIntent)
}
}
}
xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/location"
android:hint="Enter Location"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/open"
android:text="Open Map"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
[Link] a sample application with login module(check user name and password) On successful
login change Textview “Login Successful”. On login fail alert using Toast “login fail”
[Link] code
package [Link]
import [Link]
import [Link]
import [Link].*
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView([Link].activity_main)
val u = findViewById<EditText>([Link])
val p = findViewById<EditText>([Link])
val db: SQLiteDatabase = openOrCreateDatabase("mydb", MODE_PRIVATE, null)
[Link]("CREATE TABLE IF NOT EXISTS users(name TEXT, pass TEXT)")
[Link]("INSERT INTO users VALUES('admin','1234')")
findViewById<Button>([Link]).setOnClickListener {
val c = [Link](
"SELECT * FROM users WHERE name=? AND pass=?",
arrayOf([Link](), [Link]())
)
if ([Link]())
[Link](this,"Login Successful",Toast.LENGTH_SHORT).show()
else
[Link](this,"Login Fail",Toast.LENGTH_SHORT).show()
}
}
}
xml_code
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/user"
android:hint="Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/pass"
android:hint="Password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/login"
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/result"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>