Ilovepdf Merged
Ilovepdf Merged
PRACTICAL: -23
Aim: -Integrate Google maps API to your Android application and find the
distance of any nearby location from your current location and display it.
4351604 Mobile Application Development 226370316018
PRACTICAL: -23
Aim: -Integrate Google maps API to your Android application and find the
distance of any nearby location from your current location and display it.
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.com.mapexample.MapsActivity" />
build.gradel
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-maps:11.8.0'
compile 'com.google.android.gms:play-services-location:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
4351604 Mobile Application Development 226370316018
MapsActivity.java
package example.com.mapexample;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.location.LocationServices;
import android.location.Location;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
private GoogleMapmMap;
Location mLastLocation;
Marker mCurrLocationMarker;
GoogleApiClientmGoogleApiClient;
LocationRequestmLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
@Override
public void onMapReady(GoogleMapgoogleMap) {
mMap = googleMap;
if (android.os.Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
4351604 Mobile Application Development 226370316018
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
//Place current location marker
LatLnglatLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptionsmarkerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mCurrLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
@Override
public void onConnectionFailed(ConnectionResultconnectionResult) {
}
4351604 Mobile Application Development 226370316018
AndroidManifest.xm
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.com.mapexample">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<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/AppTheme">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
OUTPUT
4351604 Mobile Application Development 226370316018
4351604 Mobile Application Development 226370316018
4341604 MAD 226370316018
Practical – 18
Aim :- Create an application to retrieve data from the Firebase Database and
display it in the RecyclerView.
4341604 MAD 226370316018
Practical – 18
Aim :- Create an application to retrieve data from the Firebase Database and
display it in the RecyclerView.
Activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#65E4A6"/>
</androidx.constraintlayout.widget.ConstraintLayout>
person.xml
<androidx.cardview.widget.CardView
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="wrap_content"
android:layout_marginHorizontal="10dp"
4341604 MAD 226370316018
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:scrollbars="vertical"
app:cardCornerRadius="20dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/firstname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView1"
app:layout_constraintTop_toTopOf="parent" />
<TextView
4341604 MAD 226370316018
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Last name:"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1" />
<TextView
android:id="@+id/lastname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/firstname" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="Age"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
4341604 MAD 226370316018
<TextView
android:id="@+id/age"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/lastname" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
person.java
package com.example.recyclerview;
public person() {}
return firstname;
this.firstname = firstname;
return lastname;
this.lastname = lastname;
return age;
this.age = age;
}
4341604 MAD 226370316018
personAdapter.java
package com.example.recyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
person, personAdapter.personsViewholder> {
public personAdapter(
@NonNullFirebaseRecyclerOptions<person> options)
super(options);
@Override
protected void
onBindViewHolder(@NonNullpersonsViewholder holder,
{
4341604 MAD 226370316018
holder.firstname.setText(model.getFirstname());
holder.lastname.setText(model.getLastname());
holder.age.setText(model.getAge());
// "person.xml")in
@NonNull
@Override
public personsViewholder
onCreateViewHolder(@NonNullViewGroup parent,
intviewType)
View view
= LayoutInflater.from(parent.getContext())
class personsViewholder
extends RecyclerView.ViewHolder {
super(itemView);
firstname
= itemView.findViewById(R.id.firstname);
lastname = itemView.findViewById(R.id.lastname);
age = itemView.findViewById(R.id.age);
MainActivity.java
package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
private RecyclerViewrecyclerView;
personAdapter
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// its reference
mbase
= FirebaseDatabase.getInstance().getReference();
recyclerView = findViewById(R.id.recycler1);
recyclerView.setLayoutManager(
new LinearLayoutManager(this));
FirebaseRecyclerOptions<person> options
= new FirebaseRecyclerOptions.Builder<person>()
.setQuery(mbase, person.class)
.build();
recyclerView.setAdapter(adapter);
super.onStart();
adapter.startListening();
super.onStop();
adapter.stopListening();
AndroidManifest.xml
“uses-permission android:name=”android.permission.INTERNET”
Database
4341604 MAD 226370316018
4341604 MAD 226370316018
4341604 MAD 226370316018
4341604 MAD 226370316018
Output
(4351604) Mobile Application Development 226370316018
Practical – 22
Aim :-Integrate Google maps API to your Android application and display your
current location in the app.
(4351604) Mobile Application Development 226370316018
Practical – 22
Aim :-Integrate Google maps API to your Android application and display your
current location in the app.
Activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
MainActivity.java
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import androidx.annotation.NonNull;
(4351604) Mobile Application Development 226370316018
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
Location currentLocation;
FusedLocationProviderClientfusedLocationProviderClient;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fetchLocation();
if (ActivityCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
if (location != null) {
currentLocation = location;
SupportMapFragmentsupportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.myMap);
supportMapFragment.getMapAsync(MainActivity.this);
}
(4351604) Mobile Application Development 226370316018
});
@Override
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 5));
googleMap.addMarker(markerOptions);
@Override
switch (requestCode) {
case REQUEST_CODE:
fetchLocation();
break;
strings.xml
<resources>
<string name="app_name">Sample</string>
</resources>
androidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
(4351604) Mobile Application Development 226370316018
Output
4341604 MAD 226370316018
Practical – 16
AIM = Create an application to Update and Delete data from the
SQLite database using SQLiteOpenHelper class.
4341604 MAD 226370316018
Practical – 16
AIM = Create an application to Update and Delete data from the
SQLite database using SQLiteOpenHelper class.
activity_main.xml
<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"
tools:context=".MainActivity">
<EditText
android:id="@+id/idEdtCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
<EditText
android:id="@+id/idEdtCourseDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
<EditText
android:id="@+id/idEdtCourseTracks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
<EditText
android:id="@+id/idEdtCourseDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
<Button
android:id="@+id/idBtnAddCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Add Course"
android:textAllCaps="false" />
</LinearLayout>
4341604 MAD 226370316018
DBHandler.java
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
db.execSQL(query);
SQLiteDatabase db = this.getWritableDatabase();
values.put(NAME_COL, courseName);
values.put(DURATION_COL, courseDuration);
values.put(DESCRIPTION_COL, courseDescription);
values.put(TRACKS_COL, courseTracks);
db.close();
@Override
onCreate(db);
}
4341604 MAD 226370316018
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
courseNameEdt = findViewById(R.id.idEdtCourseName);
courseTracksEdt = findViewById(R.id.idEdtCourseTracks);
courseDurationEdt = findViewById(R.id.idEdtCourseDuration);
courseDescriptionEdt = findViewById(R.id.idEdtCourseDescription);
addCourseBtn = findViewById(R.id.idBtnAddCourse);
// below line is to add on click listener for our add course button.
addCourseBtn.setOnClickListener(new View.OnClickListener() {
@Override
return;
courseNameEdt.setText("");
courseDurationEdt.setText("");
courseTracksEdt.setText("");
courseDescriptionEdt.setText("");
});
}
4341604 MAD 226370316018
OUTPUT
Insert data
4341604 MAD 226370316018
<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"
tools:context=".UpdateCourseActivity">
<EditText
android:id="@+id/idEdtCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
<EditText
android:id="@+id/idEdtCourseDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
<EditText
android:id="@+id/idEdtCourseTracks"
android:layout_width="match_parent"
4341604 MAD 226370316018
android:layout_height="wrap_content"
android:layout_margin="10dp"
<EditText
android:id="@+id/idEdtCourseDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
<Button
android:id="@+id/idBtnUpdateCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Update Course"
android:textAllCaps="false" />
</LinearLayout>
4341604 MAD 226370316018
DBHandler.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
@Override
db.execSQL(query);
SQLiteDatabase db = this.getWritableDatabase();
values.put(NAME_COL, courseName);
values.put(DURATION_COL, courseDuration);
values.put(DESCRIPTION_COL, courseDescription);
values.put(TRACKS_COL, courseTracks);
db.close();
SQLiteDatabase db = this.getReadableDatabase();
// on below line we are creating a cursor with query to read data from database.
if (cursorCourses.moveToFirst()) {
do {
// on below line we are adding the data from cursor to our array list.
courseModalArrayList.add(new CourseModal(cursorCourses.getString(1),
cursorCourses.getString(4),
cursorCourses.getString(2),
cursorCourses.getString(3)));
} while (cursorCourses.moveToNext());
cursorCourses.close();
return courseModalArrayList;
SQLiteDatabase db = this.getWritableDatabase();
values.put(NAME_COL, courseName);
values.put(DURATION_COL, courseDuration);
values.put(DESCRIPTION_COL, courseDescription);
4341604 MAD 226370316018
values.put(TRACKS_COL, courseTracks);
// on below line we are calling a update method to update our database and passing our values.
// and we are comparing it with name of our course which is stored in original name variable.
db.close();
@Override
onCreate(db);
}
4341604 MAD 226370316018
Output
For update
4341604 MAD 226370316018
Practical – 20
AIM : - Perform insertion of data to MySQL database using PHP from
an Android application.
4341604 MAD 226370316018
Practical – 20
AIM : - Perform insertion of data to MySQL database using PHP from
an Android application.
Crud.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "id16310745_gfgdatabase";
// Create connection
$response = array();
$courseName = $_POST['courseName'];
$courseDuration = $_POST['courseDuration'];
$courseDescription = $_POST['courseDescription'];
$stmt->bind_param("sss",$courseName,$courseDuration,$courseDescription);
if($stmt->execute() == TRUE){
$response['error'] = false;
} else{
$response['error'] = true;
} else{
$response['error'] = true;
echo json_encode($response);
?>
4341604 MAD 226370316018
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<EditText
android:id="@+id/idEdtCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Course Name"
android:importantForAutofill="no"
android:inputType="text" />
<EditText
android:id="@+id/idEdtCourseDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:importantForAutofill="no"
android:inputType="time" />
4341604 MAD 226370316018
<EditText
android:id="@+id/idEdtCourseDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Course Description"
android:importantForAutofill="no"
android:inputType="text" />
<Button
android:id="@+id/idBtnSubmitCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textAllCaps="false" />
</LinearLayout>
4341604 MAD 226370316018
MainActivity.java
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
courseNameEdt = findViewById(R.id.idEdtCourseName);
courseDescriptionEdt = findViewById(R.id.idEdtCourseDescription);
courseDurationEdt = findViewById(R.id.idEdtCourseDuration);
submitCourseBtn = findViewById(R.id.idBtnSubmitCourse);
submitCourseBtn.setOnClickListener(new View.OnClickListener() {
@Override
courseName = courseNameEdt.getText().toString();
courseDescription = courseDescriptionEdt.getText().toString();
courseDuration = courseDurationEdt.getText().toString();
if (TextUtils.isEmpty(courseName)) {
} else if (TextUtils.isEmpty(courseDescription)) {
} else if (TextUtils.isEmpty(courseDuration)) {
} else {
});
4341604 MAD 226370316018
@Override
try {
} catch (JSONException e) {
e.printStackTrace();
courseNameEdt.setText("");
courseDescriptionEdt.setText("");
courseDurationEdt.setText("");
}, new com.android.volley.Response.ErrorListener() {
@Override
}) {
@Override
@Override
params.put("courseName", courseName);
params.put("courseDuration", courseDuration);
params.put("courseDescription", courseDescription);
return params;
};
queue.add(request);
4341604 MAD 226370316018
Practical:-17
Aim:- Perform Firebase Integration to your Android application and store the data
in the Firebase Database.
4351604 Mobile Application Development 7
226370316018
Practical:-17
Aim:- Perform Firebase Integration to your Android application and store the data
in the Firebase Database.
Inside that column Navigate to Firebase Realtime Database. Click on that option and you
will get to see two options on Connect app to Firebase and Add Firebase Realtime
Database to your app. Click on Connect now and your app will be connected to Firebase.
After that click on the second option and now your App is connected to Firebase.
4351604 Mobile Application Development 7
226370316018
After connecting your app to Firebase you will get to see the below screen.
4351604 Mobile Application Development 7
226370316018
<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.Pr17"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
4351604 Mobile Application Development 7
226370316018
OUTPUT:-
4351604 Mobile Application Development 7
226370316018
android:layout_margin="10dp"
android:hint="Enter employee address"
android:inputType="textPostalAddress" />
</RelativeLayout>
4351604 Mobile Application Development 7
226370316018
package com.example.pr17;
// an empty constructor is
// required when using
// Firebase Realtime Database.
public EmployeeInfo() {
OUTPUT:-
4351604 Mobile Application Development 7
226370316018
package com.example.pr17;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
EmployeeInfo employeeInfo;
@Override
4351604 Mobile Application Development 7
226370316018
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
employeeNameEdt = findViewById(R.id.idEdtEmployeeName);
employeePhoneEdt = findViewById(R.id.idEdtEmployeePhoneNumber);
employeeAddressEdt = findViewById(R.id.idEdtEmployeeAddress);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("EmployeeInfo");
sendDatabtn = findViewById(R.id.idBtnSendData);
sendDatabtn.setOnClickListener(new View.OnClickListener() {
@Override
} else {
});
}
4351604 Mobile Application Development 7
226370316018
employeeInfo.setEmployeeName(name);
employeeInfo.setEmployeeContactNumber(phone);
employeeInfo.setEmployeeAddress(address);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
databaseReference.setValue(employeeInfo);
@Override
});
}
4351604 Mobile Application Development 7
226370316018
After adding this code go to this link for Firebase. After clicking on this link you will get to see
the below page and on this page Click on Go to Console option in the top right corner.
After clicking on this screen you will get to see the below screen with your all project inside
that select your project.
4351604 Mobile Application Development 7
226370316018
After clicking on this option you will get to see the screen on the right side. On this page click
on the Rules option which is present in the top bar. You will get to see the below screen.
Inside this screen click on the Rules tab you will get to see the above screen and change the
rules to true as shown in the screenshot. The rules are changed to true because we are not
providing authentication inside our app and we have to write data to our database. That’s why
we are specifying it to true. After changing your rules click on the publish rules button. Click
on that option and your rules will be published. Now come back to the Data tab of your
database.
4351604 Mobile Application Development 7
226370316018
4341604 Mobile Application Development 9
226370316018
Practical – 21
AIM : - Perform reading of the data from the MySQL database using
PHP in the JSON format and display on the screen of an Android
application.
4341604 Mobile Application Development 9
226370316018
PHP file
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "id16310745_gfgdatabase";
echo json_encode($response);
?>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/idCVCOurseItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:visibility="gone"
app:cardCornerRadius="6dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="vertical"
android:padding="4dp">
<TextView
android:id="@+id/idTVCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="CourseName"
android:textColor="@color/purple_500"
android:textSize="18sp"
android:textStyle="bold" />
</androidx.cardview.widget.CardView>
</LinearLayout>
Main_activity.java
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
4341604 Mobile Application Development 9
226370316018
import java.util.HashMap;
import java.util.Map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
courseDescTV.setText(jsonObject.getString("courseDescription"));
courseDurationTV.setText(jsonObject.getString("courseDuration"));
courseCV.setVisibility(View.VISIBLE);
}
// on below line we are displaying
// a success toast message.
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// method to handle errors.
Toast.makeText(MainActivity.this, "Fail to get course" + error,
Toast.LENGTH_SHORT).show();
}
}) {
@Override
public String getBodyContentType() {
// as we are passing data in the form of url encoded
// so we are passing the content type below
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
protected Map<String, String> getParams() {
// below line we are creating a map for storing our values in key and value pair.
Map<String, String> params = new HashMap<String, String>();
// on below line we are passing our key and value pair to our parameters.
params.put("id", courseId);
4341604 Mobile Application Development 9
226370316018
Output
4351604 Mobile Application Development 04
226370316018
Practical No:- 19
Aim:- Connect an Android application to the MySQL database using
PHP, and insert the data in the database table.
4351604 Mobile Application Development 04
226370316018
Output:
4351604 Mobile Application Development 04
226370316018
Practical No:- 19
Aim:- Connect an Android application to the MySQL database using
PHP, and insert the data in the database table.
PHP code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "id16310745_gfgdatabase";
$response = array();
$courseName = $_POST['courseName'];
$courseDuration = $_POST['courseDuration'];
$courseDescription = $_POST['courseDescription'];
$stmt->bind_param("sss",$courseName,$courseDuration,$courseDescription);
if($stmt->execute() == TRUE){
$response['error'] = false;
} else{
4351604 Mobile Application Development 04
226370316018
$response['error'] = true;
} else{
$response['error'] = true;
echo json_encode($response);
?>
4351604 Mobile Application Development 04
226370316018
Output:
4351604 Mobile Application Development 04
226370316018
activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<EditText
android:id="@+id/idEdtCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Course Name"
android:importantForAutofill="no"
android:inputType="text" />
<EditText
android:id="@+id/idEdtCourseDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:importantForAutofill="no"
android:inputType="time" />
<EditText
android:id="@+id/idEdtCourseDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Course Description"
android:importantForAutofill="no"
android:inputType="text" />
4351604 Mobile Application Development 04
226370316018
<Button
android:id="@+id/idBtnSubmitCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textAllCaps="false" />
</LinearLayout>
MainActivity.java file:
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
4351604 Mobile Application Development 04
226370316018
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
courseNameEdt = findViewById(R.id.idEdtCourseName);
courseDescriptionEdt = findViewById(R.id.idEdtCourseDescription);
courseDurationEdt = findViewById(R.id.idEdtCourseDuration);
submitCourseBtn = findViewById(R.id.idBtnSubmitCourse);
submitCourseBtn.setOnClickListener(new View.OnClickListener() {
@Override
courseName = courseNameEdt.getText().toString();
courseDescription = courseDescriptionEdt.getText().toString();
courseDuration = courseDurationEdt.getText().toString();
if (TextUtils.isEmpty(courseName)) {
} else if (TextUtils.isEmpty(courseDescription)) {
} else if (TextUtils.isEmpty(courseDuration)) {
4351604 Mobile Application Development 04
226370316018
} else {.
}});
@Override
try {
jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();}
courseNameEdt.setText("");
courseDescriptionEdt.setText("");
courseDurationEdt.setText("");
}
4351604 Mobile Application Development 04
226370316018
}, new com.android.volley.Response.ErrorListener() {
@Override
}) {
@Override
@Override
params.put("courseName", courseName);
params.put("courseDuration", courseDuration);
params.put("courseDescription", courseDescription);
return params;
};
queue.add(request);
}
4351604 Mobile Application Development 2263703160184
Practical No:- 17
Aim:- Perform Firebase Integration to your Android applicationand
store the data in the Firebase Database.
4351604 Mobile Application Development 2263703160184
Practical No:- 17
Aim:- Perform Firebase Integration to your Android application and store thedata
in the Firebase Database.
Inside that column Navigate to Firebase Realtime Database. Click on that option and you
will get to see two options on Connect app to Firebase and Add Firebase Realtime
Database to your app. Click on Connect now and your app will be connected to Firebase.
After that click on the second option and now your App is connected to Firebase.
4351604 Mobile Application Development 2263703160184
After connecting your app to Firebase you will get to see the below screen.
4351604 Mobile Application Development 2263703160184
For adding data to Firebase we should have to give permissions for accessing the internet. For
adding these permissions navigate to the app > AndroidManifest.xml and inside that file add
the below permissions to it.
<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.Pr17"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
4351604 Mobile Application Development 2263703160184
Output:
4351604 Mobile Application Development 2263703160184
Go to the activity_main.xml file and refer to the following code. Below is the code for
the activity_main.xml file.
android:layout_below="@id/idEdtEmployeePhoneNumber"
android:layout_margin="10dp"
android:hint="Enter employee address"
android:inputType="textPostalAddress" />
</RelativeLayout>
4351604 Mobile Application Development 2263703160184
For sending multiple data to the Firebase Realtime database we have to create an Object class
and send that whole object class to Firebase. For creating an object class navigate to the app >
java > your app’s package name > Right-click on it and Click on New > Java Class >
Give a name to your class. In my case, it is EmployeeInfo, and add below code to it.
package com.example.pr17;
// an empty constructor is
// required when using
// Firebase Realtime Database.
public EmployeeInfo() {
return employeeContactNumber;
}
Output:
4351604 Mobile Application Development 2263703160184
Go to the MainActivity.java file and refer to the following code. Below is the code for
the MainActivity.java file. Comments are added inside the code to understand the code in
more detail.
package com.example.pr17;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
EmployeeInfo employeeInfo;
@Override
4351604 Mobile Application Development 2263703160184
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
employeeNameEdt = findViewById(R.id.idEdtEmployeeName);
employeePhoneEdt = findViewById(R.id.idEdtEmployeePhoneNumber);
employeeAddressEdt = findViewById(R.id.idEdtEmployeeAddress);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("EmployeeInfo");
sendDatabtn = findViewById(R.id.idBtnSendData);
sendDatabtn.setOnClickListener(new View.OnClickListener() {
@Override
} else {
});
}
4351604 Mobile Application Development 2263703160184
employeeInfo.setEmployeeName(name);
employeeInfo.setEmployeeContactNumber(phone);
employeeInfo.setEmployeeAddress(address);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
databaseReference.setValue(employeeInfo);
@Override
});
}
4351604 Mobile Application Development 2263703160184
After adding this code go to this link for Firebase. After clicking on this link you will get to see
the below page and on this page Click on Go to Console option in the top right corner.
After clicking on this screen you will get to see the below screen with your all project inside
that select your project.
4351604 Mobile Application Development 2263703160184
After clicking on this option you will get to see the screen on the right side. On this page click
on the Rules option which is present in the top bar. You will get to see the below screen.
Inside this screen click on the Rules tab you will get to see the above screen and change the
rules to true as shown in the screenshot. The rules are changed to true because we are not
providing authentication inside our app and we have to write data to our database. That’s why
we are specifying it to true. After changing your rules click on the publish rules button. Click
on that option and your rules will be published. Now come back to the Data tab of your
database.
4351604 Mobile Application Development 226370316008
PRACTICAL – 28
PRACTICAL – 28
Before diving into the specifics of publishing your app, let's make sure you have all the
essentials in place. Creating a Google Developer account is something you can do at the
beginning of the app development process. Without a Google Play Store account, you can't
publish your app on the Play Market.
You can use any of your current Google accounts or create another one to sign up for a Google
Developer Account. It doesn't matter whether it's a personal or corporate account. You may
easily transfer your app to another one down the line if needed.
Signing up for a Google Developer account is easy. All you have to do is to:
Keep in mind that developers with personal developer accounts must meet certain app testing
requirements before they can distribute their app on Google Play. Also, since 2024, developers
with new personal accounts need to verify access to a real Android device using the Play
Console mobile app.
Usually, it takes no more than two days to get approval from Google. Don't worry if you forget
to add some information. You can edit your account later.
Once you finish creating the Merchant profile, the developer account gets automatically linked
to it.
Based on our experience, we highly recommend starting to prepare the End User License
Agreement (EULA) and Privacy Policy in advance.
You can take the documents from similar apps as references and create your own based on
them, or ask a lawyer to make everything from scratch.
EULA is an agreement between a product owner and a user of the product. In brief, it contains:
● What the users can do with the app and what they aren't allowed to do
● Licensing fees
● Intellectual property information, etc.
While Google Play doesn't require you to have a Terms of Use document for your app, it's a
highly recommended practice. Terms of Use or Terms and Conditions explain how users can
interact with your app and what to expect. You can combine Privacy Policy and Terms of Use
into a single document, with separate chapters for each.
● A complete list of personal data that is collected, processed, and used through the app
● Technical information that is collected about the device and the installed OS
● Functional features of the app, its paid and free functionality
● Place of registration of the company and/or location of the copyright holder of the
application
● The chosen legal system and legislation that will be applied in resolving disputes and
regulating legal relations
● The terms of the subscription
● Citizenship (residence) of the overwhelming majority of application users
● Age criteria, the presence of specific content
If Google decides that your product violates some policy chapters, your app may get rejected,
blocked, or even deleted from Google Play. Besides, numerous and repetitive violations may
lead to account termination.
Read also: Why Google and Apple may remove your app and how to deal with that
4351604 Mobile Application Development 226370316008
● Unique bundle ID
The package name should be suitable over the life of your application. You cannot change it
after the distribution. You can set the package name in the application's manifest file.
If your app exceeds this limit, you can always switch to APK Expansion Files.
You may learn more about app file technical requirements in the Developer
Documents, Prepare for the release guide.
4351604 Mobile Application Development 226370316008
You may need input from a designer and a copywriter, so it's better to start preparing the
following materials in advance.
● Product description
It contains a title of your app (up to 50 symbols), a brief description (up to 80 symbols), and a
full description (up to 4000 symbols). But remember, avoid keyword stuffing. Focus on writing
naturally and engagingly, and the right keywords will find their way in.
4351604 Mobile Application Development 226370316008
Store listing
● Screenshots
You may add from 2 to 8 screenshots. Choose the ones that highlight the app's functionality
and its unique value.
- the ratio of the long side to the short side should not be more than 2:1
4351604 Mobile Application Development 226370316008
● Icon
The requirements are the following:
- 512px by 512px
● Feature graphic
It is an optional marketing tool displayed in various places on Google Play, including the
homepage.
- 1024px x 500px
● Promo video
If you have any promo videos, you may add a link to your YouTube channel directly in the app
listing. This video will be displayed at the top of your app's page.
4351604 Mobile Application Development 226370316008
● Tags
Unlike some app stores, Google Play doesn't allow custom keywords. When creating your app
listing, you'll be able to choose from a predefined list of relevant keywords. Here's the trick:
selecting the most accurate keywords will significantly boost your App Store Optimization
(ASO) and help users discover your app.
● Localization
If your app supports several languages, you can add translations for your app's Store Listing
page, APK files, strings or in-app products. It's highly recommended to include localized
screenshots and images.
● Contact details
Here, you should provide the support service contacts. By filling in the website URL, email, and
phone, you make it easier for the users to contact you if necessary.
4351604 Mobile Application Development 226370316008
● Privacy Policy
Google requires you to add a link to the Privacy Policy that we mentioned above.
While editing the Store Listing, you can take a break at any moment, click Save Draft, and
complete this stage later.
You must provide accurate information for the questionnaire as any misrepresentation of your
app's content might lead to suspension or removal of the Google Play account.
Finally, you are ready to upload your Android app file. Head over to the App Releases section
on the left panel. Here, you will find three options for publishing the app: Production, Beta and
Alpha tracks.
We highly recommend starting with the Alpha or Beta versions. In this case, after passing the
review process, your app will not be available to everyone on the Google Play store.
The Alpha version assumes testing in a closed environment where you can invite a trusted
group of testers. With Beta testing, anyone can sign up to be a tester and send feedback to
you after giving your app a test drive.
Pre-release testing allows you to gather people's opinions, test your app with a broader
audience, and fix issues before making the app public.
Note that if you decide to change the Alpha or Beta version to the Production type, it will take
time to go through another review round.
App releases
● Click on Review to confirm the changes and send your app for review by pressing Start
rollout to production
Don't stress over missing crucial details. Google Play offers helpful instructions and tips
throughout the publishing process. Plus, the system won't let you submit your app for review if
there are any critical missing pieces.
Remember that with the very first version, there is no opportunity to set manual publishing.
The app will be released right after it passes the review. Usually, it takes up to 2 days. Google
says the review process could take up to 7 days or even longer in exceptional cases.
Once the app is reviewed, you'll receive a notification on Google Console Dashboard.