Mobile Application Development
Mobile Application Development
MOBILE APPLICATIONS
DEVELOPMENT
1
INDEX
SNo. Programs Page No.
1. Using emulator to deploy and run mobile apps 3-5
2
Q1. Using emulator to deploy and run mobile apps.
To use the Android Emulator, you will need to download it first. You can
download it from the SDK manager located in the tools.
Select Tools > SDK Manager. Then, from the settings window, choose Android
Emulator. Click Apply, and Android Studio will download the emulator for you.
Wait for the installations to complete, and then restart your computer and Android
Studio.
Now, select Tools >AVD Manager (for Android Virtual Device), and explore
the virtual devices.
When you set out to create a new virtual device, you’ll have to determine its
hardware first. This is where you select settings like the screen size, screen
resolution, screen pixel density, and RAM. You can define the hardware from
scratch or use the default hardware options offered by Android Studio.
In the hardware selection menu, you’ll also get to see a Play Store icon beside
some of the hardware choices. The system image of these devices will have a
Play Store integrated into their interfaces.
Android Studio asks you about the minimum SDK requirements when creating a
new project. These requirements include the API level and the Android version.
3
Recall this information and select the system image accordingly. Select the most
relevant system image, and click Next to download it if it isn’t downloaded
already.
Further customizations take place on the last screen for creating a new virtual
device.
With all the settings in place, you’ve successfully created a new virtual device, and
it should now show up in your AVD manager.
If you want to see how it looks, open the drop-down menu from the Actions
column and select Cold Boot Now. The emulator will show up on your screen.
If you do not have an application to run in the emulator and just want to
experiment with it, Android Studio lets you download and run a sample project.
To import a sample project, open the File menu and select New. In there, you’ll
find an option for importing a sample project.
4
5
Q2. Create an android application that shows Hello +name of the user who
are logged in and run it on an emulator.
CODE:
package com.example.myapplication;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import
com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
importcom.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
importandroidx.annotation.NonNull;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Locale;
import android.widget.Toast;
import com.example.myapplication.databinding.ActivityMainBinding;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Create the next level button, which tries to show an interstitial when
clicked.
mNextLevelButton = binding.nextLevelButton;
mNextLevelButton.setEnabled(false);
mNextLevelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{ showInterstitial();
7
}
8
});
// Toasts the test ad message on the screen. Remove this after defining your
own ad unit ID.
Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();
}
@Override
public booleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public booleanonOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{ return true;
}
return super.onOptionsItemSelected(item);
}
Toast.makeText(MainActivity.this, "onAdLoaded()",
Toast.LENGTH_SHORT).show();
interstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback()
{ @Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Log.d(TAG, "The ad was dismissed.");
}
@Override
public void
onAdFailedToShowFullScreenContent(AdErroradError) {
// Called when fullscreen content failed to show.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Log.d(TAG, "The ad failed to show.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(TAG, "The ad was shown.");
}
});
}
@Override
public void onAdFailedToLoad(@NonNull
10
LoadAdErrorloadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mInterstitialAd = null;
mNextLevelButton.setEnabled(true);
11
}
}
OUTPUT:-
12
Q3. Create an application that takes the name from a text box and shows hello
message along with the name entered in text box, when the user clicks the OK
button.
CODE:
To start with, lets open the layout file(content_hello.xml) and click on the design
tab.
From the left palette, click and drag a button and a Text field (EditText) on to the
smartphone.
User would be entering a name in the Text filed..lets click on it and modify the
display text to “Enter Name”.
Now, select the button and open the Properties window. Modify the onClick
property and provide a method name that will be called when the button is clicked.
At this point, if you switch to the Text tab, the layout file content_hello.xml would
contain code similar to this :
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
13
tools:context="com.topjavatutorial.helloworldapp.HelloActivity"
tools:showIn="@layout/activity_hello">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter Name"
android:ems="10"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:id="@+id/button"
14
android:layout_below="@+id/editText"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_marginTop="104dp"
android:onClick="doSomething" />
</RelativeLayout>
Button b = (Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener()
});
15
OUTPUT:
16
Q4. Develop an ANDRIOD application that uses GUI components, Font and
Colors.
Open Android Stdio and then click on File -> New -> New project.
1 package com.example.exno1;
2
3 import android.graphics.Color;
4 import android.support.v7.app.AppCompatActivity;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.widget.Button;
8 import android.widget.TextView;
9
10public class MainActivity extends AppCompatActivity
11{
12 int ch=1;
13 float font=30;
14 @Override
15 protected void onCreate(Bundle savedInstanceState)
16 {
18
19
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_main);
19 final TextView t= (TextView) findViewById(R.id.textView);
20 Button b1= (Button) findViewById(R.id.button1);
21 b1.setOnClickListener(new View.OnClickListener() {
22 @Override
23 public void onClick(View v)
24 { t.setTextSize(font);
25 font = font +
26 5; if (font ==
27 50) font = 30;
28 }
29 });
30 Button b2= (Button) findViewById(R.id.button2);
31 b2.setOnClickListener(new View.OnClickListener() {
32 @Override
33 public void onClick(View v)
34 { switch (ch) {
35 case 1:
36 t.setTextColor(Color.RED);
37 break;
38 case 2:
39 t.setTextColor(Color.GREEN);
40 break;
41 case 3:
42 t.setTextColor(Color.BLUE);
43 break;
44 case 4:
45 t.setTextColor(Color.CYAN);
46 break;
47 case 5:
48 t.setTextColor(Color.YELLOW);
49 break;
50 case 6:
t.setTextColor(Color.MAGENTA);
break;
}
ch++;
if (ch ==
7) ch =
1;
}
});
}
20
21
}
OUTPUT:
22
Q5. Write an application that draws basic graphical primitives on the screen.
package com.example.exno4;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
23
//Creating a Bitmap
i.setBackgroundDrawable(new BitmapDrawable(bg));
Canvas(bg);
paint.setColor(Color.BLUE);
paint.setTextSize(50);
paint);
25
//To draw a Square
paint);
paint);
26
Q6. Develop an application that uses Layout Managers and event listeners.
(incomp)
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Details Form"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
27
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:layout_marginBottom="200dp"
android:columnCount="2"
android:rowCount="3">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="0"
android:text="Name"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="1"
android:ems="10"/>
28
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="0"
android:text="Reg.No"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="1"
android:inputType="number"
android:ems="10"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
29
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="0"
android:text="Dept"
android:textSize="20sp"
android:gravity="center"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="1"
android:spinnerMode="dropdown"/>
</GridLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="150dp"
30
android:text="Submit"/>
</RelativeLayout>
31
Q7. Create and Login application as above. On successful login, open browser
with any URL
CODE:
package ps.pro4;
import android.app.Activity;
import
android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* www.master-gtu.blogspot.com
* pankajsharma(8460479175),
* chavdavijay(8460420769)
*/
Button btlogin,btclear;
EditTexteditemail,editpass;
@Override
{ super.onCreate(savedInstanceState);
32
setContentView(R.layout.main);
33
editemail=(EditText) findViewById(R.id.editemail);
editpass=(EditText) findViewById(R.id.editpass);
btlogin=(Button) findViewById(R.id.btlogin);
btclear=(Button) findViewById(R.id.btclear);
btlogin.setOnClickListener(this);
btclear.setOnClickListener(this);
@Override
Button action=(Button) v;
if(action.getId()==btlogin.getId())
String email=editemail.getText().toString();
String pass=editpass.getText().toString();
if(email.equals("pankaj@gmail.com") &&pass.equals("pankaj"))
Intent myintent=new
Intent(Intent.ACTION_VIEW,Uri.parse("http://www.gmail.com"));
this.startActivity(myintent);
else
Toast.makeText(this,"Sorry", Toast.LENGTH_SHORT).show();
34
}
else if(action.getId()==btclear.getId())
if(!editemail.getText().toString().equals("") ||
!editpass.getText().toString().equals(""))
editemail.setText("");
editpass.setText("");
else
35
Q8. Testing mobile app - unit testing, black box testing and test automation.
Tools used for Black box testing largely depends on the type of black box testing
you are doing.
36
Black Box Testing Techniques
37
Q9. Create an Iosapplication that can play audio and video files.
CODE:
Step 1 − Open Xcode → New Project → Single View Application → Let’s name it
“AudioVideo”.
Step 2 − Open Main.storyboard and add three buttons and name them as shown
below.
Step 3 − Create @IBOutlet for three buttons and name it to stop, playButton
and video button, as their name suggests they will be used to play the sound and
stop the sound and playVideo.
Step 5 − Navigate to your project Build Phases and add AVFoundation Framework
Step 6 − In your project directory add the mp3/audio file which you wish to play.
import AVFoundation
38
Step 8 − Create an Object of AVAudioPlayer.
else {
return
do {
url) avPlayer.play()
catch {
avPlayer.stop()
40
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
41
Q10. Write an iOS application that creates alarm clock.
In Main.storyboardfile add UIDatePicker and one UIButton titled Set Alarm into
view. Now add one UILabel to show date and time whatever we will set using date
picker. Add one UISwitch to makes scheduled alarm ON or OFF. Connect UI
objects to respective IBOutlet and IBActions as we described above. Check below
screen shots.
42
date into date picker. Create a date formatter to display date and time into Alarm
label, which we took earlier as lblAlarm.
Now we will implement a method to toggle alarm state either you want to keep
alarm ON or OFF. Let’s give this method name alarmSwitch and in this method we
have implemented logic to schedule or cancel alarm using the methods
alarmSetButtonTapped and alarmCancelButtonTapped respectively
User will tap on Set Alarm button located at bottom centre and
In this method we simply getting time from DatePicker and setting alarm by
scheduling a UILocalNotification through
scheduleLocalNotificationWithDatemethod that you can see coded at last into
below screenshot. The use of scheduleLocalNotificationWithDatemethod is to
schedule a notification by setting a fire date and to store the same date and time
into device memory through NSUserDefaults.
That’s it! You’re now ready to test your application. Just hit the Run button. If
everything is correct your application should run properly.
43
44
Q11. Devise an iOS application that draws basic graphical primitives
(rectangle, circle) on the screen.
45
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating a Bitmap
Bitmapbg = Bitmap.createBitmap(720,
1280, Bitmap.Config.ARGB_8888);
//Setting the Bitmap as background for the ImageView
ImageViewi = (ImageView) findViewById(R.id.imageView);
i.setBackgroundDrawable(new BitmapDrawable(bg));
//Creating the Canvas Object
Canvas canvas = new
Canvas(bg);
//Creating the Paint Object and set its color&TextSize
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(50);
//To draw a Rectangle
canvas.drawText("Rectangle", 420, 150,
paint);
canvas.drawRect(400, 200, 650, 700, paint);
//To draw a Circle
canvas.drawText("Circle", 120, 150,
paint);
47
canvas.drawLine(520, 850, 520, 1150, paint);
}
OUTPUT:
48
Q12. Build an iOS mobile application that create, save, update and delete data
in a database.
Once we are done creating the project, open the Main.storyboard file and add a
table view to the View Controller as shown in the below image.
To use the table view, we also need to create its outlet in the ViewController.swift
class. Once we implement the delegate and datasource methods, the
ViewController.swift contains the following code.
import UIKit
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
return UITableView.automaticDimension
return 5
50
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -
> UITableViewCell {
return cell
Now, we will create a model class Employee. In our project, the Employee has the
name, age, and id. For this purpose, create a new swift file and add the following
code.
import Foundation
class Employee
self.id = id
self.name = name
self.age = age
51
Now, we will create a class that contains the DB operations. We have named the
class as DBManager. It includes the methods to create the database, create the
table, insert in the table, read from the table, and performing the deletion. The
DBManager contains the following code.
import Foundation
import SQLite3
class DBManager
init()
db =
openDatabase()
createTable()
var db:OpaquePointer?
52
.appendingPathComponent(dbPath)
53
var db: OpaquePointer? = nil
return nil
else
return db
func createTable() {
if sqlite3_prepare_v2(db, createTableString, -
1, &createTableStatement, nil) == SQLITE_OK
if sqlite3_step(createTableStatement) == SQLITE_DONE
} else {
print("person table could not be created.")
}
54
} else {
sqlite3_finalize(createTableStatement)
for p in persons
if p.id == id
return
let insertStatementString = "INSERT INTO person (Id, name, age) VALUES (?, ?,
?);"
if sqlite3_prepare_v2(db, insertStatementString, -
1, &insertStatement, nil) == SQLITE_OK {
sqlite3_bind_int(insertStatement, 1, Int32(id))
sqlite3_bind_int(insertStatement, 3, Int32(age))
55
if sqlite3_step(insertStatement) == SQLITE_DONE {
} else {
} else {
sqlite3_finalize(insertStatement)
if sqlite3_prepare_v2(db, queryStatementString, -
1, &queryStatement, nil) == SQLITE_OK {
let id = sqlite3_column_int(queryStatement, 0)
print("Query Result:")
56
print("\(id) | \(name) | \(year)")
} else {
sqlite3_finalize(queryStatement)
return emps
func deleteByID(id:Int) {
if sqlite3_prepare_v2(db, deleteStatementStirng, -
1, &deleteStatement, nil) == SQLITE_OK {
sqlite3_bind_int(deleteStatement, 1, Int32(id))
if sqlite3_step(deleteStatement) == SQLITE_DONE {
} else {
} else {
sqlite3_finalize(deleteStatement)
}
57
}
Finally, we need to save the data in db in the ViewController. For this purpose, we
will use DBManager class to save and retrieve data. The ViewController.swift
contains the following code.
import UIKit
var db = DBManager()
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
59
emps = db.read()
return UITableView.automaticDimension
return emps.count
60
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell") ??
UITableViewCell()
return cell
OUTPUT:
61