Nafis Andriod - Manual
Nafis Andriod - Manual
Nafis Andriod - Manual
INFORMATION TECHNOLOGY
(AIIT)
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
Index
Index Description Page no
4
1. Installation of the the Android studio, ogram 7
5
Boxes and Changing Colors 26
7. Firebase in Android 30
8. Services in Android 33
6
PRACTICAL – 1
7
8
PRACTICAL – 2
android.widget.Button; import
android.widget.TextView; import
androidx.appcompat.app.AppCompatActivity; public
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() {
textView.setText("Text Changed
on
" + getCurrentDate());
});
// You can implement your logic to get the current date here
XML
<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"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_marginBottom="16dp"/>
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
11
PRACTICAL – 3
MainActivity.java
MainActivity.java package com.example.myapplication;
12
android.view.View; import android.widget.Button; import
public class
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener(){
onClick(View view) {
// TextView t1=(TextView)findViewById(R.id.text1);
EditText e1=(EditText)findViewById(R.id.editText1);
EditText e2=(EditText)findViewById(R.id.editText2);
EditText e3=(EditText)findViewById(R.id.editText3);
Integer.valueOf(str1.toString()); int b=
sum = a+b;
e3.setText(String.valueOf(sum));
});
13
Button b2=(Button)findViewById(R.id.button2);
onClick(View view) {
// TextView t1=(TextView)findViewById(R.id.text1);
EditText e1=(EditText)findViewById(R.id.editText1);
EditText e2=(EditText)findViewById(R.id.editText2);
EditText e3=(EditText)findViewById(R.id.editText3);
Integer.valueOf(str1.toString()); int b=
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
<LinearLayout 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:orientation="vertical"
android:padding="16dp" tools:context=".MainActivity">
<EditText android:id="@+id/operand1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" android:hint="Operand
1" />
<EditText android:id="@+id/operand2EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" android:hint="Operand
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="18sp"
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
17
android:text="Addition" /> <Button
android:id="@+id/subtractButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/multiplyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiplication" />
<Button android:id="@+id/divideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
18
resultTextView = findViewById(R.id.resultTextView);
subtractButton = findViewById(R.id.subtractButton);
= findViewById(R.id.divideButton); addButton.setOnClickListener(new
View.OnClickListener() {
});
subtractButton.setOnClickListener(new View.OnClickListener() {
onClick(View v) {
performOperation('-');
});
onClick(View v) { performOperation('*');
});
divideButton.setOnClickListener(new View.OnClickListener() {
onClick(View v) {
performOperation('/');
19
}); } private void performOperation(char operator)
Double.parseDouble(operand1EditText.getText
Double.parseDouble(operand2EditText.getText
'/': if(operand2 != 0)
} catch (NumberFormatException e) {
numbers.");
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>
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import
java.util.SimpleTimeZone;
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>
import androidx.appcompat.app.AppCompatActivity;
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
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"
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: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>
import androidx.appcompat.app.AppCompatActivity;
extends AppCompatActivity {
savedInstanceState) {
super.onCreate(savedInstanceState);
(Button) findViewById(R.id.b3);
bb1.setOnClickListener(new View.OnClickListener() {
34
@Override public void onClick(View
view) {
startActivity(i);
});
bb2.setOnClickListener(new View.OnClickListener() {
view) {
Intent i = Intent(Intent.ACTION_VIEW,Uri.parse("https://www.google.com/"));
startActivity(i);
});
bb3.setOnClickListener(new View.OnClickListener() {
Intent(Intent.ACTION_VIEW,
Uri.parse("geo:32.23543.-
143.5468"));; startActivity(i);
});
Output:
35
PRACTICAL – 9
<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: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"
/>
com.example.sensors; import
androidx.appcompat.app.AppCompatActivity; import
android.hardware.Sensor; import
android.hardware.SensorManager; import
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = sMgr.getSensorList(Sensor.TYPE_ALL);
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
<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"
<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"
/>
com.example.db; import
androidx.appcompat.app.AppCompatActivity; import
android.content.ContentValues;
android.database.sqlite.SQLiteDatabase; import
47
android.widget.Toast; public class MainActivity extends
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); db objdatabase =
setContentView(R.layout.activity_main); edn =
findViewById(R.id.tv4); b1 = (Button)
findViewById(R.id.b1); b2 = (Button)
findViewById(R.id.b2); b1.setOnClickListener(new
});
b2.setOnClickListener(new View.OnClickListener() {
view) {
48
SQLiteDatabase db = openOrCreateDatabase("table", MODE_PRIVATE, null);
startManagingCursor(cursor); while
(!cursor.isAfterLast()) {
String n = cursor.getString(1);
String no = cursor.getString(2);
String de = cursor.getString(3);
tv3.setText(Observations2); tv4.setText(Observations3);
edn.getText().toString(); empno =
edno.getText().toString(); empdept =
49
edd.getText().toString(); ContentValues
objdatabase.close();
android.content.Context; import
android.database.sqlite.SQLiteDatabase; import
android.database.sqlite.SQLiteOpenHelper; import
context) {
db) {
"Name TEXT NOT NULL, Empno TEXT NOT NULL, Dept TEXT NOT NULL"
+ ")";
50
db.execSQL(CREATE_USER_TABLE);
51
PRACTICAL – 13
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