Nafis Andriod - Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

AMITY INSTITUTE OF

INFORMATION TECHNOLOGY
(AIIT)

Name: - Nafis Parwez


Enrolment No.: - A710145023050
Course: - MCA SEM I

ANDROID PROGRAMMING LAB


REPORT

Name :- Nafis Parwez


Enrollment No. :- A710145023050
Course :- MCA SEM I
subject:-Android Programming

3
AMITY UNIVERSITY MAHARASHTRA
Established vide Maharashtra Act No. 13 of 20 14, of Government of Maharashtra,
and recogized under Section 2(t) of UGC Act 1956

CERTIFICATE

This is to certify that Mr.Nafis Parwez Enrolment No.


A710145023050 of class MCA, Semester I has satisfactorily
completed the Android Programming lab Manual prescribed
by Amity University Maharashtra during the academic year
20232024.

Sign of Faculty Sign of Dept. Coordinator

Name: Dr. Manoj Devare Name:

Index
Index Description Page no

4
1. Installation of the the Android studio, ogram 7

Creating Android Vitual Device and Hello World pr


using Java progamming. o,

(Explain all steps , each window of Android Studi

various resources for android app.)

2. Create a Android app using Java programming with a 9

simple button to handle the Event using onclick(). On


click of button

the text of the TextView Should Change. (Date: 15


September 2023)

For each assignment take screenshots and text of t he


.Java file,

XML file of the Layouts, and AndroidManifest.xm l


file, if applicable.

3. Create a android app with three edit text and a but 12


ton. Display the sum of the two numbers accepted in
edit text 1 and edit text 2 into the edit text3.

4. Create a Android app for arithmetic Calculator 16


Operations.

Navigating from one Activity to Another using Intent, 21

5. and PutExtra(), getStringExtra().

6. Navigating Activities - Swapping Text of Two


Text

5
Boxes and Changing Colors 26

7. Firebase in Android 30

8. Services in Android 33

9. Define Sensors, Bluetooth Accessi ng and 37


Programming

10. Define APK Development Process 40

11. Audio File Playing using MediaPla yer 41

12. SQLite Database Operations in An droid 45

13. Alert Dialog in Android 53

6
PRACTICAL – 1

7
8
PRACTICAL – 2

MainActivity.java import android.os.Bundle;

import android.view.View; import

android.widget.Button; import

android.widget.TextView; import

androidx.appcompat.app.AppCompatActivity; public

class MainActivity extends AppCompatActivity {

private TextView textView; private

Button button;

9
@Override protected void onCreate(Bundle savedInstanceState)

{ super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); textView

= findViewById(R.id.textView); button

= findViewById(R.id.button); button.setOnClickListener(new

View.OnClickListener() {

@Override public void

onClick(View view) { // Change

the text on button click

textView.setText("Text Changed

on

" + getCurrentDate());

});

} private String getCurrentDate()

// You can implement your logic to get the current date here

// For simplicity, returning a static date return

"15 September 2023";

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"

10
android:layout_width="match_parent" android:layout_height="match_parent"

android:paddingLeft="16dp" android:paddingTop="16dp"

android:paddingRight="16dp" android:paddingBottom="16dp"

tools:context=".MainActivity"> <TextView android:id="@+id/textView"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="Hello World!" android:textSize="18sp"

android:layout_centerHorizontal="true" android:layout_marginBottom="16dp"/>

<Button android:id="@+id/button" android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Change Text" android:layout_below="@id/textView"

android:layout_centerHorizontal="true"/>

</RelativeLayout>

11
PRACTICAL – 3

MainActivity.java
MainActivity.java package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity; import

android.os.Bundle; import android.text.Editable; import

12
android.view.View; import android.widget.Button; import

android.widget.EditText; import android.widget.TextView;

public class

MainActivity extends AppCompatActivity { @Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Button b1=(Button)findViewById(R.id.button1); Button

b1=(Button)findViewById(R.id.button1);

b1.setOnClickListener(new View.OnClickListener(){

@Override public void

onClick(View view) {

// TextView t1=(TextView)findViewById(R.id.text1);

// t1.setText(&quot;You clicked on Button&quot;);

EditText e1=(EditText)findViewById(R.id.editText1);

EditText e2=(EditText)findViewById(R.id.editText2);

EditText e3=(EditText)findViewById(R.id.editText3);

Editable str1 =e1.getText(); Editable str2 =e2.getText(); int a=

Integer.valueOf(str1.toString()); int b=

Integer.valueOf(str2.toString()); int sum;

sum = a+b;

e3.setText(String.valueOf(sum));

});

13
Button b2=(Button)findViewById(R.id.button2);

b2.setOnClickListener (new View.OnClickListener (){

@Override public void

onClick(View view) {

// TextView t1=(TextView)findViewById(R.id.text1);

// t1.setText(&quot;You clicked on Button&quot;);

EditText e1=(EditText)findViewById(R.id.editText1);

EditText e2=(EditText)findViewById(R.id.editText2);

EditText e3=(EditText)findViewById(R.id.editText3);

Editable str1 =e1.getText(); Editable str2 =e2.getText(); int a=

Integer.valueOf(str1.toString()); int b=

Integer.valueOf(str2.toString()); int mul;

mul = a*b;

e3.setText(String.valueOf(mul));

});

mainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout
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" tools:context=".MainActivity"> <Button
android:id="@+id/button1" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" android:layout_marginTop="423dp"
android:layout_marginBottom="260dp" android:text="Add"
tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="140dp"
14
tools:layout_editor_absoluteY="355dp" /> <Button android:id="@+id/button2"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_alignParentBottom="true"
android:layout_marginTop="495dp" android:layout_marginBottom="188dp"
android:text="Multiply" tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="140dp" tools:layout_editor_absoluteY="355dp" /> <EditText
android:id="@+id/editText1" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_marginTop="50dp" android:ems="10"
android:hint="Enter 1st Number" android:inputType="text" android:textSize="24sp"
tools:layout_editor_absoluteX="128dp" tools:layout_editor_absoluteY="94dp" /> <EditText
android:id="@+id/editText2" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_marginTop="100dp" android:ems="10"
android:hint="Enter 2nd Number" android:inputType="text" android:textSize="24sp"
tools:layout_editor_absoluteX="131dp" tools:layout_editor_absoluteY="171dp" /> <EditText
android:id="@+id/editText3" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_marginTop="224dp" android:ems="10" android:inputType="text"
android:textSize="50dp" tools:layout_editor_absoluteX="127dp"
tools:layout_editor_absoluteY="461dp" /> <TextView android:id="@+id/textView2"
android:layout_width="143dp" android:layout_height="47dp"
android:layout_alignParentStart="true" android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" android:layout_marginStart="14dp"
android:layout_marginTop="185dp" android:layout_marginEnd="253dp"
android:text="Result" android:textColor="#AF1616" android:textSize="24sp"
android:textStyle="bold" /> </RelativeLayout>

15
PRACTICAL – 4

16
Xml code

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;

xmlns:tools=&quot;http://schemas.android.com/tools&quot;

android:layout_width=&quot;match_parent&quot;

android:layout_height=&quot;match_parent&quot; android:orientation=&quot;vertical&quot;

android:padding=&quot;16dp&quot; tools:context=&quot;.MainActivity&quot;&gt;

&lt;EditText android:id=&quot;@+id/operand1EditText&quot;

android:layout_width=&quot;match_parent&quot;

android:layout_height=&quot;wrap_content&quot;

android:inputType=&quot;numberDecimal&quot; android:hint=&quot;Operand

1&quot; /&gt;

&lt;EditText android:id=&quot;@+id/operand2EditText&quot;

android:layout_width=&quot;match_parent&quot;

android:layout_height=&quot;wrap_content&quot;

android:inputType=&quot;numberDecimal&quot; android:hint=&quot;Operand

2&quot; /&gt; &lt;TextView

android:id=&quot;@+id/resultTextView&quot;

android:layout_width=&quot;wrap_content&quot;

android:layout_height=&quot;wrap_content&quot;

android:text=&quot;Result: &quot;

android:textSize=&quot;18sp&quot;

android:paddingTop=&quot;16dp&quot; /&gt; &lt;Button

android:id=&quot;@+id/addButton&quot;

android:layout_width=&quot;wrap_content&quot;

android:layout_height=&quot;wrap_content&quot;

17
android:text=&quot;Addition&quot; /&gt; &lt;Button

android:id=&quot;@+id/subtractButton&quot;

android:layout_width=&quot;wrap_content&quot;

android:layout_height=&quot;wrap_content&quot;

android:text=&quot;Subtraction&quot; /&gt; &lt;Button

android:id=&quot;@+id/multiplyButton&quot;

android:layout_width=&quot;wrap_content&quot;

android:layout_height=&quot;wrap_content&quot;

android:text=&quot;Multiplication&quot; /&gt;

&lt;Button android:id=&quot;@+id/divideButton&quot;

android:layout_width=&quot;wrap_content&quot;

android:layout_height=&quot;wrap_content&quot;

android:text=&quot;Division&quot; /&gt; Java

import android.os.Bundle; import android.view.View;


import android.widget.Button; import
android.widget.EditText; import
android.widget.TextView; import
androidx.appcompat.app.AppCompatActivity; public class
MainActivity extends AppCompatActivity { private
EditText operand1EditText, operand2EditText; private
TextView resultTextView; @Override protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
operand1EditText =
findViewById(R.id.operand1EditText);
operand2EditText =
findViewById(R.id.operand2EditText);

18
resultTextView = findViewById(R.id.resultTextView);

Button addButton = findViewById(R.id.addButton); Button

subtractButton = findViewById(R.id.subtractButton);

Button multiplyButton = findViewById(R.id.multiplyButton); Button divideButton

= findViewById(R.id.divideButton); addButton.setOnClickListener(new

View.OnClickListener() {

@Override public void onClick(View v) { performOperation(&#39;+&#39;);

});

subtractButton.setOnClickListener(new View.OnClickListener() {

@Override public void

onClick(View v) {

performOperation(&#39;-&#39;);

});

multiplyButton.setOnClickListener(new View.OnClickListener() { @Override public void

onClick(View v) { performOperation(&#39;*&#39;);

});

divideButton.setOnClickListener(new View.OnClickListener() {

@Override public void

onClick(View v) {

performOperation(&#39;/&#39;);

19
}); } private void performOperation(char operator)

{ try { double operand1 =

Double.parseDouble(operand1EditText.getText

().toString()); double operand2 =

Double.parseDouble(operand2EditText.getText

().toString()); double result = 0; switch

(operator) { case &#39;+&#39;:

result = operand1 + operand2;

break; case &#39;-&#39;: result

= operand1 - operand2; break;

case &#39;*&#39;: result =

operand1 * operand2; break; case

&#39;/&#39;: if(operand2 != 0)

{ result = operand1 / operand2;

} else { resultTextView.setText(&quot;Cannot divide by

zero&quot;);return; } break; default: break; }

resultTextView.setText(&quot;Result: &quot; + result);

} catch (NumberFormatException e) {

resultTextView.setText(&quot;Please enter valid

numbers.&quot;);

20
PRACTICAL – 5
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schem <?
as.android.com/tools" 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:id="@+id/tv1" android:text="Explicit Intent
- Returning Data" android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" android:textSize="25dp"
android:textColor="@color/black"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
21
android:id="@+id/tv2" android:text="Activity 1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" android:layout_below="@id/tv1"
android:textSize="40dp" android:textColor="@color/black"
/>
<Button
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/tv2"
android:id="@+id/b1" android:text="Go to
the second Layout"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp" android:textSize="30dp"
/>

</RelativeLayout>

MainActivity.java package com.example.ass2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog; import


android.content.Intent; import android.os.Bundle;
import android.view.View; import
android.widget.AdapterView; import
android.widget.ArrayAdapter; import
android.widget.Button; import
android.widget.ListView; import
android.widget.SimpleAdapter; import
android.widget.Spinner; import
android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import
java.util.SimpleTimeZone;

public class MainActivity extends AppCompatActivity { private


final static int REQUEST_CODE = 1; int requestValue = 1;

@Override protected void onCreate(Bundle


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

Button b1 = (Button) findViewById(R.id.b1); b1.setOnClickListener(new


View.OnClickListener() {

22
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, MainActivity2.class);
startActivityForResult(i, REQUEST_CODE);
}
});
}
@Override protected void onActivityResult(int requestCode ,
int resultCode ,
Intent data)
{
super.onActivityResult(requestCode , resultCode , data); if(requestCode ==
requestValue)
{
if(resultCode == RESULT_OK)
{
Toast.makeText(MainActivity.this, data.getData().toString(),
Toast.LENGTH_LONG).show();
}

}
}
}

Activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity2">

<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/tv3"
android:text="Explicit Intent - Returning Data"
android:layout_centerHorizontal="true" android:layout_marginTop="50dp"
android:textSize="25dp" android:textColor="@color/black"
/>

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv4" android:text="Activity 2"
android:layout_centerHorizontal="true"

23
android:layout_marginTop="50dp"
android:layout_below="@id/tv3" android:textSize="40dp"
android:textColor="@color/black"
/>

<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/et1"
android:hint="Enter
the Text" android:textSize="30dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/tv4" android:layout_marginTop="40dp"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et1"
android:id="@+id/b2" android:text="Go to the
first Layout" android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:textSize="30dp"
/>

</RelativeLayout>

MainActivity2.java package com.example.ass2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent; import


android.net.Uri; import
android.os.Bundle;
import android.view.View;
import android.widget.Button;
import
android.widget.EditText;

public class MainActivity2 extends AppCompatActivity {

@Override protected void onCreate(Bundle


savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); Button b2 = (Button)
findViewById(R.id.b2); b2.setOnClickListener(new View.OnClickListener()
{

24
@Override public void onClick(View
view) {
Intent data = new Intent();
EditText ed = (EditText) findViewById(R.id.et1);
data.setData(Uri.parse(ed.getText().toString())); setResult(RESULT_OK,data);
finish();
}
});
}
}

Output:

25
PRACTICAL – 6

import android.content.Intent; import android.os.Bundle;


import android.view.View; import
android.widget.Button; import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editText1, editText2; private Button
swapButton;
@Override protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); editText1 =
findViewById(R.id.editText1); editText2 =
findViewById(R.id.editText2); swapButton =
findViewById(R.id.swapButton);
swapButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View
view) {
// Swap text
String temp = editText1.getText().toString();
editText1.setText(editText2.getText().toString());
editText2.setText(temp);
// Change background colors int
color1 = getRandomColor(); int color2 =
getRandomColor();
editText1.setBackgroundColor(color1);
editText2.setBackgroundColor(color2);
}

26
});
}
private int getRandomColor() {
// You can implement your logic to get random colors here
// For simplicity, returning a static color return
getResources().getColor(R.color.colorAccent);
}
public void goToSecondActivity(View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
SecondActivity.java import
android.os.Bundle; import
android.view.View;
import android.widget.Button; import
androidx.appcompat.app.AppCompatActivity; public
class SecondActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button backButton = findViewById(R.id.backButton);
backButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
finish(); // Finish the current activity and go back to the previous one
}
});
}
}
Activitymain.xml
<!-- Similar layout for activity_second.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"
android:paddingLeft="16dp" android:paddingTop="16dp"
android:paddingRight="16dp" android:paddingBottom="16dp"
tools:context=".MainActivity">
27
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text Box 1"/>

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editText1"
android:layout_marginTop="16dp"
android:hint="Text Box 2"/>

<Button
android:id="@+id/swapButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Swap
Text and Change Colors"
android:layout_below="@id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>

<Button
android:id="@+id/goToSecondButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Go
to Second Activity"
android:layout_below="@id/swapButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:onClick="goToSecondActivity"/>

28
PRACTICAL – 7

29
30
31
PRACTICAL – 8
<?xml version="1.0" encoding="utf-8"?>
32
<RelativeLayout 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"

tools:context=".MainActivity">

<TextView android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Implicit Intent"

android:layout_centerHorizontal="true"

android:layout_marginTop="50dp"

android:id="@+id/tv"

/>

<Button android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="CALL"

android:layout_centerHorizontal="true"

android:layout_marginTop="50dp"

android:id="@+id/b1" android:layout_below="@id/tv"

android:textColor="@color/teal_200"

/> <Button android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="WEB BROWSER"

android:layout_centerHorizontal="true"

android:layout_marginTop="50dp"

33
android:id="@+id/b2" android:layout_below="@id/b1"

android:textColor="@color/teal_200" /> <Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="MAP"

android:layout_centerHorizontal="true"

android:layout_marginTop="50dp"

android:id="@+id/b3" android:layout_below="@id/b2"

/>

</RelativeLayout>

MainActivity.java package com.example.implicitintent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent; import android.net.Uri;

import android.os.Bundle; import android.view.View;

import android.widget.Button; public class MainActivity

extends AppCompatActivity {

@Override protected void onCreate(Bundle

savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); Button bb1 =

(Button) findViewById(R.id.b1); Button bb2 = (Button)

findViewById(R.id.b2); Button bb3 =

(Button) findViewById(R.id.b3);

bb1.setOnClickListener(new View.OnClickListener() {

34
@Override public void onClick(View

view) {

Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel: +7498112598"));

startActivity(i);

});

bb2.setOnClickListener(new View.OnClickListener() {

@Override public voidNnClick(View

view) {

Intent i = Intent(Intent.ACTION_VIEW,Uri.parse("https://www.google.com/"));

startActivity(i);

});

bb3.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View

view) { Intent i = new

Intent(Intent.ACTION_VIEW,

Uri.parse("geo:32.23543.-

143.5468"));; startActivity(i);

});

Output:

35
PRACTICAL – 9

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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"

36
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="List of Available Sensors"

android:id="@+id/tv1"

android:layout_centerHorizontal="true"

android:layout_marginTop="50dp"

android:textStyle="bold"

/>

<TextView android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Sensors" android:id="@+id/tv2"

android:layout_centerHorizontal="true"

android:layout_marginTop="50dp"

android:layout_below="@id/tv1"

/>

</RelativeLayout> MainActivity.java package

com.example.sensors; import

androidx.appcompat.app.AppCompatActivity; import

android.hardware.Sensor; import

android.hardware.SensorManager; import

android.os.Bundle; import android.widget.TextView;

import android.widget.Toast; import java.util.List; public

class MainActivity extends AppCompatActivity {


37
SensorManager sMgr; @Override protected void

onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView tvv = (TextView) findViewById(R.id.tv2); sMgr =

(SensorManager) this.getSystemService(SENSOR_SERVICE); List<Sensor>

list = sMgr.getSensorList(Sensor.TYPE_ALL);

for(int i=1; i<list.size(); i++)

tvv.append("\n" + list.get(i).getName() + "\n" + list.get(i).getVendor() + "\n" +


list.get(i).getVersion());

Toast.makeText(MainActivity.this, "List of Sensors Displayed",


Toast.LENGTH_SHORT).show();

38
PRACTICAL – 10

39
PRACTICAL – 11

40
MainActivity.java import
android.media.MediaPlayer; import
android.os.Bundle; import android.view.View;
import android.widget.Button; import
androidx.appcompat.app.AppCompatActivity; public
class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer; private Button
playButton, pauseButton, stopButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, R.raw.your_audio_file);
// Replace with your audio file playButton =
findViewById(R.id.playButton); pauseButton =
findViewById(R.id.pauseButton); stopButton =
findViewById(R.id.stopButton);
playButton.setOnClickListener(new View.OnClickListener() {
@Override public void
onClick(View view) {
playAudio();
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override public void
onClick(View view) {
pauseAudio();
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override public void
onClick(View view) {
stopAudio();
}
});
} private void playAudio()
{
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();

41
} } private void
pauseAudio() { if
(mediaPlayer.isPlaying()) {
mediaPlayer.pause(); }
} private void stopAudio()
{
if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer
= MediaPlayer.create(this, R.raw.your_audio_file); // Reset MediaPlayer
}
}
@Override protected void
onDestroy() { super.onDestroy();
if (mediaPlayer
!= null) {
mediaPlayer.release();
}
}
}
Activitymain.cml
<?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"
android:paddingLeft="16dp" android:paddingTop="16dp"
android:paddingRight="16dp" android:paddingBottom="16dp"
tools:context=".MainActivity">

<Button android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>
<Button android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_below="@id/playButton"

42
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>
<Button android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_below="@id/pauseButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>
</RelativeLayout>

43
PRACTICAL – 12

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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"

tools:context=".MainActivity">
44
<ImageView android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/amity"

android:layout_centerHorizontal="true"

android:id="@+id/img1"

android:layout_marginTop="50dp"

/> <EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="Employee Name"

android:layout_marginTop="50dp"

android:layout_below="@id/img1"

android:textSize="20dp" android:id="@+id/ed1"

/>

<EditText android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="Employee Number"

android:layout_marginTop="30dp"

android:layout_below="@id/ed1"

android:textSize="20dp" android:id="@+id/ed2"

/>

<EditText android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="Employee Department"

45
android:layout_marginTop="30dp"

android:layout_below="@id/ed2"

android:textSize="20dp" android:id="@+id/ed3"

/>

<Button android:id="@+id/b1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/ed3"

android:layout_centerHorizontal="true"

android:layout_marginTop="30dp"

android:backgroundTint="@color/teal_200"

android:text="Save Button" android:textSize="20dp" />

<Button android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Display"

android:layout_marginTop="10dp"

android:layout_below="@id/b1"

android:layout_centerHorizontal="true"

android:textSize="20dp" android:id="@+id/b2"

android:backgroundTint="@color/teal_200"

/>

<TextView android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Employee Name"

android:layout_below="@id/b2"
46
android:layout_marginTop="20dp"

android:id="@+id/tv2"

/>

<TextView android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Employee Number"

android:layout_below="@id/tv2"

android:id="@+id/tv3"

/>

<TextView android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Employee Department"

android:layout_below="@id/tv3"

android:id="@+id/tv4"

/>

</RelativeLayout> MainActivity.java package

com.example.db; import

androidx.appcompat.app.AppCompatActivity; import

android.content.ContentValues;

import android.database.Cursor; import

android.database.sqlite.SQLiteDatabase; import

android.os.Bundle; import android.view.View; import

android.widget.Button; import android.widget.EditText;

import android.widget.TextView; import

47
android.widget.Toast; public class MainActivity extends

AppCompatActivity { private EditText edn; private

EditText edno; private EditText edd; private TextView

tv2; private TextView tv3; private TextView tv4; private

Button b1; private Button b2;

SQLiteDatabase db1; @Override protected void

onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); db objdatabase =

new db(this); db1 = objdatabase.getWritableDatabase();

setContentView(R.layout.activity_main); edn =

(EditText) findViewById(R.id.ed1); edno = (EditText)

findViewById(R.id.ed2); edd = (EditText)

findViewById(R.id.ed3); tv2 = (TextView)

findViewById(R.id.tv2); tv3 = (TextView)

findViewById(R.id.tv3); tv4 = (TextView)

findViewById(R.id.tv4); b1 = (Button)

findViewById(R.id.b1); b2 = (Button)

findViewById(R.id.b2); b1.setOnClickListener(new

View.OnClickListener() { @Override public void

onClick(View view) { registerUser();

});

b2.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View

view) {
48
SQLiteDatabase db = openOrCreateDatabase("table", MODE_PRIVATE, null);

String selectQuery = "SELECT * FROM userdetails2";

Cursor cursor = db.rawQuery(selectQuery, null); if

(cursor !=null){ cursor.moveToFirst();

startManagingCursor(cursor); while

(!cursor.isAfterLast()) {

String n = cursor.getString(1);

String no = cursor.getString(2);

String de = cursor.getString(3);

String Observations1 = "Name of the Employee: " + n;

String Observations2 = "Number of the Employee: " + no; String Observations3

= "Department of the Employee: " + de; tv2.setText(Observations1);

tv3.setText(Observations2); tv4.setText(Observations3);

Toast.makeText(getApplicationContext(), "Data is Displayed of ",


Toast.LENGTH_SHORT).show();
cursor.moveToNext();

}); } public void registerUser(){ String

empname, empno, empdept; db objdatabase =

new db(this); empname =

edn.getText().toString(); empno =

edno.getText().toString(); empdept =

49
edd.getText().toString(); ContentValues

values = new ContentValues();

values.put("Name", empname); values.put("Empno",


empno); values.put("Dept", empdept); long ID =
db1.insert("userdetails2", null, values);
Toast.makeText(MainActivity.this, "No. of Records : " + ID,
Toast.LENGTH_SHORT).show();

objdatabase.close();

Db.java package com.example.db; import

android.content.Context; import

android.database.sqlite.SQLiteDatabase; import

android.database.sqlite.SQLiteOpenHelper; import

androidx.annotation.Nullable; public class db extends

SQLiteOpenHelper { private static final int

DATABASE_VERSION = 1; private static final String

DATABASE_NAME = "table"; public db(Context

context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

@Override public void onCreate(SQLiteDatabase

db) {

String CREATE_USER_TABLE = "CREATE TABLE userdetails12(ID INTEGER


PRIMARY KEY AUTOINCREMENT, " +

"Name TEXT NOT NULL, Empno TEXT NOT NULL, Dept TEXT NOT NULL"
+ ")";

50
db.execSQL(CREATE_USER_TABLE);

@Override public void onUpgrade(SQLiteDatabase db,

int i, int i1) {

51
PRACTICAL – 13

MainActivity.java import android.os.Bundle;


import android.view.View; import
android.widget.Button; import
androidx.appcompat.app.AlertDialog; import
androidx.appcompat.app.AppCompatActivity
; public class MainActivity extends
AppCompatActivity { @Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button showAlertButton = findViewById(R.id.showAlertButton);
showAlertButton.setOnClickListener(new View.OnClickListener() {
@Override public void
onClick(View view) {
showAlertDialog();
}
});
}
private void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert Title")
.setMessage("This is a sample alert dialog.")
.setPositiveButton("OK", null); // You can add listeners for buttons if
needed
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
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"
android:paddingLeft="16dp" android:paddingTop="16dp"

52
android:paddingRight="16dp" android:paddingBottom="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/showAlertButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Show Alert"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>

53
54

You might also like