KEMBAR78
Softcopy Android Application Development | PDF | Android (Operating System) | String (Computer Science)
0% found this document useful (0 votes)
8 views57 pages

Softcopy Android Application Development

Uploaded by

ayushbhoir321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views57 pages

Softcopy Android Application Development

Uploaded by

ayushbhoir321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Practical No 1

Aim: Write a program using Kotlin to implement control


structures and loops.
INPUT:
A) Classes in kotlin:
INPUT:

OUTPUT:
1) Multiple objects for the classes.
Input:

output:
B) Kotlin Setters and Getters.
INPUT:

OUTPUT:
C) Kotlin Constructors
INPUT:

OUTPUT:
Practical no 2
AIM: Write an android application demonstrating response to
event/user interaction for:
A. RadioButton & Group
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView"
android:layout_width="147dp"
android:layout_height="40dp"
android:layout_x="111dp"
android:layout_y="25dp"
android:text="Enter your name:"
android:textSize="18sp" />

<EditText
android:id="@+id/editTextName"
android:layout_width="127dp"
android:layout_height="56dp"
android:layout_x="112dp"
android:textStyle="bold"
android:layout_y="73dp"
android:hint="Your name" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="100dp">

</RadioGroup>

<Button
android:layout_width="103dp"
android:layout_height="wrap_content"
android:layout_x="54dp"
android:layout_y="318dp"
android:text="submit"
android:textStyle="bold" />

<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_x="182dp"
android:layout_y="316dp"
android:text="Reset" />

<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_x="50dp"
android:layout_y="200dp" />

<RadioButton
android:id="@+id/radioMale"
android:layout_width="107dp"
android:layout_height="70dp"
android:layout_x="110dp"
android:layout_y="153dp"
android:text="Male" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="112dp"
android:layout_height="76dp"
android:layout_x="110dp"
android:layout_y="233dp"
android:text="Female" />

</AbsoluteLayout>

MainActivity.kt
package com.example.myapp

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.m
ain)) { v, insets ->
val systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top,
systemBars.right, systemBars.bottom)
insets
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<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/Theme.My2PracticalB"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
tools:ignore="MissingClass">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>
OUTPUT:
B) Create an Android application to design screens using different
layout and UI including Button, EditText, Textview, RadioButton etc.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView2"
android:layout_width="247dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_x="93dp"
android:layout_y="70dp"
android:text="Login "
android:textSize="30dp"
android:textStyle="bold|italic" />

<TextView
android:id="@+id/text_name"
android:layout_width="112dp"
android:layout_height="41dp"
android:layout_x="53dp"
android:layout_y="145dp"
android:text="Name"
android:textSize="20dp" />

<EditText
android:layout_width="175dp"
android:layout_height="60dp"
android:layout_x="178dp"
android:layout_y="127dp"
android:textSize="20dp"
android:hint="Username" />

<TextView
android:layout_width="112dp"
android:layout_height="41dp"
android:layout_x="50dp"
android:layout_y="194dp"
android:text="Password" />
<EditText
android:id="@+id/text_password"
android:layout_width="176dp"
android:layout_height="55dp"
android:layout_x="178dp"
android:layout_y="187dp"
android:hint="Password"
android:password="true" />

<Button
android:layout_width="109dp"
android:layout_height="wrap_content"
android:layout_x="57dp"
android:layout_y="263dp"
android:text="submit"
android:textStyle="bold" />

<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_x="197dp"
android:layout_y="264dp"
android:text="Reset"
android:textStyle="bold" />

</AbsoluteLayout>

MainActivity.kt
package com.example.mypractical

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ListView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {


@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
val lv1 = findViewById<ListView>(R.id.lv1)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) {
v, insets ->
val systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom)
insets
}
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<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/Theme.MyPractical"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

OUTPUT:
Practical No 3
AIM: Create an simple application to create Flipper and Image
Gallery.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
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:padding="10dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="64dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_x="164dp"
android:layout_y="116dp"
android:text="0 / 0"
android:textSize="20sp" />

<ImageView
android:id="@+id/imageView"
android:layout_width="365dp"
android:layout_height="303dp"
android:layout_marginTop="4dp"
android:layout_x="13dp"
android:layout_y="151dp"
tools:srcCompat="@drawable/e1" />

<Button
android:id="@+id/btn_next"
android:layout_width="131dp"
android:layout_height="60dp"
android:layout_marginTop="76dp"
android:layout_marginEnd="48dp"
android:layout_x="40dp"
android:layout_y="510dp"
android:text="next" />

<Button
android:id="@+id/btn_back"
android:layout_width="129dp"
android:layout_height="58dp"
android:layout_marginStart="16dp"
android:layout_marginTop="76dp"
android:layout_x="238dp"
android:layout_y="516dp"
android:text="back" />

</AbsoluteLayout>

MainActivity.kt
package com.example.mygallery

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {


@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)

val imageView = findViewById<ImageView>(R.id.imageView)


val textView = findViewById<TextView>(R.id.textView)
val btn_next = findViewById<Button>(R.id.btn_next)
val btn_back = findViewById<Button>(R.id.btn_back)

val ids: Array<Int> = arrayOf(


R.drawable.e1,
R.drawable.e2,
R.drawable.e3,
R.drawable.e4,
R.drawable.e5,
R.drawable.e6
)
var counter = 0

val length = ids.size


var currentImage = counter + 1
imageView.setImageResource(ids[0])

textView.text = "$currentImage / $length"

btn_next.setOnClickListener {
counter++
if (counter >= length) {
counter = 0
currentImage = 0
}
imageView.setImageResource(ids[counter])
currentImage++
textView.text = "$currentImage / $length"
}
btn_back.setOnClickListener {
counter--
if (counter <0) {
counter = length -1
currentImage = 7
}
imageView.setImageResource(ids[counter])
currentImage--
textView.text = "$currentImage / $length"
}
}
}

Build.gradle.kts(Module:app)
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
}

android {
namespace = "com.example.mygallery"
compileSdk = 35

defaultConfig {
applicationId = "com.example.mygallery"
minSdk = 24
//noinspection EditedTargetSdkVersion
targetSdk = 35
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
}

dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.runtime)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<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/Theme.MyGallery"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

OUTPUT:
Practical no 4
Aim: Create an application to demonstrate shared Preferences.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/idEdtEmail"
android:layout_width="381dp"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:layout_x="9dp"
android:layout_y="181dp"
android:hint="@string/enter_email"
android:importantForAutofill="no"
android:inputType="textEmailAddress"
android:minHeight="48dp"
tools:ignore="DuplicateClickableBoundsCheck" />

<EditText
android:id="@+id/idEdtPassword"
android:layout_width="346dp"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtEmail"
android:layout_marginStart="50dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="10dp"
android:layout_x="12dp"
android:layout_y="270dp"
android:hint="@string/enter_password"
android:importantForAutofill="no"
android:inputType="textPassword"
android:minHeight="48dp" />

<Button
android:id="@+id/idBtnLogin"
android:layout_width="350dp"
android:layout_height="48dp"
android:layout_below="@id/idEdtPassword"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="10dp"
android:layout_x="16dp"
android:layout_y="385dp"
android:text="@string/login" />

</AbsoluteLayout>

MainActivity.kt
package com.example.sharedpreferences

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.text.TextUtils
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

// creating constant keys for shared preferences.


companion object {
const val SHARED_PREFS = "shared_prefs"
const val EMAIL_KEY = "email_key"
const val PASSWORD_KEY = "password_key"
}

// variable for shared preferences.


private lateinit var sharedpreferences: SharedPreferences
private var email: String? = null
private var password: String? = null

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Initializing EditTexts and our Button


val emailEdt = findViewById<EditText>(R.id.idEdtEmail)
val passwordEdt = findViewById<EditText>(R.id.idEdtPassword)
val loginBtn = findViewById<Button>(R.id.idBtnLogin)

// getting the data which is stored in shared preferences.


sharedpreferences = getSharedPreferences(SHARED_PREFS,
Context.MODE_PRIVATE)

// in shared prefs inside get string method


// we are passing key value as EMAIL_KEY and
// default value is
// set to null if not present.
email = sharedpreferences.getString("EMAIL_KEY", null)
password = sharedpreferences.getString("PASSWORD_KEY", null)

// calling on click listener for login button.


loginBtn.setOnClickListener {
// to check if the user fields are empty or not.
if (TextUtils.isEmpty(emailEdt.text.toString()) &&
TextUtils.isEmpty(passwordEdt.text.toString())) {
// this method will call when email and password fields are empty.
Toast.makeText(this@MainActivity, "Please Enter Email and
Password", Toast.LENGTH_SHORT).show()
} else {
val editor = sharedpreferences.edit()

// below two lines will put values for


// email and password in shared preferences.
editor.putString(EMAIL_KEY, emailEdt.text.toString())
editor.putString(PASSWORD_KEY, passwordEdt.text.toString())

// to save our data with key and value.


editor.apply()

// starting new activity.


val i = Intent(this@MainActivity, HomeActivity::class.java)
startActivity(i)
finish()
}
}
}

override fun onStart() {


super.onStart()
if (email != null && password != null) {
val i = Intent(this@MainActivity, HomeActivity::class.java)
startActivity(i)
}
}
}

activity_home.xml
Note: Create this new file.
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">

<!--Textview for displaying


user's email address-->

<!--button for logging out of the app-->

<TextView
android:id="@+id/idTVWelcome"
android:layout_width="366dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_x="10dp"
android:layout_y="287dp"
android:padding="5dp"
android:textAlignment="center"
android:textSize="20sp" />

<Button
android:id="@+id/idBtnLogout"
android:layout_width="326dp"
android:layout_height="wrap_content"
android:layout_below="@id/idTVWelcome"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_x="33dp"
android:layout_y="381dp"
android:text="@string/logout" />

</AbsoluteLayout>

HomeActivity.kt
package com.example.sharedpreferences

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class HomeActivity : AppCompatActivity() {

// creating constant keys for shared preferences.


companion object {
const val SHARED_PREFS = "shared_prefs"
const val EMAIL_KEY = "email_key"
const val PASSWORD_KEY = "password_key"
}

// variable for shared preferences.


private lateinit var sharedpreferences: SharedPreferences
private var email: String? = null

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)

// initializing our shared preferences.


sharedpreferences = getSharedPreferences(SHARED_PREFS,
Context.MODE_PRIVATE)

// getting data from shared prefs and


// storing it in our string variable.
email = sharedpreferences.getString(EMAIL_KEY, null)

// initializing our textview and button.


val welcomeTV = findViewById<TextView>(R.id.idTVWelcome)
welcomeTV.text = "Welcome $email"
val logoutBtn = findViewById<Button>(R.id.idBtnLogout)
logoutBtn.setOnClickListener {
// calling method to edit values in shared prefs.
val editor = sharedpreferences.edit()

// below line will clear


// the data in shared prefs.
editor.clear()

// below line will apply empty


// data to shared prefs.
editor.apply()

// starting main activity after


// clearing values in shared preferences.
val i = Intent(this@HomeActivity, MainActivity::class.java)
startActivity(i)
finish()
}
}
}

Strings.xml
<resources>
<string name="app_name">sharedPreferences</string>
<!--string for login button-->
<string name="login">Login</string>
<!--string for edittext hint in password-->
<string name="enter_password">Enter password</string>
<!--string for edittext hint in email-->
<string name="enter_email">Enter your Email</string>
<!--string for logout button-->
<string name="logout">Logout</string>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Sharedpreferences">

<activity android:name=".HomeActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".MainActivity" android:exported="true" />


<!-- Declare other activities here -->

</application>
</manifest>
OUTPUT:
Practical No 5
Aim: Create a media player application in android that plays audio.
Implement play, pause and loop features
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="@color/colorAccent">

<TextView
android:id="@+id/songTitle"
android:layout_width="276dp"
android:layout_height="67dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:layout_x="67dp"
android:layout_y="154dp"
android:text="Audio Player"
android:textSize="40sp"
android:textStyle="bold|italic" />

<Button
android:id="@+id/playButton"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:layout_x="15dp"
android:layout_y="259dp"
android:text="Play" />
<Button
android:id="@+id/pauseButton"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:layout_below="@id/playButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:layout_x="125dp"
android:layout_y="340dp"
android:text="Pause" />

<Button
android:id="@+id/stopButton"
android:layout_width="118dp"
android:layout_height="wrap_content"
android:layout_below="@id/pauseButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:layout_x="237dp"
android:layout_y="265dp"
android:text="Stop" />

</AbsoluteLayout>

Colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<!-- Accent color used for highlights -->
<color name="colorAccent">#03DAC5</color> <!-- Teal -->
</resources>
MainActivity.kt
package com.example.mypractical5

import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val mediaPlayer: MediaPlayer = MediaPlayer.create(applicationContext,


R.raw.music)

val bPlay: Button = findViewById(R.id.playButton)


val bPause: Button = findViewById(R.id.pauseButton)
val bStop: Button = findViewById(R.id.stopButton)

bPlay.setOnClickListener {
mediaPlayer.start()
}

bPause.setOnClickListener {
mediaPlayer.pause()
}

bStop.setOnClickListener {
mediaPlayer.stop()
mediaPlayer.prepare()
}
}
}

themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.MyPractical5"
parent="Theme.Material3.DayNight.NoActionBar">
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="Theme.MyPractical5" parent="Base.Theme.MyPractical5" />
</resources>

Note: download any mp3 audio and named it music paste it


in src make raw directory and import in it.
OUTPUT:
Practical No 6
Aim: Create an Android application to demonstrate xml based
animation.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageview"
android:layout_width="280dp"
android:layout_height="328dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:contentDescription="@string/app_name"
android:src="@drawable/e1" />
<LinearLayout
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageview"
android:layout_alignParentEnd="true"
android:layout_marginTop="59dp"
android:layout_marginEnd="0dp"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:id="@+id/BTNblink"
style="@style/TextAppearance.AppCompat.Widget.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:padding="3dp"
android:text="blink"
android:textColor="@color/white" />
<Button
android:id="@+id/BTNrotate"
style="@style/TextAppearance.AppCompat.Widget.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:padding="3dp"
android:text="clockwise"
android:textColor="@color/white" />
<Button
android:id="@+id/BTNfade"
style="@style/TextAppearance.AppCompat.Widget.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:padding="3dp"
android:text="fade"
android:textColor="@color/white" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linear1"
android:layout_alignParentEnd="true"
android:layout_marginTop="40dp"
android:layout_marginEnd="-1dp"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:id="@+id/BTNmove"
style="@style/TextAppearance.AppCompat.Widget.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:padding="3dp"
android:text="move"
android:textColor="@color/white" />
<Button
android:id="@+id/BTNslide"
style="@style/TextAppearance.AppCompat.Widget.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:padding="3dp"
android:text="slide"
android:textColor="@color/white" />
<Button
android:id="@+id/BTNzoom"
style="@style/TextAppearance.AppCompat.Widget.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:padding="3dp"
android:text="zoom"
android:textColor="@color/white" />
</LinearLayout>
<Button
android:id="@+id/BTNstop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linear2"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginRight="30dp"
android:text="Stop Animation" />
</RelativeLayout>

Note: Now make directory name anim in res folder for the
animations file And paste 1 image in drawable.
blink_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="500"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>

fade_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:repeatMode="reverse"
android:repeatCount="infinite">

<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1" />

<alpha
android:duration="1000"
android:fromAlpha="1"
android:startOffset="2000"
android:toAlpha="0" />

</set>
move_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">

<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="700"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>

rotate_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:repeatMode="reverse"
android:repeatCount="infinite">
<rotate
android:duration="6000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />

<rotate
android:duration="6000"
android:fromDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="5000"
android:toDegrees="0" />

</set>
slide_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:toXScale="1.0"
android:toYScale="0.0"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>

zoom_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >

<scale
android:interpolator="@android:anim/linear_interpolator"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="5"
android:toYScale="5"/>
</set>
OUTPUT:
Practical No 7
Aim: Create a suitable Android application to store and retrieve data
in the SQLite database.
Activity_main.xml File-
<?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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="8dp" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:autofillHints="Age"
android:inputType="number"
android:padding="8dp"
android:textColor="@android:color/background_dark" />
<Button
android:id="@+id/btnInsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="8dp"
android:text="Add data" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:id="@+id/btnRead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:padding="8dp"
android:text="Read" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="16sp"
android:textStyle="bold" />
</ScrollView>
</LinearLayout>

MainActivity.ktpackage
package com.example.sycspractical10
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
private val Nothing?.text: Any
get() {
TODO("Not yet implemented")
}
private fun Nothing?.setOnClickListener(function: () -> Unit) {
TODO("Not yet implemented")
}
private fun Any.clear() {
TODO("Not yet implemented")
}
class MainActivity : AppCompatActivity() {
private fun User(toString: String, toInt: Int): Any {
TODO("Not yet implemented")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val context = this
val db = DataBaseHandler(context)
val btnInsert = null
btnInsert.setOnClickListener {
val editTextName = null
val editTextAge = null
if (editTextName.text.toString().isNotEmpty() &&
editTextAge.text.toString().isNotEmpty()
){
val user = User(editTextName.text.toString(),
editTextAge.text.toString().toInt())
db.insertData(user)
clearField()
}
else {
Toast.makeText(context, "Please Fill All Data's", Toast.LENGTH_SHORT).show()
}
}
val btnRead = null
btnRead.setOnClickListener {
val data = db.readData()
val tvResult = null
tvResult.text = ""
for (i in 0 until data.size) {
tvResult?.append(
data[i].id.toString() + " " + data[i].name + " " + data[i].age + "
"
)
}
}
}
private fun clearField() {
val editTextName = null
editTextName.text.clear()
val editTextAge = null
editTextAge.text.clear()
}
}

Now Create a New File Named DataBaseHandler.kt and Add


the Folllowing code –
package com.example.sycspractical10
import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.widget.Toast
private val <User> User.age: String?
get() {
TODO("Not yet implemented")
}
private val <User> User.name: String?
get() {
TODO("Not yet implemented")
}
val DATABASENAME = "MY DATABASE"
val TABLENAME = "Users"
val COL_NAME = "name"
val COL_AGE = "age"
val COL_ID = "id"
class DataBaseHandler<User>(var context: Context) :
SQLiteOpenHelper(context, DATABASENAME,
null,
1) {
override fun onCreate(db: SQLiteDatabase?) {
val createTable = "CREATE TABLE " + TABLENAME + " (" + COL_ID + " INTEGER
PRIMARY KEY
AUTOINCREMENT," + COL_NAME + " VARCHAR(256)," + COL_AGE + "
INTEGER)"
db?.execSQL(createTable)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int)
{
//onCreate(db);
}
fun insertData(user: User) {
val database = this.writableDatabase
val contentValues = ContentValues()
contentValues.put(COL_NAME, user.name)
contentValues.put(COL_AGE, user.age)
val result = database.insert(TABLENAME, null, contentValues)
if (result == (0).toLong()) {
Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show()
}
else {
Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show()
}
}
@SuppressLint("Range")
fun readData(): MutableList<User> {
val list: MutableList<User> = ArrayList()
val db = this.readableDatabase
val query = "Select * from $TABLENAME"
val result = db.rawQuery(query, null)
if (result.moveToFirst()) {
do {
val user = User()
user.id = result.getString(result.getColumnIndex(COL_ID)).toInt()
user.name = result.getString(result.getColumnIndex(COL_NAME))
user.age = result.getString(result.getColumnIndex(COL_AGE)).toInt()
list.add(user)
}
while (result.moveToNext())
}
return list
}
}
OUTPUT-
Practical No 8
Aim: Create a suitable Android application to work with Firebase for
storing and manipulating data.
First of all add the the Following lines In
AndroidManifest.xml File-
<uses-permission
android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STAT
E"/>
Now After That Add The Following Code in
activity_main.xml file-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--EditText for adding employee name-->
<EditText
android:id="@+id/idEdtEmployeeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:hint="@string/enter_employee_name"
android:importantForAutofill="no"
android:inputType="textPersonName" />
<!--EditText for adding employee phone-->
<EditText
android:id="@+id/idEdtEmployeePhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtEmployeeName"
android:layout_margin="10dp"
android:hint="@string/enter_employee_phone_number"
android:importantForAutofill="no"
android:inputType="phone" />
<!--EditText for adding employee address-->
<EditText
android:id="@+id/idEdtEmployeeAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtEmployeePhoneNumber"
android:layout_margin="10dp"
android:hint="@string/enter_employee_address"
android:inputType="textPostalAddress" />
<!--Button for adding data to Firebase-->
<Button
android:id="@+id/idBtnSendData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtEmployeeAddress"
android:layout_margin="10dp"
android:text="@string/add_employee_details"
android:textAllCaps="false" />
</RelativeLayout>

Now Create a Class File Named “EmployeeInfo.kt” And Add


The Following Code to it package
com.example.firebaseapplication
class EmployeeInfo
{
var employeeName: String? = null
var employeeContactNumber: String? = null
var employeeAddress: String? = null
}

So Now add the Following Code to MainActivity.kt


Filepackage com.example.firebaseapplication
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
class MainActivity : AppCompatActivity() {
// creating variables for
// EditText and buttons.
private var employeeNameEdt: EditText? = null
private var employeePhoneEdt: EditText? = null
private var employeeAddressEdt: EditText? = null
private var sendDatabtn: Button? = null
// creating a variable for our
// Firebase Database.
var firebaseDatabase: FirebaseDatabase? = null
// creating a variable for our Database
// Reference for Firebase.
var databaseReference: DatabaseReference? = null
// creating a variable for
// our object class
var employeeInfo: EmployeeInfo? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initializing our edittext and button
employeeNameEdt = findViewById(R.id.idEdtEmployeeName)
employeePhoneEdt = findViewById(R.id.idEdtEmployeePhoneNumber)
employeeAddressEdt = findViewById(R.id.idEdtEmployeeAddress)
// below line is used to get the// instance of our FIrebase database.
firebaseDatabase = FirebaseDatabase.getInstance()
// below line is used to get reference for our database.
databaseReference = firebaseDatabase.getReference("EmployeeInfo")
// initializing our object
// class variable.
employeeInfo = EmployeeInfo()
sendDatabtn = findViewById(R.id.idBtnSendData)
// adding on click listener for our button.
this.sendDatabtn.setOnClickListener(View.OnClickListener {
// getting text from our edittext fields.
val name = this.employeeNameEdt.getText().toString()
val phone = this.employeePhoneEdt.getText().toString()
val address = this.employeeAddressEdt.getText().toString()
// below line is for checking whether the
// edittext fields are empty or not.
if (TextUtils.isEmpty(name) && TextUtils.isEmpty(phone) &&
TextUtils.isEmpty(address)) {
// if the text fields are empty
// then show the below message.
Toast.makeText(this@MainActivity, "Please add some data.",
Toast.LENGTH_SHORT)
.show()
} else {
// else call the method to add
// data to our database.
addDatatoFirebase(name, phone, address)
}
})
}
private fun addDatatoFirebase(name: String, phone: String, address: String) {
// below 3 lines of code is used to set
// data in our object class.
employeeInfo!!.employeeName = name
employeeInfo!!.employeeContactNumber = phone
employeeInfo!!.employeeAddress = address
// we are use add value event listener method
// which is called with database reference.
databaseReference.addValueEventListener(object : ValueEventListener() {
fun onDataChange(snapshot: DataSnapshot) {
// inside the method of on Data change we are setting
// our object class to our database reference.
// data base reference will sends data to firebase.
databaseReference.setValue(employeeInfo!!)
// after adding this data we are showing toast message.
Toast.makeText(this@MainActivity, "data added",
Toast.LENGTH_SHORT).show()
}
fun onCancelled(error: DatabaseError) {
// if the data is not added or it is cancelled then
// we are displaying a failure toast message.
Toast.makeText(this@MainActivity, "Fail to add data $error",
Toast.LENGTH_SHORT)
.show()
}
})
}

OUTPUT:

You might also like