Practical – 6
1. Create a Recycler View and list the details of
student using following fields:
1) Name
2) Address
3) Photo (Image)
4) Delete ( Button Operation)
activity_main.xml
Layout: Constraint Layout
Widgets:
RecyclerView(id: @+id/recyclerView)
Mobile Application Development (3170726)
list_items.xml
Layout: Relative Layout
Widgets:
ImageView(id: @+id/imageView)
TextView(id: @+id/textView1)
TextView(id: @+id/textView2)
FloatingActionButton(id: @+id/floatingActionButon)
dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="ic_clear_margin">56dp</dimen>
</resources>
border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#CCCCCC" />
</shape>
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_6;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListData[] myListData = new ListData[] {
new ListData("Jack", "NYC",R.drawable.i1),
new ListData("Hadley", "Washington DC",R.drawable.i2),
new ListData("Dudley", "Cape Town",R.drawable.i3),
new ListData("Tom", "Athens",R.drawable.i4),
new ListData("Ferdinand", "Barcelona",R.drawable.i5),
};
RecyclerView recyclerView = (RecyclerView)
findViewById(R.id.recyclerView);
ListAdapter adapter = new ListAdapter(myListData);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}
Mobile Application Development (3170726)
ListData.java
package com.example.mad_p_6;
public class ListData{
private String name;
private int image;
private String address;
public ListData(String name, String address, int image) {
this.name = name;
this.image = image;
this.address = address;
}
public String getName() {
return "Name: "+name;
}
public void setName(String name) {
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getAddress() {
return "Address: "+address;
}
public void setAddress(String address) {
this.address = address;
}
}
Mobile Application Development (3170726)
ListAdapter.java
package com.example.mad_p_6;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class ListAdapter extends
RecyclerView.Adapter<ListAdapter.ViewHolder>{
private ListData[] listdata;
// RecyclerView recyclerView;
public ListAdapter(ListData[] listdata) {
this.listdata = listdata;
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.list_items, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
public void onBindViewHolder(ViewHolder holder, int position) {
final ListData myListData = listdata[position];
holder.textView1.setText(listdata[position].getName());
Mobile Application Development (3170726)
holder.textView2.setText(listdata[position].getAddress());
holder.imageView.setImageResource(listdata[position].getImage());
holder.floatingActionButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"Item cannot be deleted:
"+myListData.getName(),Toast.LENGTH_LONG).show();
}
});
public int getItemCount() {
return listdata.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView1;
public TextView textView2;
public FloatingActionButton floatingActionButton;
public RelativeLayout relativeLayout;
public ViewHolder(View itemView) {
super(itemView);
this.imageView = (ImageView)
itemView.findViewById(R.id.imageView);
this.textView1 = (TextView) itemView.findViewById(R.id.textView1);
this.textView2 = (TextView) itemView.findViewById(R.id.textView2);
this.floatingActionButton =
itemView.findViewById(R.id.floatingActionButton);
relativeLayout =
(RelativeLayout)itemView.findViewById(R.id.relativeLayout);
}
}
}
Mobile Application Development (3170726)
Output Screenshots:
Mobile Application Development (3170726)
Practical – 7
1. Practical: Theme, Custom Styles, Drawables
activity_main.xml
Layout: Constraint Layout
Widgets:
TextView(id: @+id/title)
Button(id: @+id/button)
Mobile Application Development (3170726)
styles.xml
<resources>
<style name="text_subheader_wh">
<item name="android:textColor">#000</item>
<item name="android:textSize">20dp</item>
<item name="android:shadowDy">1.0</item>
<item name="android:shadowRadius">1</item>
<item name="android:shadowColor">#000</item>
</style>
</resources>
grad.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="@color/purple_200"
android:endColor="@color/teal_200"/>
</shape>
</item>
</selector>
buttonstyle.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<gradient android:angle="270"
android:centerColor="#1200FFFF"
android:endColor="#FF2003FF"
android:startColor="#F00FFFFF" />
<padding
android:bottom="8dp"
Mobile Application Development (3170726)
android:left="16dp"
android:right="16dp"
android:top="8dp" />
<corners android:radius="48dp" />
</shape>
</item>
</selector>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_7;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Output Screenshots:
Mobile Application Development (3170726)
Practical – 8
1. Practical: Save user data in a database
activity_main.xml
Layout: Linear Layout
Widgets:
TextView(id: @+id/name, text: “Name:”)
EditText(id: @+id/namevalue)
TextView(id: @+id/email, text: “E-Mail:”)
EditText(id: @+id/emailvalue)
TextView(id: @+id/contact, text: “Contact:”)
EditText(id: @+id/contactvalue)
TextView(id: @+id/city, text: “City:”)
EditText(id: @+id/cityvalue)
Mobile Application Development (3170726)
Button(id: @+id/submitbutton, text: “Submit”)
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.*;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button submitButton = findViewById(R.id.submitbutton);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Button
clicked",Toast.LENGTH_LONG);
Log.d("","Button clicked");
EditText name = findViewById(R.id.namevalue);
EditText email = findViewById(R.id.emailvalue);
EditText contact = findViewById(R.id.contactvalue);
EditText city = findViewById(R.id.cityvalue);
DBHandler dbHandler = new DBHandler(MainActivity.this);
String nameValue = name.getText().toString();
String emailValue = email.getText().toString();
String contactValue = contact.getText().toString();
String cityValue = city.getText().toString();
dbHandler.newEntry(nameValue,emailValue,contactValue,cityValue);
Toast.makeText(MainActivity.this,"Data successfully
added",Toast.LENGTH_LONG);
Mobile Application Development (3170726)
Log.d("","Data successfully added");
}
});
}
public void addEntry(View view){
}
}
DBHandler.java
package com.example.mad_p_8;
import android.database.sqlite.*;
import android.content.*;
import android.util.Log;
public class DBHandler extends SQLiteOpenHelper
{
public DBHandler(Context context){
super(context,"sample_database",null,1);
}
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String createQuery = "CREATE TABLE sampleTable (name TEXT, email
TEXT, contact TEXT, city TEXT)";
Log.d("","TABLE CREATED");
sqLiteDatabase.execSQL(createQuery);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS sampleTable");
Log.d("","TABLE DELETED");
onCreate(sqLiteDatabase);
}
Mobile Application Development (3170726)
public void newEntry(String name, String email, String contact, String city)
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("Name",name);
contentValues.put("EMail",email);
contentValues.put("contact",contact);
contentValues.put("City",city);
sqLiteDatabase.insert("sampleTable",null,contentValues);
Log.d("","DATA INSERTED");
sqLiteDatabase.close();
}
}
Mobile Application Development (3170726)
Output Screenshots:
Mobile Application Development (3170726)
(Before) ` (After)
Practical – 9
1. Use an AsyncTask to access remote database (make
a use of simple PhP Web service)
activity_main.xml
Layout: Constraint Layout
Widgets:
Button(id: @+id/showImage, text: “Show Image”)
ImageView(id:@+id/namevalue,
srcCompat="@drawable/ic_launcher_background")
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_9;
//package com.example.practical9;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
String apiUrl =
"https://asset.swarovski.com/images/$size_1450/t_swa103/b_rgb:ffffff,c_scale,dp
r_3.0,f_auto,w_500/5379499_png/star-wars---darth-vader-swarovski-
5379499.png";
ProgressDialog progressDialog;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button viewButton = findViewById(R.id.showImage);
imageView = findViewById(R.id.image);
viewButton.setOnClickListener(view -> {
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(apiUrl);
});
}
public class MyAsyncTask extends AsyncTask<String, Void, Bitmap>{
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
Mobile Application Development (3170726)
progressDialog.setMessage("Please Wait!");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Bitmap doInBackground(String... URL) {
String imageURL = URL[0];
Bitmap bitmap = null;
try{
Log.d("","In Try");
InputStream inputStream = new
java.net.URL(imageURL).openStream();
bitmap = BitmapFactory.decodeStream(inputStream);
}
catch (Exception e){ e.printStackTrace();
Log.d("","In Catch ");}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result){
super.onPostExecute(result);
imageView.setImageBitmap(result);
progressDialog.dismiss();
}
}
}
Mobile Application Development (3170726)
Output Screenshots:
Mobile Application Development (3170726)
Practical – 10
1. Use Retrofit to access remote database (make a
use of simple PhP Web service)
activity_main.xml
Layout: Linear Layout
Widgets:
ScrollView(id: @+id/scrollView2, text: “Show Image”)
TextView(id:@+id/textView)
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_10;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.List;
import java.util.Objects;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
TextView textView;
String WEB_URL = "https://raw.githubusercontent.com/devp22/json/main/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
Retrofit retrofit = new
Retrofit.Builder().baseUrl(WEB_URL).addConverterFactory(GsonConverterFact
ory.create()
).build();
getJSONData getData = retrofit.create(getJSONData.class);
Call<List<Model>> call = getData.getModels();
call.enqueue(new Callback<List<Model>>() {
@Override
public void onResponse(@NonNull Call<List<Model>> call, @NonNull
Response<List<Model>> response) {
List<Model> data = response.body();
for(int i = 0; i < data.size(); i++){
Mobile Application Development (3170726)
textView.append(" Name: "+data.get(i).getName()+"\n Number of
songs:"+data.get(i).getNumberOfSongs()+"\n
Publisher:"+data.get(i).getPublisher()+"\n\n\n");
}
}
@Override
public void onFailure(@NonNull Call<List<Model>> call, @NonNull
Throwable t) {
}
});
}
}
Model.java
package com.example.mad_p_10;
public class Model {
String NumberOfSongs, Name, Publisher;
public Model(String NumberOfSongs, String Publisher, String Name) {
this.Name = Name;
this.Publisher = Publisher;
this.NumberOfSongs = NumberOfSongs;
}
public String getNumberOfSongs() {
return NumberOfSongs;
}
public void setNumberOfSongs(String numberOfSongs){
this.NumberOfSongs = numberOfSongs;
}
public String getName() {
return Name;
}
Mobile Application Development (3170726)
public void setName(String name) {
this.Name = name;
}
public String getPublisher() {
return Publisher;
}
public void setPublisher(String publisher) {
this.Publisher = publisher;
}
}
getJSONData.java
package com.example.mad_p_10;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface getJSONData {
@GET("newplaylist.json")
Call<List<Model>> getModels();
}
Mobile Application Development (3170726)
Output Screenshots:
Mobile Application Development (3170726)
Practical – 11
1. Practical : Use Firebase to perform CRUID
operation
a) Main Screen
activity_main.xml
Layout: Linear Layout
Widgets:
Button(id: @+id/read, text: “Read”, onClick = readScreen)
Button(id: @+id/update, text: “Update”, onClick =
updateScreen)
Button(id: @+id/insert, text: “Insert”, onClick = insertScreen)
Button(id: @+id/delete, text: “Delete”, onClick = deleteScreen)
Mobile Application Development (3170726)
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_11;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
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;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void readScreen(View view){
Intent i = new Intent(MainActivity.this, readData.class);
startActivity(i);
}
public void insertScreen(View view){
Intent i = new Intent(MainActivity.this, insertData.class);
startActivity(i);
}
public void deleteScreen(View view){
Intent i = new Intent(MainActivity.this, deleteData.class);
startActivity(i);
}
public void updateScreen(View view){
Mobile Application Development (3170726)
Intent i = new Intent(MainActivity.this, updateData.class);
startActivity(i);
}
}
Output Screenshots:
Mobile Application Development (3170726)
b) Read Screen
activity_read_data.xml
Layout: Linear Layout
Widgets:
EditText(id: @+id/id, hint: “Enter if of record)
Button(id: @+id/getData, text: “Get Data”, onClick =
readValues)
TextView(id: @+id/DataName)
TextView(id: @+id/DataAddress)
TextView(id: @+id/DataEMail)
TextView(id: @+id/DataContact)
TextView(id: @+id/dataid, inputType = number)
Mobile Application Development (3170726)
readData.java
package com.example.mad_p_11;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.ChildEventListener;
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;
import java.util.ArrayList;
public class readData extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Read Screen");
setContentView(R.layout.activity_read_data);
}
public void readValues(View view){
EditText id = findViewById(R.id.id);
if(id.getText().toString()==null )
{
Toast.makeText(this,"Please enter missing
values",Toast.LENGTH_LONG).show();
}
else
{
Mobile Application Development (3170726)
String idValue = id.getText().toString();
Log.d("ID",idValue);
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference =
firebaseDatabase.getReference("Student");
databaseReference.child(idValue).addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Student student = snapshot.getValue(Student.class);
String name = student.getName();
String address = student.getAddress();
String email = student.getEMail();
String contact = student.getContact();
String idn = student.getID();
TextView nameval = findViewById(R.id.DataName);
TextView addval = findViewById(R.id.DataAddress);
TextView emailval = findViewById(R.id.DataEMail);
TextView cnoval = findViewById(R.id.DataContact);
TextView idval = findViewById(R.id.dataid);
nameval.setText("Name: "+name);
addval.setText("Address: "+address);
emailval.setText("E-Mail: "+email);
idval.setText("ID: "+idn);
cnoval.setText("Contact: "+contact);
Toast.makeText(readData.this,"Data fetched
successfully",Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.d("ERROR","Error");
}
});
}
}
}
Output Screenshots:
Mobile Application Development (3170726)
c) Update Screen
activity_update_data.xml
Mobile Application Development (3170726)
Layout: Linear Layout
Widgets:
EditText(id: @+id/updateid, hint: “Enter id of record”)
TextView(id: @+id/textView, text: “Choose the field you want
to update:”)
RadioGroup(id: @+id/radiogroup)
RadioButton(id: @+id/rb1, text: “name”)
RadioButton(id: @+id/rb2, text: “email”)
RadioButton(id: @+id/rb3, text: “address”)
RadioButton(id: @+id/rb4, text: “contact”)
EditText(id: @+id/updatevalue, hint: “Enter new value”)
Button(id: @+id/updatebutton, text: “Update Data”, onClick:
updateValue)
Mobile Application Development (3170726)
updateData.java
package com.example.mad_p_11;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class updateData extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Update Screen");
setContentView(R.layout.activity_update_data);
}
public void updateValue(View view){
EditText updateid = findViewById(R.id.updateid);
EditText newvalue = findViewById(R.id.updatevalue);
String newData = newvalue.getText().toString();
RadioGroup rg = findViewById(R.id.radiogroup);
RadioButton rb = findViewById(rg.getCheckedRadioButtonId());
if(updateid.getText().toString()==null )
{
Toast.makeText(this,"Please enter missing
Mobile Application Development (3170726)
values",Toast.LENGTH_LONG).show();
}
else
{
String idValue = updateid.getText().toString();
Log.d("ID",idValue);
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference =
firebaseDatabase.getReference("Student");
updateid.setText("");
newvalue.setText("");
rb.clearFocus();
databaseReference.child(idValue).child(rb.getText().toString()).setValue(newDat
a);
Toast.makeText(this,"Data updated
successfully",Toast.LENGTH_LONG).show();
}
}
}
Output Screenshots:
Mobile Application Development (3170726)
(Before) (After)
Mobile Application Development (3170726)
d) Insert Screen
activity_insert_data.xml
Layout: Linear Layout
Widgets:
TextView(id: @+id/name, text: “Name:”)
EditText(id: @+id/namevalue)
TextView(id: @+id/address, text: “Address:”)
EditText(id: @+id/addressvalue)
TextView(id: @+id/email, text: “E-Mail:”)
EditText(id: @+id/emailvalue)
TextView(id: @+id/contact, text: “Contact:”)
EditText(id: @+id/contactvalue)
Button(id: @+id/submit, text: “Insert Data”, onClick:
insertData)
Mobile Application Development (3170726)
insertData.java
public void insertData(View view){
EditText name = findViewById(R.id.namevalue);
EditText address = findViewById(R.id.addressvalue);
EditText email = findViewById(R.id.emailvalue);
EditText contact = findViewById(R.id.contactvalue);
if(name.getText().toString()==null ||address.getText().toString()==null ||
email.getText().toString()==null ||contact.getText().toString()==null )
{
Toast.makeText(this,"Please enter missing
values",Toast.LENGTH_LONG).show();
}
else
{
String nameValue = name.getText().toString();
String addressValue = address.getText().toString();
String emailValue = email.getText().toString();
String contactValue = contact.getText().toString();
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference =
firebaseDatabase.getReference("Student");
String key = databaseReference.push().getKey();
Student student = new
Student(nameValue,addressValue,emailValue,contactValue,key);
try {
databaseReference.push().setValue(student);
Toast.makeText(this,"Data inserted
successfully",Toast.LENGTH_LONG).show();
name.setText("");
address.setText("");
email.setText("");
contact.setText("");
}
catch (Error error){
Toast.makeText(this,error.toString(),Toast.LENGTH_LONG).show();
}
}
}
Mobile Application Development (3170726)
Output Screenshots:
Mobile Application Development (3170726)
(Before) (After)
Mobile Application Development (3170726)
e) Delete Screen
activity_delete_data.xml
Layout: Linear Layout
Widgets:
EditText(id: @+id/deleteid, hint: “Enter id of record”)
Button(id: @+id/deletebutton, text: “Delete Data”, onClick:
deleteValue)
Mobile Application Development (3170726)
deleteData.java
package com.example.mad_p_11;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class deleteData extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Delete Screen");
setContentView(R.layout.activity_delete_data);
}
public void deleteValue(View view){
EditText deleteid = findViewById(R.id.deleteid);
if(deleteid.getText().toString()==null )
{
Toast.makeText(this,"Please enter missing
values",Toast.LENGTH_LONG).show();
}
else
{
String idValue = deleteid.getText().toString();
Log.d("ID",idValue);
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference =
firebaseDatabase.getReference("Student");
deleteid.setText("");
databaseReference.child(idValue).removeValue();
Toast.makeText(this,"Data delete
successfully",Toast.LENGTH_LONG).show();
}
Mobile Application Development (3170726)
}
}
Output Screenshots:
Mobile Application Development (3170726)
(Before) (After)
Practical – 12
1. Practical: BroadcastReceiver
activity_main.xml
Mobile Application Development (3170726)
Layout: Constraint Layout
Widgets:
TextView(text: “Hello World!”)
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_12;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
BatteryChange batteryChange = new BatteryChange();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new
IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryChange, filter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(batteryChange);
}
}
Mobile Application Development (3170726)
BatteryChange.java
package com.example.mad_p_12;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.widget.Toast;
public class BatteryChange extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (true) {
Toast.makeText(context, "Battery changed",
Toast.LENGTH_SHORT).show();
} if(false) {
Toast.makeText(context, "Battery doesn't changed",
Toast.LENGTH_SHORT).show();
}
}
}
Mobile Application Development (3170726)
Output Screenshots:
Mobile Application Development (3170726)
Practical – 13
1. Practical: Notifications
activity_main.xml
Layout: Constraint Layout
Widgets:
Button(id:@+id/getNotify, text: “Get Notification”, onClick:
generateNotification)
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_13;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void generateNotification(View view) {
NotificationCompat.Builder builder = new
NotificationCompat.Builder(MainActivity.this, String.valueOf(32))
.setSmallIcon(R.drawable.messageicon)
.setContentTitle("Top priority")
.setContentText("Back off !!!")
.setPriority(NotificationCompat.PRIORITY_MAX);
Mobile Application Development (3170726)
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(MainActivity.this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
builder.setChannelId(channelId);
}
notificationManager.notify(0, builder.build());
Toast.makeText(MainActivity.this,"Notified",Toast.LENGTH_LONG).show();
Log.d("","Notified");
}
}
Mobile Application Development (3170726)
Output Screenshots:
Mobile Application Development (3170726)
Practical – 15
1. Practical : make a use of android system
activity_main.xml
Layout: Linear Layout
Widgets:
TextView(id:@+id/textView, text: “Enter your text:”)
EditText(id:@+id/editTextTextPersonName2)
Button(id:@+id/button, text: “Speak”)
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_15;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.*;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
EditText Text;
Button btnText;
TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Text = findViewById(R.id.editTextTextPersonName2);
btnText = findViewById(R.id.button);
textToSpeech = new TextToSpeech(getApplicationContext(), new
TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
// if No error is found then only it will run
if(i!=TextToSpeech.ERROR){
// To Choose language of speech
textToSpeech.setLanguage(Locale.US);
}
}
Mobile Application Development (3170726)
});
// Adding OnClickListener
btnText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textToSpeech.speak(Text.getText().toString(),TextToSpeech.QUEUE_FLUSH,nu
ll);
}
});
}
}
Mobile Application Development (3170726)
Output Screenshots:
Mobile Application Development (3170726)
Practical – 16
1. Using location service get the current location and
display in TextView
activity_main.xml
Layout: Constraint Layout
Widgets:
TextView(id:@+id/textView)
Button(id:@+id/button, text: “Button”)
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_16;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.CancellationToken;
import com.google.android.gms.tasks.OnSuccessListener;
public class MainActivity extends AppCompatActivity {
Button getLocationBtn;
TextView locationText;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocationBtn = (Button)findViewById(R.id.button);
Mobile Application Development (3170726)
locationText = (TextView)findViewById(R.id.textView);
getLocationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(MainActivity.this,new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
});
}
public void onRequestPermissionsResult(int requestCode, String permissions[],
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
getLocation();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
void getLocation() {
try {
locationManager = (LocationManager)
getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
Mobile Application Development (3170726)
5000, 5, MainActivity.this);
Log.d("GOT LOCATION","");
Toast.makeText(MainActivity.this,"GOT
LOCATION",Toast.LENGTH_LONG).show();
}
catch(SecurityException e) {
Log.d("ERROR","");
e.printStackTrace();
}
}
public void onGetLocation(Location location) {
Log.d("Longitude",String.valueOf(location.getLongitude()));
locationText.setText("Current coordinates: " + location.getLatitude() + ", " +
location.getLongitude());
Toast.makeText(MainActivity.this,"Current Location: " +
location.getLatitude() + ", " +
location.getLongitude(),Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this, "Please Enable GPS and Internet",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
public void onProviderEnabled(String provider) {
}
}
Mobile Application Development (3170726)
Output Screenshots:
Mobile Application Development (3170726)
Practical – 17
1. Practical : Display the use of animations
activity_main.xml
Layout: Constraint Layout
Widgets:
ImageView(id:@+id/imageView, srcCompat:
@drawable/mickey)
Button(id:@+id/button, text: “Animate”, onClick: zoomer)
Mobile Application Development (3170726)
animate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.5"
android:toXScale="2.0"
android:fromYScale="0.5"
android:toYScale="2.0"
android:duration="5000"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="5000"
android:fromXScale="2.0"
android:toXScale="0.5"
android:fromYScale="2.0"
android:toYScale="0.5"
android:duration="5000"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
</set>
Mobile Application Development (3170726)
MainActivity.java
package com.example.mad_p_17;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void zoomer(View view){
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.animate);
image.startAnimation(animation);
}
}
Mobile Application Development (3170726)
Output Screenshots:
Mobile Application Development (3170726)