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

Android Development Basics and Lifecycle

The document outlines practical exercises for learning Android development, covering topics such as project creation, Android components, activity lifecycle, and various layout types. It includes code snippets for creating a simple 'Hello World' application, managing activities, and implementing different layouts like Linear, Relative, and Table layouts. Additionally, it provides examples of handling user interactions and displaying lists in Android applications.

Uploaded by

nandinipatil3421
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 views27 pages

Android Development Basics and Lifecycle

The document outlines practical exercises for learning Android development, covering topics such as project creation, Android components, activity lifecycle, and various layout types. It includes code snippets for creating a simple 'Hello World' application, managing activities, and implementing different layouts like Linear, Relative, and Table layouts. Additionally, it provides examples of handling user interactions and displaying lists in Android applications.

Uploaded by

nandinipatil3421
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

Practical 1

Aim: Introduction to Android, Introduction to Android Studio IDE, Application


Fundamentals:
Creating a Project, Android Components, Activities, Services, Content Providers,
Broadcast Receivers, Interface overview, Creating Android Virtual device, USB
debugging mode, Android Application Overview. Simple “Hello World” program.
Solution:
Creating a project
Activity_Main.kt

Activity_Main.xml
Create Virtual Device :

Choose Devices for AVD:


Select Android Version :
Output :
Practical 2
Programming Resources
Aim: Android Resources: (Color, Theme , String, Drawable, Dimension, Image).
Code:

Output:-
Practical 3
Programming Activities and fragments
Activity Life Cycle, Activity methods, Multiple Activities, Life Cycle of fragments and
multiple fragments.
Activity Lifecycle:

onCreate(): Called by the OS when the activity is first created.


This is where you
initialize any UI elements or data objects. You also have the
savedInstanceState of the
activity that contains its previously saved state, and you can use
it to recreate that state.
onStart(): Just before presenting the user with an activity, this
method is called. It’s
always followed by onResume(). In here, you generally should
start UI animations, audio
based content or anything else that requires the activity’s
contents to be on screen.
onResume(): As an activity enters the foreground, this method is
called. Here you have a
good place to restart animations, update UI elements, restart
camera previews, resume
audio/video playback or initialize any components that you
release during onPause().
 onPause(): This method is called before sliding into the
background. Here you should
stop any visuals or audio associated with the activity such as UI
animations, music
playback or the camera. This method is followed by onResume() if
the activity returns to
the foreground or by onStop() if it becomes hidden.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />-->

<Button
android:id="@+id/About_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:text="AboutUs" />
</RelativeLayout>

[Link]
package [Link]

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

class MainActivity : AppCompatActivity() {


val tag="Main Activity"
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
enableEdgeToEdge()
setContentView([Link].activity_main)
Log.d(tag, "In onCreate")
val About_us = findViewById<Button>([Link].About_us)
About_us.setOnClickListener {
val i = Intent(this, AboutUs::[Link])
startActivity(i)
}
[Link](findViewById([Link])) { v, insets ->
val systemBars = [Link]([Link]())
[Link]([Link], [Link], [Link],
[Link])
insets
}
}

override fun onStart() {


[Link]()
Log.d(tag, "In onStart")
}

override fun onStop() {


[Link]()
Log.d(tag, "In onStop")
}

override fun onPause() {


[Link]()
Log.d(tag, "In onPause")
}

override fun onDestroy() {


[Link]()
Log.d(tag, "In onDestroy")
}

override fun onRestart() {


[Link]()
Log.d(tag, "In onRestart")
}

override fun onResume() {


[Link]()
Log.d(tag, "In onResume")
}
}

[Link]
package [Link]

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

class AboutUs:AppCompatActivity() {
val tag= "About Us"
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
enableEdgeToEdge()
//setContentView([Link].activity_main)
Log.d(tag, "Inside onCreate")
[Link](this, "You are under about us", Toast.LENGTH_LONG).show()
}

override fun onStart() {


[Link]()
Log.d(tag, "In onStart")
}

override fun onStop() {


[Link]()
Log.d(tag, "In onStop")
}

override fun onPause() {


[Link]()
Log.d(tag, "In onPause")
}

override fun onDestroy() {


[Link]()
Log.d(tag, "In onDestroy")
}

override fun onRestart() {


[Link]()
Log.d(tag, "In onRestart")
}

override fun onResume() {


[Link]()
Log.d(tag, "In onResume")
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />

<category android:name="[Link]" />


</intent-filter>
</activity>
<activity android:name=".AboutUs"> </activity>
</application>
</manifest>

Output:
PRACTICAL 4

Programs related of different Layouts


Coordinate, Linear, Relative, Table, Absolute, Frame, List View, Grid View
1. Linear Layout:
[Link] : Vertical
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/btn1"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_Service"
/>

<Button
android:id="@+id/btn2"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"
/>

<Button
android:id="@+id/btn3"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"
/>

</LinearLayout>

Output:-
b. orientation=horizontal:
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start_service"
/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause_service"
/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop_service"
/>
</LinearLayout>

Output:

[Link]:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/etReminder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="reminder" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/etReminder"
android:layout_alignParentStart="true">

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />

<Button
android:id="@+id/btn12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button2" />

</LinearLayout>
</RelativeLayout>

Output:-
[Link]:-
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="150dp"
>

<TableRow>
<Button
android:id="@+id/btn1"
android:text="1"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn2"
android:text="2"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn3"
android:text="3"
android:layout_gravity="center"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn4"
android:text="4"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn5"
android:text="5"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn6"
android:text="6"
android:layout_gravity="center"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn7"
android:text="7"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn8"
android:text="8"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn9"
android:text="9"
android:layout_gravity="center"
/>
</TableRow>
</TableLayout>
</LinearLayout>]

Activity_main.kt:
package [Link].practical4

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

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
val binding = [Link](layoutInflater)
setContentView([Link])
[Link] { [Link]( this,"1",
Toast.LENGTH_SHORT).show() }
[Link] { [Link]( this,"2",
Toast.LENGTH_SHORT).show() }
[Link] { [Link]( this,"3",
Toast.LENGTH_SHORT).show() }
[Link] { [Link]( this,"4",
Toast.LENGTH_SHORT).show() }
[Link] { [Link]( this,"5",
Toast.LENGTH_SHORT).show() }
[Link] { [Link]( this,"6",
Toast.LENGTH_SHORT).show() }
[Link] { [Link]( this,"7",
Toast.LENGTH_SHORT).show() }
[Link] { [Link]( this,"8",
Toast.LENGTH_SHORT).show() }
[Link] { [Link]( this,"9",
Toast.LENGTH_SHORT).show() }
}
}

Output:

[Link]:
Activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/nature"
android:scaleType="centerCrop"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="100sp"
android:text="Hello World!"
android:gravity="center"
android:textColor="@color/Orange"
android:layout_marginTop="220dp"
/>
</FrameLayout>

Output:
[Link] View:
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to view list"
android:layout_marginTop="200dp"
android:layout_marginLeft="90dp"
/>
</LinearLayout>

Activity_main.kt:
package [Link].practical4

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

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
val binding = [Link](layoutInflater)
setContentView([Link])
[Link] {
val listScreen = Intent(this, Activity_List_Screen::[Link])
startActivity(listScreen)
}
}
}

Activity_List_Screen.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="@array/insert_list"
tools:context=".Activity_List_Screen">
</ListView>
Activity_List_Screen.kt:

package [Link].practical4

import [Link]
import [Link]

class Activity_List_Screen : AppCompatActivity() {


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

[Link]:
<resources>
<string name="app_name">Practical4</string>

<array name="insert_list">
<item>One</item>
<item>Two</item>
<item>Three</item>
<item>Four</item>
<item>Five</item>
<item>Six</item>
<item>Seven</item>
<item>Eight</item>
<item>Nine</item>
<item>Ten</item>
</array>
</resources>

Output:-
6. Grid layout:
Activity_Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="3"
android:padding="50dp"
tools:context=".MainActivity">

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
/>
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
/>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
/>
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
/>
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
/>
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
/>
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
/>

</GridLayout>

[Link]:

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)

}
}

Output:

You might also like