3.
Create an application to design a Visiting Card
XML
Download the logo and save it as [Link], copy and paste it (or drag
and drop it) to drawable (below res folder)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- Top row: Company name + logo -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ABC TECHNOLOGIES"
android:textStyle="bold"
android:textSize="22sp"
android:gravity="center" />
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/logo" />
</LinearLayout>
<!-- Employee Name -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: Rajesh Kumar"
android:layout_marginTop="16dp" />
<!-- Job Title -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Job Title: Software Engineer" />
<!-- Horizontal line -->
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp" />
<!-- Contact Details -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone: 9876543210" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address: Bangalore, India" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email: info@[Link]" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fax: 080-123456" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Website: [Link]" />
</LinearLayout>
Kotlin file:
package [Link] // your package name may differ
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
4. Create an application to develop Login window using UI controls.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:id="@+id/etUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/etPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
<TextView
android:id="@+id/tvMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:paddingTop="12dp" />
</LinearLayout>
KOTLIN:
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val u = findViewById<EditText>([Link])
val p = findViewById<EditText>([Link])
val t = findViewById<TextView>([Link])
findViewById<Button>([Link]).setOnClickListener {
[Link] = if ([Link]() == "admin" && [Link]() == "1234")
"Login Successful"
else
"Login Fail"
}
}
}
5. Create an application to implement new activity using explicit intent, implicit
intent and content provider.
Explicit Intent means opening another screen inside your app,
so Android must have another Activity class.
Therefore you must create one more Kotlin file → [Link].
How to create it
1. In Android Studio
app → Right click
2. Select
New → Activity → Empty Views Activity
3. Name it: SecondActivity
4. Click Finish
Code for activity_main.xml:
<?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">
<Button android:id="@+id/e" android:text="Explicit"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/i" android:text="Implicit"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/c" android:text="Contacts"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
Code for [Link]
package [Link]
import [Link]
import [Link]
import [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)
findViewById<Button>([Link].e).setOnClickListener {
startActivity(Intent(this, SecondActivity::[Link])) // Explicit
}
findViewById<Button>([Link].i).setOnClickListener {
startActivity(Intent(Intent.ACTION_VIEW, [Link]("[Link] //
Implicit
}
findViewById<Button>([Link].c).setOnClickListener {
startActivity(Intent(Intent.ACTION_VIEW, [Link].CONTENT_URI))
// Content Provider
}
}
}
Code for [Link]
package [Link]
import [Link]
import [Link]
import [Link]
class SecondActivity : AppCompatActivity() {
override fun onCreate(b: Bundle?) {
[Link](b)
setContentView(TextView(this).apply { text = "Second Activity"; textSize = 22f })
6. Create an application that displays a custom designed Opening
Screen.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAIN ACTIVITY SCREEN"
android:textSize="24sp"
android:textStyle="bold"/>
</LinearLayout>
[Link]
package [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_splash)
Handler([Link]()).postDelayed({
startActivity(Intent(this, MainActivity::[Link]))
finish()
}, 2000)
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Splash Screen"
android:textSize="26sp"
android:textStyle="bold"/>
</LinearLayout>
In the [Link]
do the changes under <activity> tag as shown below:
Extra programs :
Implementing About box:
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val btn = findViewById<Button>([Link])
[Link] {
[Link](this)
.setTitle("About")
.setMessage("OpeningScreen App\nVersion 1.0\nDeveloped by
xyz")
.setPositiveButton("OK", null)
.show()
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btnAbout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"/>
</LinearLayout>
Applying themes and styles:
Main_Activity.kt:
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
enableEdgeToEdge()
setContentView([Link].activity_main)
[Link](findViewById([Link])) {
v, insets ->
val systemBars =
[Link]([Link]())
[Link]([Link], [Link], [Link],
[Link])
insets
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
style="@style/MyTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Theme and Style Demo"/>
</[Link]>
[Link]:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="[Link]"
parent="[Link]">
<item name="android:windowBackground">#3F51B5</item>
</style>
<style name="MyTextStyle">
<item name="android:textSize">28sp</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textStyle">bold</item>
<item name="android:fontFamily">Serif</item>
</style>
</resources>
7. Create an UI with all views:
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val sp = findViewById<Spinner>([Link].sp1)
val items = arrayOf("BCA", "MCA", "BSc")
[Link] = ArrayAdapter(this,
[Link].simple_spinner_dropdown_item, items)
}
}
activity_main. xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<!-- TextView -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android UI Demo"
android:textSize="24sp"
android:textStyle="bold"/>
<!-- EditText -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"/>
<!-- Button -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"/>
<!-- CheckBox -->
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept Terms"/>
<!-- RadioGroup -->
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
</RadioGroup>
<!-- Spinner -->
<Spinner
android:id="@+id/sp1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- ImageView -->
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
</ScrollView>
8. Create menu in Application
Step 1: Create a new application.
Step 2: Go to resources folder, right click then select new and
click on Android Resource Directory.
Step 3: Select Menu from the drop down, click OK.
Step 4: Right click on menu (under resources folder) then click on
new and select Menu Resource file and name the file.
Now you have 3 files altogether (activity_main. xml,
menu_items. xml and MainActivity. kt )
activity_main. 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">
<TextView
android:text="Menu Demo"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
menu_items. xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="[Link]
<item android:id="@+id/about" android:title="About"/>
<item android:id="@+id/settings" android:title="Settings"/>
</menu>
[Link]:
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
override fun onCreateOptionsMenu(menu: Menu): Boolean {
[Link]([Link].menu_items, menu)
return true
override fun onOptionsItemSelected(item: MenuItem): Boolean {
[Link](this, [Link], Toast.LENGTH_SHORT).show()
return true
OUTPUT:
9. Read/write the Local data.
[Link] file:
package [Link]
import [Link]
import [Link]
import [Link].*
import [Link]
class MainActivity : AppCompatActivity() {
lateinit var editText: EditText
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
editText = findViewById([Link])
fun saveData(v: View) {
openFileOutput("[Link]", MODE_PRIVATE)
.write([Link]().toByteArray())
[Link](this, "Saved", Toast.LENGTH_SHORT).show()
fun readData(v: View) {
val data = openFileInput("[Link]").bufferedReader().readText()
[Link](data)
activity_main.xml:
<?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/editText"
android:hint="Enter text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="Save"
android:onClick="saveData"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="Read"
android:onClick="readData"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
10. Create an application to send SMS and receive
SMS
Step1: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:id="@+id/num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number" />
<EditText
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message" />
<Button
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>
Step 2: [Link]:
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
companion object {
lateinit var tvMsg: TextView
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val num = findViewById<EditText>([Link])
val msg = findViewById<EditText>([Link])
val send = findViewById<Button>([Link])
[Link](
this,
arrayOf([Link].SEND_SMS,
[Link].RECEIVE_SMS),
[Link] {
if ([Link](this,
[Link].SEND_SMS)
== PackageManager.PERMISSION_GRANTED
) {
[Link]().sendTextMessage(
[Link](),
null,
[Link](),
null,
null
)
[Link](this, "SMS Sent", Toast.LENGTH_SHORT).show()
Step 3 : create another activity (right click on App, click on new, click on
activity and select empty views activity) and name it (here I have named it
as smsReceiver)
[Link] :
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class SmsReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val bundle: Bundle? = [Link]
val pdus = bundle?.get("pdus") as Array<*>
for (p in pdus) {
val sms = [Link](p as ByteArray)
val msg = [Link]
[Link] = "Received: $msg"
}
}
Step 4: make the following changes in [Link] file :
Step 4: Run. After entering the phone number and message, you can
see a small dialog message appears stating SMS sent.
Step 5: Go to “Messages” app in the emulator and check that you have
received the SMS.
OUTPUT:
11. Create an application to send an e-mail.
activity_main.xml:
<?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/email"
android:hint="Enter Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/subject"
android:hint="Subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/message"
android:hint="Message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="Send Email"
android:onClick="sendEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
[Link]:
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
lateinit var email: EditText
lateinit var subject: EditText
lateinit var message: EditText
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
email = findViewById([Link])
subject = findViewById([Link])
message = findViewById([Link])
fun sendEmail(v: View) {
val intent = Intent(Intent.ACTION_SENDTO)
[Link] = [Link]("[Link] +
[Link]())
[Link](Intent.EXTRA_SUBJECT, [Link]())
[Link](Intent.EXTRA_TEXT, [Link]())
startActivity(intent)
OUTPUT:
12. Display Map based on the Current/given location.
Activity_main.xml
<?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/location"
android:hint="Enter Location (e.g., Bangalore)"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="Show Map"
android:onClick="showMap"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
lateinit var location: EditText
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
location = findViewById([Link])
fun showMap(v: View) {
val loc = [Link]()
val uri = [Link]("geo:0,0?q=$loc")
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
Output:
13. Create 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"
activity_main.xml:
<?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="Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/password"
android:hint="Password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="Login"
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/result"
android:text=""
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
[Link]:
package [Link]
import [Link]
import [Link].*
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
lateinit var username: EditText
lateinit var password: EditText
lateinit var result: TextView
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
username = findViewById([Link])
password = findViewById([Link])
result = findViewById([Link])
}
fun login(v: View) {
val user = [Link]()
val pass = [Link]()
if (user == "admin" && pass == "1234") {
[Link] = "Login Successful"
} else {
[Link](this, "Login Fail", Toast.LENGTH_SHORT).show()
Output: