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

Android Programing Lab

The document provides instructions for creating two Android applications: one that displays 'Hello World' on the screen and another that shows a toast message with the same text. It includes the necessary Kotlin code for the MainActivity and the XML layout files for both applications. Additionally, it outlines steps to set up the project in Android Studio and run the app on an emulator or device.
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)
2 views4 pages

Android Programing Lab

The document provides instructions for creating two Android applications: one that displays 'Hello World' on the screen and another that shows a toast message with the same text. It includes the necessary Kotlin code for the MainActivity and the XML layout files for both applications. Additionally, it outlines steps to set up the project in Android Studio and run the app on an emulator or device.
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

Android Programming Lab

1. Create an android application to display “Hello World”?

[Link]

package [Link]

import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
}
}

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="Hello World"
android:textSize="24sp" />
</LinearLayout>
2. Create an android application to display toast message as "Hello World"?

Option 1: Show Toast on App Launch


[Link]

package [Link]

import [Link]
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)

[Link](this, "Hello World", Toast.LENGTH_SHORT).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">

</LinearLayout>
Option 2: Show Toast on Button Click
[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 btnShowToast = findViewById<Button>([Link])


[Link] {
[Link](this, "Hello World", Toast.LENGTH_SHORT).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/btnShowToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast" />

</LinearLayout>

 Open Android Studio

 Create a new Empty Activity project

 Replace the default code with the files above

 Run the app on an emulator or physical device

You might also like