0% found this document useful (0 votes)
72 views

IT WM 131.1 Mobile Application Development 1 Laboratory

The document provides instructions for creating an Android project in Eclipse IDE and implementing an activity lifecycle sample program. It includes steps to create a new Android project, add an activity class, layout files and strings, and implement the various activity lifecycle callback methods like onCreate, onStart, onResume etc. to track state changes. The sample program displays the lifecycle method calls in a text view as the activity transitions between states when buttons are clicked.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

IT WM 131.1 Mobile Application Development 1 Laboratory

The document provides instructions for creating an Android project in Eclipse IDE and implementing an activity lifecycle sample program. It includes steps to create a new Android project, add an activity class, layout files and strings, and implement the various activity lifecycle callback methods like onCreate, onStart, onResume etc. to track state changes. The sample program displays the lifecycle method calls in a text view as the activity transitions between states when buttons are clicked.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 112

IT WM 131.

MOBILE APPLICATION DEVELOPMENT


1

LABORATORY MANUAL

1st Semester

Prepared by

JOHN B. LACEA, MCS


Android Development Kit Platform
HOW TO CREATE AN ANDROID PROJECT USING ECLIPSE IDE
1. First create a folder where to store the android project in your computer.
e.g. Android Projects Set A or Android Projects Set B
2. Click File menu>Switch Workspace>Other.

3. Browse the location of the folder where to store and load your android projects.

4. Click OK button.

1
5. Click menu File>New>Android Application Project. If in case the Android Application Project
does not appear, select Other>Android>Android Application Project.

6. Fill-up the following then click Next button.


Application Name: Activity Life Cycle
Project Name: Activity Life Cycle
Package Name: com.BSU.ActivityLifeCycle
Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
Target SDK: API 16: Android 4.1 (Jelly Bean)
Compile With: API 17: Android 4.2 (Jelly Bean)
Theme: Holo Light

2
7. Click Next button.

8. Click Next button.

3
9. Click Next button.

10. Fill-up the following then click Finish button.


Activity Name: Main
Layout Name: main
Navigation Type: None

4
HOW TO COMPILE AND RUN THE ANDROID PROJECT
1. On the Package Explorer (located at the left), right-click the Project Name..

2. Point to Run As and select Android Application.

3. Please wait more or less 5 minutes to run the Android OS SmartPhone Emulator.

Running the Sample Program in an Android OS SmartPhone Emulator


5
4. Click the OK button for Auto Monitor Logcat.

HOW TO EXIT THE RUNNING PROGRAM


1. Click the Back button.

2. Do not close the Android OS SmartPhone Emulator while you develop your application.

3. If you run your Mobile Application for several attempts and it does not install and run on
the Android OS SmartPhone Emulator then close the Android OS SmartPhone Emulator
and run again your Mobile Application to launch a new Android OS SmartPhone Emulator.

6
HOW TO CLOSE THE PROJECT
1. On the Package Explorer, Right-Click the project name and then click Close Project. You
need to close the project that you are not working to save memory space and lessen the
burden for the CPU.

2. Click the File menu> Close All.

HOW TO OPEN PROJECT


1. On the Package Explorer, Right-Click the project name and then click Open Project.

7
LABORATORY HANDS-ON ACTIVITY NO.1
ACTIVITY LIFE CYCLE
//Application Name: Activity Life Cycle
//Project Name: Activity Life Cycle
//Package Name: com.BSU.ActivityLifeCycle
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Activity Life Cycle</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.ActivityLifeCycle"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name="com.BSU.ActivityLifeCycle.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:orientation="vertical"
android:background="#0000ff"
tools:context=".Main" >

8
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ACTIVITY LIFE CYCLE"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#ff0000ff"
android:visibility="visible"
/>
<TextView
android:id="@+id/LblStatus"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:gravity="left"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#00ffff"
android:visibility="visible"
/>
<Button
android:id="@+id/BtnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Exit"
android:textSize="16sp"
android:textStyle="normal"
android:textColor="#000000"
android:background="#ffffff"
android:visibility="visible"
/>
</LinearLayout>
values-v11
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>

values-v14
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>

9
Main.java
//Program Activity Life Cycle
package com.BSU.ActivityLifeCycle;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;

public class Main extends Activity implements View.OnClickListener


{
//Data Members
private Button BtnExit;
private TextView LblStatus;
private StringBuilder Buffer;

//Default Constructor
public Main( )
{
//Instantiate Buffer
Buffer = new StringBuilder( );
Buffer.append("Constructor Main( )\n");
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//Instantiate the BtnExit


BtnExit = (Button) findViewById(R.id.BtnExit);
BtnExit.setOnClickListener(this);

//Instantiate the LblStatus


LblStatus = (TextView) findViewById(R.id.LblStatus);

LblStatus.setText(Buffer.toString( ));
}

//Button Click Listener


public void onClick(View v)
{
if(v.equals(BtnExit))
{
finish( );
}
}

@Override
protected void onStart( )
{
super.onStart( );
Buffer.append("onStart( )\n");
LblStatus.setText(Buffer.toString( ));
}

10
@Override
protected void onRestart( )
{
super.onRestart( );
Buffer.append("onRestart( )\n");
LblStatus.setText(Buffer.toString( ));
}

@Override
protected void onPause( )
{
super.onPause( );
Buffer.append("onPause( )\n");
LblStatus.setText(Buffer.toString( ));
}

@Override
protected void onStop( )
{
super.onStop( );
Buffer.append("onStop( )\n");
LblStatus.setText(Buffer.toString( ));
}

@Override
protected void onResume( )
{
super.onResume( );
Buffer.append("onResume( )\n");
LblStatus.setText(Buffer.toString( ));
}

@Override
protected void onPostResume( )
{
super.onPostResume( );
Buffer.append("onPostResume( )\n");
LblStatus.setText(Buffer.toString( ));
}

@Override
protected void onDestroy( )
{
super.onDestroy( );
Buffer.append("onDestroy( )\n");
LblStatus.setText(Buffer.toString( ));
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

11
Laboratory Hands-On Activity No.1 Normal Screen Output

LABORATORY HANDS-ON ACTIVITY NO.2


TIME DEPOSIT
//Application Name: Time Deposit
//Project Name: Time Deposit
//Package Name: com.BSU.TimeDeposit
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Time Deposit</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.TimeDeposit"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

12
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.TimeDeposit.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

main.xml
<TableLayout 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:stretchColumns="1"
android:background="#0000ff"
tools:context=".Main" >

<TableRow android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TIME DEPOSIT"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:visibility="visible"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Customer Name"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#00ff00"
android:background="#0000ff"
android:visibility="visible"
/>
<EditText
android:id="@+id/TxtCustomerName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="text|textAutoCorrect"
android:text=""/>
</TableRow>

13
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Principal Amount"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#00ff00"
android:background="#0000ff"
android:visibility="visible"
/>
<EditText
android:id="@+id/TxtPrincipalAmount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="numberSigned|numberDecimal"
android:text=""/>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rate"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#00ff00"
android:background="#0000ff"
android:visibility="visible"
/>
<EditText
android:id="@+id/TxtRate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="numberDecimal"
android:text=""/>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#00ff00"
android:background="#0000ff"
android:visibility="visible"
/>
<EditText
android:id="@+id/TxtTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
14
android:inputType="number"
android:text=""/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/LblCompoundInterest"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Compound Interest is P0.00"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#00ff00"
android:background="#0000ff"
android:visibility="visible"
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/LblCompoundAmount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Compound Amount is P0.00"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#00ff00"
android:background="#0000ff"
android:visibility="visible"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/BtnCompute"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Compute"/>
<Button
android:id="@+id/BtnExit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Exit"/>
</TableRow>
</TableLayout>
values-v11
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>

15
values-v14
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>
Main.java
//Program Time Deposit
package com.BSU.TimeDeposit;

import java.text.DecimalFormat;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends Activity implements View.OnClickListener, DialogInterface.OnClickListener


{
//Data Members
private double PrincipalAmount,Rate;
private int Time;
private double CompoundInterest, CompoundAmount;
//GUI
private EditText TxtCustomerName, TxtPrincipalAmount;
private EditText TxtRate, TxtTime;
private TextView LblCompoundInterest, LblCompoundAmount;
private Button BtnExit, BtnCompute;

private AlertDialog.Builder MessageBox;

//Default Constructor
public Main( )
{
PrincipalAmount = Rate = 0.00;
Time = 0;
CompoundAmount = CompoundInterest = 0.00;
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//Hide the Soft Keyboard


getWindow( ).setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

16
//Setup EditText
TxtCustomerName = (EditText) findViewById(R.id.TxtCustomerName);
TxtPrincipalAmount = (EditText) findViewById(R.id.TxtPrincipalAmount);
TxtRate = (EditText) findViewById(R.id.TxtRate);
TxtTime = (EditText) findViewById(R.id.TxtTime);

//Setup TextView
LblCompoundInterest = (TextView) findViewById(R.id.LblCompoundInterest);
LblCompoundAmount = (TextView) findViewById(R.id.LblCompoundAmount);

//Setup Button
BtnCompute = (Button) findViewById(R.id.BtnCompute);
BtnCompute.setOnClickListener(this);

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


BtnExit.setOnClickListener(this);

//Setup the Alert Message Box


MessageBox = new AlertDialog.Builder(this);
MessageBox.setTitle("Lacea");
MessageBox.setIcon(R.drawable.ic_launcher);
MessageBox.setMessage("Do you want to quit?");
MessageBox.setPositiveButton("Yes",this);
MessageBox.setNegativeButton("No", this);
//Set Modal Dialog
MessageBox.setCancelable(false);
}

//Button Click Listener


public void onClick(View v)
{
if(v.equals(BtnCompute))
{
try
{
DecimalFormat Currency = new DecimalFormat("P#,##0.00");

PrincipalAmount = Double.parseDouble(TxtPrincipalAmount.getText( ).toString( ));


Rate = Double.parseDouble(TxtRate.getText( ).toString( ));
Time = Integer.parseInt(TxtTime.getText( ).toString( ));

CompoundInterest = PrincipalAmount * Rate * Time;


CompoundAmount = PrincipalAmount + CompoundInterest;

LblCompoundInterest.setText("Compound Interest is "


+ Currency.format(CompoundInterest));
LblCompoundAmount.setText("Compound Amount is "
+ Currency.format(CompoundAmount));
}
catch (Exception e)
{

}
}
else if(v.equals(BtnExit))
{
MessageBox.show( );
}
}
17
//AlertDialog Click Listener
public void onClick(android.content.DialogInterface dialogInterface, int i)
{
if(i == DialogInterface.BUTTON_POSITIVE)
{
//Terminate the application
finish( );
}
else if(i == DialogInterface.BUTTON_NEGATIVE)
{
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
MessageBox.show( );
return true;
}

return super.onKeyDown(keyCode, event);


}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

Laboratory Hands-On Activity No.2 Fullscreen Output


18
INSTALL TO YOUR CELLPHONE TO RUN AND TEST

1. Browse the folder where you compiled the Time Deposit android project. Browse the sub-folder
bin.

2. Connect your cellphone to your computer using the USB cable.

3. Copy and paste the filename Time Deposit.apk to your cellphone’s memory card.

4. In your cellphone, browse using file explorer and tap the filename Time Deposit.apk to install.
5. Run and Test in your cellphone.
6. If you have a powerful PC with fast video graphics card and high capacity RAM size then
install an Android Emulator such as NOX player and LDPlay. You can install and test the Time
Deposit.apk mobile app in the Android Emulator.

19
LABORATORY HANDS-ON EXERCISE NO.1 – SET A
1. Area of Triangles:

𝒉 = 𝒃 𝐬𝐢𝐧 𝜽

𝒂𝒉
𝑨𝒓𝒆𝒂 =
𝟐
Where:
h = height
a = base
b = hypotenuse
θ = angle in degrees

Write a program that asks the user to input the base, hypotenuse and angle (in degrees) as
decimal numbers. Compute degrees to radians and area of triangle. Display the Area with 2 decimal
places.

2. Length of a Circular Arc:

𝒔=𝒓𝜽
Where:
r = radius of the circle
θ = central angle in radians
s = length of a circular arc

Write a program that asks the user to input the radius and central angle (in degrees) as
decimal numbers. Compute degrees to radians and length of a circular arc. Display the Length with 2
decimal places.

20
LABORATORY HANDS-ON EXERCISE NO.1 – SET B
1. Area of Triangles:

Heron’s Formula
(𝒂 + 𝒃 + 𝒄)
𝒔=
𝟐

𝑨𝒓𝒆𝒂 = √𝒔 (𝒔 − 𝒂)(𝒔 − 𝒃)(𝒔 − 𝒄)

Where:
a, b, c = lengths of three (3) sides of triangle

Write a program that asks the user to input the three (3) sides of a triangle as decimal
numbers. Compute the area of triangle. Display the Area with 2 decimal places.

2. Area of a Circular Sector:

𝒓𝟐 𝜽
𝑨𝒓𝒆𝒂 =
𝟐
Where:
r = radius of the circle
θ = central angle in radians
Area = Area of a Circular Sector

Write a program that asks the user to input the radius and central angle (in degrees) as
decimal numbers. Compute degrees to radians and area of a circular arc. Display the Area with 2
decimal places.

21
LABORATORY HANDS-ON ACTIVITY NO.3
RADIOBUTTON AND CHECKBOX
//Application Name: RadioButton and CheckBox
//Project Name: RadioButton and CheckBox
//Package Name: com.BSU.RadioButtonAndCheckBox
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None
main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0000ff"
android:scrollbarAlwaysDrawVerticalTrack="true"
tools:context=".Main" >

<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Application Title -->
<TableRow android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton and CheckBox"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:visibility="visible"
android:gravity="center"/>
</TableRow>

<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Direction"
android:textColor="#ffff00"/>
</TableRow>

<TableRow>
<RadioGroup
android:id="@+id/DirectionRadioGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<RadioButton
android:id="@+id/RadioBtnNorth"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="North"
22
android:textColor="#ffff00"/>
<RadioButton
android:id="@+id/RadioBtnWest"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="West"
android:textColor="#ffff00"/>
<RadioButton
android:id="@+id/RadioBtnSouth"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="South"
android:textColor="#ffff00"/>
<RadioButton
android:id="@+id/RadioBtnEast"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="East"
android:textColor="#ffff00"/>
</RadioGroup>
</TableRow>

<TableRow>
<TextView
android:id="@+id/LblStatusRadioGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Selected Direction: "
android:textColor="#ffff00"/>
</TableRow>

<TableRow>
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableRow>
<CheckBox
android:id="@+id/ChkRestaurant"
android:text="Restaurant"
android:textColor="#ffff00"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<CheckBox
android:id="@+id/ChkGrocery"
android:text="Grocery"
android:textColor="#ffff00"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<CheckBox
android:id="@+id/ChkMall"
android:text="Mall"
23
android:textColor="#ffff00"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<CheckBox
android:id="@+id/ChkNationalParks"
android:text="National Parks"
android:textColor="#ffff00"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</TableRow>
<TableRow>
<!-- ScrollView to scroll the contents of the TextView -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="80dp"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TextView
android:id="@+id/LblStatusCheckBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Selected Location: "
android:textColor="#ffff00"/>
</ScrollView>
</TableRow>

<TableRow>
<Button
android:id="@+id/BtnExit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Exit"/>
</TableRow>
</TableLayout>
</ScrollView>

values-v14
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>

24
Main.java
package com.BSU.RadioButtonAndCheckBox;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class Main extends Activity implements OnCheckedChangeListener,


android.widget.CompoundButton.OnCheckedChangeListener, OnClickListener
{
//Data Members
private RadioGroup DirectionRadioGroup;
private TextView LblStatusRadioGroup;
private TextView LblStatusCheckBox;
private CheckBox ChkRestaurant;
private CheckBox ChkGrocery;
private CheckBox ChkMall;
private CheckBox ChkNationalParks;
private Button BtnExit;

private StringBuffer[ ] ChkMessage;

//Default Constructor
public Main( )
{
ChkMessage = new StringBuffer[4];

for(int i = 0; i < 4; i++)


{
ChkMessage[i] = new StringBuffer( );
}
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

DirectionRadioGroup = (RadioGroup) findViewById(R.id.DirectionRadioGroup);


//Important: check( ) must be called first before setOnCheckedChangeListener( )
DirectionRadioGroup.check(R.id.RadioBtnNorth);
DirectionRadioGroup.setOnCheckedChangeListener(this);

LblStatusRadioGroup = (TextView) findViewById(R.id.LblStatusRadioGroup);


LblStatusRadioGroup.setText("Selected Direction: North");

ChkRestaurant = (CheckBox) findViewById(R.id.ChkRestaurant);


ChkRestaurant.setChecked(true);
ChkRestaurant.setOnCheckedChangeListener(this);

ChkGrocery = (CheckBox) findViewById(R.id.ChkGrocery);


ChkGrocery.setOnCheckedChangeListener(this);
25
ChkMall = (CheckBox) findViewById(R.id.ChkMall);
ChkMall.setOnCheckedChangeListener(this);

ChkNationalParks = (CheckBox) findViewById(R.id.ChkNationalParks);


ChkNationalParks.setOnCheckedChangeListener(this);

LblStatusCheckBox = (TextView) findViewById(R.id.LblStatusCheckBox);

ChkMessage[0].delete(0,ChkMessage[0].length( ));
ChkMessage[0].append("Selected Location: Restaurant\n");

LblStatusCheckBox.setText(ChkMessage[0].toString( ));

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


BtnExit.setOnClickListener(this);
}

public void onCheckedChanged(android.widget.RadioGroup radioGroup, int i)


{
if(radioGroup.equals(DirectionRadioGroup))
{
switch( i )
{
case – 1:
LblStatusRadioGroup.setText("Choices cleared!");
break;
case R.id.RadioBtnNorth:
LblStatusRadioGroup.setText("Selected Direction: North");
break;
case R.id.RadioBtnWest:
LblStatusRadioGroup.setText("Selected Direction: West");
break;
case R.id.RadioBtnSouth:
LblStatusRadioGroup.setText("Selected Direction: South");
break;
case R.id.RadioBtnEast:
LblStatusRadioGroup.setText("Selected Direction: East");
break;
}
}
}

public void onCheckedChanged(android.widget.CompoundButton compoundButton, boolean b)


{
if(((CheckBox)compoundButton) == ChkRestaurant)
{
if(b == true)
{
ChkMessage[0].delete(0,ChkMessage[0].length( ));
ChkMessage[0].append("Selected Location: Restaurant\n");
}
else
{
ChkMessage[0].delete(0,ChkMessage[0].length( ));
ChkMessage[0].append("UnSelected Location: Restaurant\n");
}
}

26
if(((CheckBox)compoundButton) == ChkGrocery)
{
if(b == true)
{
ChkMessage[1].delete(0, ChkMessage[1].length( ));
ChkMessage[1].append("Selected Location: Grocery\n");
}
else
{
ChkMessage[1].delete(0, ChkMessage[1].length( ));
ChkMessage[1].append("UnSelected Location: Grocery\n");
}
}

if(((CheckBox)compoundButton) == ChkMall)
{
if(b == true)
{
ChkMessage[2].delete(0, ChkMessage[2].length( ));
ChkMessage[2].append("Selected Location: Mall\n");
}
else
{
ChkMessage[2].delete(0, ChkMessage[2].length( ));
ChkMessage[2].append("UnSelected Location: Mall\n");
}
}

if(((CheckBox)compoundButton) == ChkNationalParks)
{
if(b == true)
{
ChkMessage[3].delete(0, ChkMessage[3].length( ));
ChkMessage[3].append("Selected Location: National Parks\n");
}
else
{
ChkMessage[3].delete(0, ChkMessage[3].length( ));
ChkMessage[3].append("UnSelected Location: National Parks\n");
}
}

StringBuilder s = new StringBuilder( );

for(int i = 0; i < 4; i++)


{
s.append(ChkMessage[i].toString( ));
}

LblStatusCheckBox.setText(s.toString( ));
}

@Override
public void onClick(View v)
{
if(v.equals(BtnExit))
{
Runtime.getRuntime( ).exit(0);
}
}
27
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

Laboratory Hands-on Activity No.3 Fullscreen Output

LABORATORY HANDS-ON EXERCISE NO.2 – SET A


Write a program that automates the daily order transactions at the BSU Canteen. The software
must accept order from the customer. It must compute the total bill upon selecting or un-selecting the
product in real-time.

28
LABORATORY HANDS-ON EXERCISE NO.2 – SET B
Write a program that automates the daily order transactions at the BSU Marketing. The
software must accept order from the customer. It must compute the total bill upon selecting or un-
selecting the product in real-time.

LABORATORY HANDS-ON ACTIVITY NO.4


LIST VIEW
//Application Name: List View
//Project Name: List View
//Package Name: com.BSU.ListView
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:orientation="vertical"
android:background="#0000ff"
tools:context=".Main" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="LIST VIEW MENU"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#00ff00"
android:background="#0000ff"
android:visibility="visible"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
<LinearLayout
android:id="@+id/Panel"
29
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#0000ff"
android:orientation="vertical"/>
</LinearLayout>
HOW TO ADD A LAYOUT XML
1. Under res folder, right-click layout folder and point to New then click Android XML File.

2. Type the File name: row.xml and click Next button.

30
3. Click Finish button.

4. Enter the contents of the row.xml


row.xml
<TableLayout 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:stretchColumns="1"
android:background="#0000ff">

<TableRow>
<ImageView
android:id="@+id/icon"
android:layout_width="30sp"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingTop="0px"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>

<TextView
android:id="@+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff00"
android:textSize="30sp" />
</TableRow>
</TableLayout>

31
5. Under res>layout folder, add form1.xml and enter its contents.
form1.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#008000"
android:stretchColumns="1"
tools:context=".Main">

<TableRow android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADDITION"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#008000"
android:visibility="visible"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1st number"
android:textColor="#ffff00"/>
<EditText
android:id="@+id/TxtNum1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="numberSigned|numberDecimal"
android:text=""/>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2nd number"
android:textColor="#ffff00"
/>
<EditText
android:id="@+id/TxtNum2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="numberSigned|numberDecimal"
android:text=""/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/LblSum"
android:layout_width="0dp"
android:layout_height="wrap_content"
32
android:layout_weight="1"
android:text="The sum is 0.00"
android:textColor="#ffff00"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/BtnCompute"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Compute"/>
<Button
android:id="@+id/BtnClose"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Close"/>
</TableRow>
</TableLayout>
6. Under res>layout folder, add form2.xml and enter its contents.
form2.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#008000"
android:stretchColumns="1"
tools:context=".Main">
<TableRow android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBTRACTION"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#008000"
android:visibility="visible"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1st number"
android:textColor="#ffff00"
/>
<EditText
android:id="@+id/TxtNum1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType=" numberSigned|numberDecimal "
android:text=""/>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dp"
33
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2nd number"
android:textColor="#ffff00"
/>
<EditText
android:id="@+id/TxtNum2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType=" numberSigned|numberDecimal "
android:text=""/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/LblDifference"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="The Difference is 0.00"
android:textColor="#ffff00"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/BtnCompute"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Compute"/>
<Button
android:id="@+id/BtnClose"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Close"/>
</TableRow>
</TableLayout>

7. Open and edit the AndroidManifest.xml and insert the bold text below.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.ListView"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.ListView.Main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
34
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>
</manifest>

values-v14
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>
HOW TO ADD JAVA FILE
1. Under src folder, right-click the package name e.g. com.BSU.Test then point to New and select
Class.

35
2. Enter the Name: FormFragment1 and then click Finish button.

3. Enter the contents of FormFragment1.java

FormFragment1.java
package com.BSU.ListView;

import java.text.DecimalFormat;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class Form1Fragment extends Fragment


{
//Data Members
private double Num1,Num2, Sum;
//GUI
private EditText TxtNum1, TxtNum2;
private TextView LblSum;
private Button BtnCompute;

private View view;

36
private Button BtnClose;
private ListView lv;

//Default Constructor
public Form1Fragment(ListView lv)
{
this.lv = lv;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{

view = inflater.inflate(R.layout.form1, container, false);

return view;
}

@Override
public void onStart( )
{
super.onStart( );

TxtNum1 = (EditText) view.findViewById(R.id.TxtNum1);


TxtNum2 = (EditText) view.findViewById(R.id.TxtNum2);
LblSum = (TextView) view.findViewById(R.id.LblSum);

BtnClose = (Button) view.findViewById(R.id.BtnClose);

BtnClose.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
view.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
}
});

BtnCompute = (Button) view.findViewById(R.id.BtnCompute);

BtnCompute.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
try
{
DecimalFormat Number = new DecimalFormat("#,##0.00");

Num1 = Double.parseDouble(TxtNum1.getText( ).toString( ));


Num2 = Double.parseDouble(TxtNum2.getText( ).toString( ));
Sum = Num1 + Num2;

LblSum.setText("The Sum is " + Number.format(Sum));


}
catch (Exception e)
{
}
}
});
}
37
}
4. Add Java filename: FormFragment2.java and enter the contents.
FormFragment2.java
package com.BSU.ListView;

import java.text.DecimalFormat;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class Form2Fragment extends Fragment


{
//Data Members
private double Num1,Num2, Difference;
//GUI
private EditText TxtNum1, TxtNum2;
private TextView LblDifference;
private Button BtnCompute;

private View view;


private Button BtnClose;
private ListView lv;

//Default Constructor
public Form2Fragment(ListView lv)
{
this.lv = lv;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.form2, container, false);

return view;
}

@Override
public void onStart( )
{
super.onStart( );

TxtNum1 = (EditText) view.findViewById(R.id.TxtNum1);


TxtNum2 = (EditText) view.findViewById(R.id.TxtNum2);
LblDifference = (TextView) view.findViewById(R.id.LblDifference);

BtnClose = (Button) view.findViewById(R.id.BtnClose);

BtnClose.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
view.setVisibility(View.GONE);
38
lv.setVisibility(View.VISIBLE);
}
});

BtnCompute = (Button) view.findViewById(R.id.BtnCompute);

BtnCompute.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
try
{
DecimalFormat Number = new DecimalFormat("#,##0.00");

Num1 = Double.parseDouble(TxtNum1.getText( ).toString( ));


Num2 = Double.parseDouble(TxtNum2.getText( ).toString( ));
Difference = Num1 – Num2;

LblDifference.setText("The Difference is " + Number.format(Difference));


}
catch (Exception e)
{
}
}
});
}
}

5. Open and edit the source file Main.java


Main.java
package com.BSU.ListView;

import android.os.Bundle;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Main extends ListActivity


{
//Data Members
private final int MAX = 3;
private String[ ] MenuItems;
private ArrayAdapter<String> Adapter;

private Toast Toaster;

private FragmentManager fragmentManager;


private FragmentTransaction fragmentTransaction;
private Form1Fragment Form1Fragment;
private Form2Fragment Form2Fragment;

39
//Default Constructor
public Main( )
{
MenuItems = new String[MAX];
MenuItems[0] = "Addition";
MenuItems[1] = "Subtraction";
MenuItems[2] = "Exit";
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Adapter = new ArrayAdapter<String>(this,R.layout.row, R.id.label1,MenuItems);


setListAdapter(Adapter);

//Specify the layout to use when the list of choices appears


Adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);

getListView( ).setTextFilterEnabled(true);

//Tell the list view to show one checked/activated item at a time.


getListView( ).setChoiceMode(ListView.CHOICE_MODE_SINGLE);

//Start with first item activated.


//Make the newly clicked item the currently selected one.
getListView( ).setItemChecked(0, true);

Toaster = Toast.makeText(this, " ", 5000);


Toaster.setGravity(Gravity.BOTTOM,0,0);
}

//ListView Item Click Listener


public void onListItemClick(ListView parent, View v, int position, long id)
{
Toaster.setText(MenuItems[position]);

switch(position)
{
case 0: //Addition
getListView( ).setVisibility(View.GONE);

Form1Fragment = new Form1Fragment(getListView( ));


//get an instance of FragmentTransaction from your Activity
fragmentManager = getFragmentManager( );
fragmentTransaction = fragmentManager.beginTransaction( );

//add a fragment
fragmentTransaction.replace(R.id.Panel, Form1Fragment);
fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit( );

Toaster.show( );
break;
case 1: //Subtraction
getListView( ).setVisibility(View.GONE);

Form2Fragment = new Form2Fragment(getListView( ));


40
// get an instance of FragmentTransaction from your Activity
fragmentManager = getFragmentManager( );
fragmentTransaction = fragmentManager.beginTransaction( );

//add a fragment
fragmentTransaction.replace(R.id.Panel, Form2Fragment);
fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit( );

Toaster.show( );
break;
case 2: //Exit
//Terminate the application
Runtime.getRuntime( ).exit(0);
break;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

Laboratory Hands-On Activity No.4 Fullscreen Output

form1.xml form2.xml

41
LABORATORY HANDS-ON EXERCISE NO.3 – SET A
1. The value of 𝜋 can be determined by the series equation.
1 1 1 1 1 1 (−1)𝑛+1
𝜋 = 4 𝑥 (1 − + − + − + − ...+ )
3 5 7 9 11 13 2𝑛 − 1
Write a program to approximate the value of 𝜋 using the formula given. Ask the user to input the
nth times of iteration as integer. Display the approximate value of pi.

2. Write a program that accepts a decimal value x to compute the cosine and the number of nth
iteration as integer to approximate using the Taylor Series. Display the cosine of that decimal
value with 5 decimal values.
TAYLOR SERIES OF COSINE

𝒙𝟐 𝒙𝟒 𝒙𝟔 (−𝒙)𝟐𝒏
𝒄𝒐𝒔 𝒙 = 𝟏 − + − +. . .
𝟐! 𝟒! 𝟔! 𝟐𝒏!

LABORATORY HANDS-ON EXERCISE NO.3 – SET B


1. Write a program that accepts an integer x and the nth iteration as integer. Display the
approximate value of e raise to x.
Natural Exponential Function 𝒆𝒙 Approximation
𝒙
𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝒏
𝒆 = 𝟏+ + + + ...+
𝟏! 𝟐! 𝟑! 𝒏!
Where:
x = the exponent number
n = nth iteration
2. Write a program that accepts a decimal value x to compute the sine and the number of nth
iteration as integer to approximate using the Taylor Series. Display the sine of that decimal
value with 5 decimal values.
TAYLOR SERIES OF SINE

𝒙 𝒙𝟑 𝒙𝟓 𝒙𝟕 (−𝒙)(𝟐𝒏−𝟏)
𝒔𝒊𝒏 𝒙 = − + − + . . .
𝟎! 𝟑! 𝟓! 𝟕! (𝟐𝒏 − 𝟏)!

42
LABORATORY HANDS-ON ACTIVITY NO.5
SPINNER
//Application Name: Spinner
//Project Name: Spinner
//Package Name: com.BSU.Spinner
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None
main.xml
<TableLayout 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:stretchColumns="1"
android:background="#0000ff"
tools:context=".Main" >

<!-- Application Title -->


<TableRow android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SPINNER"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:visibility="visible"
android:gravity="center"/>
</TableRow>

<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Prepaid Card"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#00ff00"
android:background="#0000ff"
android:visibility="visible"/>
<Spinner
android:id="@+id/SpinnerPrepaidCard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="14sp"/>
</TableRow>

<TableRow>
<TextView
android:id="@+id/LblSpinnerValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
43
android:text="You have selected"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#00ff00"
android:background="#0000ff"
android:visibility="visible"/>
</TableRow>

<!-- LinearLayout just to center the button exit -->


<TableRow android:gravity="center">
<Button
android:id="@+id/BtnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"/>
</TableRow>
</TableLayout>

Under res>values folder, add Denominations.xml and enter its contents.

Denominations.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="Denominations">
<item>100</item>
<item>300</item>
<item>500</item>
<item>1000</item>
</string-array>
</resources>
values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
</style>
</resources>
Main.java
package com.BSU.Spinner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class Main extends Activity


{
//Data Members
private Spinner SpinnerDenominations;
private TextView LblSpinnerValue;
private Button BtnExit;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

44
LblSpinnerValue = (TextView) findViewById(R.id.LblSpinnerValue);
SpinnerDenominations = (Spinner) findViewById(R.id.SpinnerPrepaidCard);

// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> AdapterDenomination =
ArrayAdapter.createFromResource(this,R.array.Denominations,
android.R.layout.simple_spinner_item);

// Specify the layout to use when the list of choices appears


AdapterDenomination.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// Apply the adapter to the spinner


SpinnerDenominations.setAdapter(AdapterDenomination);
SpinnerDenominations.setSelection(0);

SpinnerDenominations.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener( ) {
@Override
public void onItemSelected(AdapterView<?> parent, View view,int pos, long id)
{
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
LblSpinnerValue.setText("You have selected P" +
parent.getItemAtPosition(pos).toString( ) + " denomination.");
}

@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
BtnExit = (Button) findViewById(R.id.BtnExit);

BtnExit.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
//Terminate the application
Runtime.getRuntime( ).exit(0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

Laboratory Hands-On Activity No.5 Fullscreen Output

45
LABORATORY HANDS-ON EXERCISE NO.4 – SET A
An auto mechanic charges a 40% markup based on cost for parts. What would the price be for
an air filter that cost him $14.95? What is the dollar amount of his markup on this item.
Markup based on Cost
𝑆𝑒𝑙𝑙𝑖𝑛𝑔 𝑃𝑟𝑖𝑐𝑒 = 𝐶𝑜𝑠𝑡 𝑥 (1 + 𝑟)
Where:
r = Percent Markup
a. Selling Price
40
𝑆𝑒𝑙𝑙𝑖𝑛𝑔 𝑃𝑟𝑖𝑐𝑒 = $14.95 𝑥 (1 + ) = $20.93
100
b. Markup Cost
𝑀𝑎𝑟𝑘𝑢𝑝 𝐶𝑜𝑠𝑡 = 𝐶𝑜𝑠𝑡 𝑥 𝑟
40
𝑀𝑎𝑟𝑘𝑢𝑝 𝐶𝑜𝑠𝑡 = $14.95 𝑥 = $5.98
100
Or
𝑀𝑎𝑟𝑘𝑢𝑝 𝐶𝑜𝑠𝑡 = 𝑆𝑒𝑙𝑙𝑖𝑛𝑔 𝑃𝑟𝑖𝑐𝑒 − 𝐶𝑜𝑠𝑡
𝑀𝑎𝑟𝑘𝑢𝑝 𝐶𝑜𝑠𝑡 = $20.93 − $14.95 = $5.98
Write a program that prompts the user to enter the original cost and percent markup as decimals.
Compute the selling price and markup cost. Here is a sample run:
Please enter original cost of the item> 14.95
Please enter the percent markup> 40
The selling price is $20.93
The markup cost is $5.98
 Apply Spinner for percent markup. Spinner values are 10, 20, 30, 40, 50

LABORATORY HANDS-ON EXERCISE NO.4 – SET B


At its Anniversary Day Sale, a furniture store is offering 15% off everything in the store. What
would the sale price be for a sofa that normally sells for $1,279.95? What is the dollar amount of the
mark down?
Markdown based on Cost
𝑆𝑒𝑙𝑙𝑖𝑛𝑔 𝑃𝑟𝑖𝑐𝑒 = 𝐶𝑜𝑠𝑡 𝑥 (1 − 𝑟)
Where:
r = Percent Markdown
a. Selling Price
15
𝑆𝑒𝑙𝑙𝑖𝑛𝑔 𝑃𝑟𝑖𝑐𝑒 = $1,279.95 𝑥 (1 − ) = $1,087.96
100
b. Markdown Cost
𝑀𝑎𝑟𝑘𝑑𝑜𝑤𝑛 𝐶𝑜𝑠𝑡 = 𝐶𝑜𝑠𝑡 𝑥 𝑟
15
𝑀𝑎𝑟𝑘𝑑𝑜𝑤𝑛 𝐶𝑜𝑠𝑡 = $1,279.95 𝑥 = $191.99
100
Or
𝑀𝑎𝑟𝑘𝑑𝑜𝑤𝑛 𝐶𝑜𝑠𝑡 = 𝑆𝑒𝑙𝑙𝑖𝑛𝑔 𝑃𝑟𝑖𝑐𝑒 − 𝐶𝑜𝑠𝑡
𝑀𝑎𝑟𝑘𝑑𝑜𝑤𝑛 𝐶𝑜𝑠𝑡 = $1,087.96 − $1,279.95 = $191.99
Write a program that prompts the user to enter the original cost and percent markdown as
decimals. Compute the selling price and markdown cost. Here is a sample run:
Please enter original cost of the item> 1279.95
Please enter the percent markdown> 15
The selling price is $1,087.96
The markdown cost is $191.99
 Apply Spinner for percent markdown. Spinner values are 5, 10, 15, 20, 25
46
LABORATORY HANDS-ON ACTIVITY NO.6
DATE TIME PICKER
//Application Name: Date Time Picker
//Project Name: Date Time Picker
//Package Name: com.BSU.DateTimePicker
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None
main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0000ff"
android:scrollbarAlwaysDrawVerticalTrack="true"
tools:context=".Main">

<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">

<!-- Application Title -->


<TableRow android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date Time Picker"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:visibility="visible"
android:gravity="center"/>
</TableRow>

<TableRow android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter the date"
android:textColor="#ffff00"
android:gravity="center"/>
</TableRow>

<TableRow android:gravity="center">
<DatePicker
android:id="@+id/DatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>

<TableRow android:gravity="center">
<TextView
android:layout_width="wrap_content"
47
android:layout_height="wrap_content"
android:text="Enter the time"
android:textColor="#ffff00"
android:gravity="center"/>
</TableRow>

<TableRow android:gravity="center">
<TimePicker
android:id="@+id/TimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>

<TableRow android:gravity="center">
<TextView
android:id="@+id/LblDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff00"
android:gravity="center"/>
</TableRow>

<TableRow android:gravity="center">
<TextView
android:id="@+id/LblTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff00"
android:gravity="center"/>
</TableRow>

<TableRow android:gravity="center">
<AnalogClock
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>

<TableRow android:gravity="center">
<DigitalClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffff00"/>
</TableRow>
<!-- LinearLayout just to center the button exit -->
<TableRow android:gravity="center">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/BtnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"/>
</LinearLayout>
</TableRow>
</TableLayout>
</ScrollView>
48
values-v14
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.DateTimePicker"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.DateTimePicker.Main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>
Main.java
package com.BSU.DateTimePicker;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Formatter;

public class Main extends Activity implements DatePicker.OnDateChangedListener,


TimePicker.OnTimeChangedListener,View.OnClickListener
{
//Data Members
private TextView LblDatePicker, LblTimePicker;
private Button BtnExit;
49
private DatePicker DP;
private TimePicker TP;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

DP = (DatePicker) findViewById(R.id.DatePicker);
DP.init(DP.getYear( ),DP.getMonth( ),DP.getDayOfMonth( ),this);
TP = (TimePicker) findViewById(R.id.TimePicker);
TP.setOnTimeChangedListener(this);

LblDatePicker = (TextView) findViewById(R.id.LblDatePicker);


LblTimePicker = (TextView) findViewById(R.id.LblTimePicker);

Formatter TimeFormat = new Formatter( );

if(TP.is24HourView( ) == false)
{
String AmPm = null;
int Hour = TP.getCurrentHour( );

TP.setIs24HourView(true);
if(Hour >= 12 && Hour <=23)
{
AmPm = "pm";

if(Hour == 12)
{
Hour = 12;
}
else
{
Hour = Hour % 12;
}
}
else if(Hour >= 0 && Hour <=11)
{
AmPm = "am";

if(Hour == 0)
{
Hour = 12;
}
}

TP.setIs24HourView(false);

Hour = Hour % 24;

TimeFormat.format("Time: %d:%02d %s", Hour, TP.getCurrentMinute( ), AmPm);


}
else
{
TimeFormat.format("Time: %d:%02d", TP.getCurrentHour(), TP.getCurrentMinute( ));
}

50
// The month, and just the month, is zero-based. Add 1 for display.
LblDatePicker.setText("Date:" + (DP.getMonth( ) + 1) + "/" + DP.getDayOfMonth( ) + "/"
+ DP.getYear( ));
LblTimePicker.setText(TimeFormat.toString( ));

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


BtnExit.setOnClickListener(this);
}

//DatePicker onDateChanged Listener


public void onDateChanged(android.widget.DatePicker datePicker, int i, int i1, int i2)
{
LblDatePicker.setText("Date:" + (i1 + 1) + "/" + i2 + "/" + i);
}

//TimePicker onTimeChanged Listener


public void onTimeChanged(android.widget.TimePicker timePicker, int i, int i1)
{
Formatter f = new Formatter( );

if(timePicker.is24HourView( ) == false)
{
String AmPm = null;
int Hour = 0;
timePicker.setIs24HourView(true);

if(i >= 12 && i <=23)


{
AmPm = "pm";

if(i == 12)
{
Hour = 12;
}
else
{
Hour = i % 12;
}

}
else if(i >= 0 && i <=11)
{
AmPm = "am";

if(i == 0)
{
Hour = 12;
}
else
{
Hour = i;
}
}

timePicker.setIs24HourView(false);

f.format("Time: %d:%02d %s", Hour, i1, AmPm);


}
else
{
51
f.format("Time: %d:%02d", i, i1);
}

LblTimePicker.setText(f.toString( ));
}

//Button onClick Listener


public void onClick(View v)
{
if(v.equals(BtnExit))
{
//Terminate Application
Runtime.getRuntime( ).exit(0);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

Scroll Down Scroll Up


Laboratory Hands-On Activity No.6 Fullscreen Output

52
LABORATORY HANDS-ON ACTIVITY NO.7
ACTION BAR
//Application Name: Action Bar
//Project Name: Action Bar
//Package Name: com.BSU.ActionBar
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

Under res>layout folder, edit main.xml and enter its contents.


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:background="#0000ff"
tools:context=".Main" >

<ScrollView
android:id="@+id/Panel"
android:fillViewport="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0000ff"
android:scrollbarAlwaysDrawVerticalTrack="true"/>
</LinearLayout>

Under res>layout folder, add form1.xml and enter its contents.


form1.xml
<TableLayout 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:stretchColumns="1"
android:background="#0000ff">

<TableRow android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FORM 1"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:visibility="visible"
android:layout_gravity="center"/>
</TableRow>

<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Customer Name"
android:textSize="14sp"
android:textStyle="bold"
53
android:textColor="#00ff00"
android:background="#0000ff"
android:visibility="visible"/>
<EditText
android:id="@+id/TxtCustomerName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="text|textAutoCorrect"
android:text=""/>
</TableRow>

<TableRow android:gravity="center">
<Button
android:id="@+id/BtnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:layout_gravity="center"/>
</TableRow>
</TableLayout>

Under res>layout folder, add form2.xml and enter its contents.


form2.xml
<TableLayout 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:stretchColumns="1"
android:background="#0000ff">

<TableRow android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FORM 2"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:visibility="visible"
android:layout_gravity="center"/>
</TableRow>

<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose one (1) Programming Language"
android:textColor="#ffff00"/>
</TableRow>

<TableRow>
<RadioGroup
android:id="@+id/ProgrammingRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/RadioBtnJava"
54
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:textColor="#ffff00"/>
<RadioButton
android:id="@+id/RadioBtnCSharp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C#"
android:textColor="#ffff00"/>
<RadioButton
android:id="@+id/RadioBtnCPlusPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++"
android:textColor="#ffff00"/>
<RadioButton
android:id="@+id/RadioBtnVB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Visual Basic"
android:textColor="#ffff00"/>
</RadioGroup>
</TableRow>

<TableRow android:gravity="center">
<Button
android:id="@+id/BtnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:layout_gravity="center"/>
</TableRow>
</TableLayout>

Under res>layout folder, add form3.xml and enter its contents.


form3.xml
<TableLayout 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:stretchColumns="1"
android:background="#0000ff">

<TableRow android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FORM 3"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:visibility="visible"
android:layout_gravity="center"/>
</TableRow>

<TableRow>
<TextView
android:layout_width="fill_parent"
55
android:layout_height="wrap_content"
android:text="Choose any Development Platform"
android:textColor="#ffff00"/>
</TableRow>

<TableRow>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<CheckBox
android:id="@+id/ChkMobile"
android:text="Mobile"
android:textColor="#ffff00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/ChkPC"
android:text="PC"
android:textColor="#ffff00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</TableRow>

<TableRow android:gravity="center">
<Button
android:id="@+id/BtnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:layout_gravity="center"/>
</TableRow>
</TableLayout>
Values-v11
<resources>
<style name="AppBaseTheme" parent="android:Theme.WithActionBar">
</style>
</resources>
values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.WithActionBar">
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.ActionBar"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
56
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.ActionBar.Main"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:uiOptions="splitActionBarWhenNarrow"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Under res>menu folder, edit menu.xml and enter the following codes.
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/BtnForm1"
android:title="Form1"
android:icon="@drawable/ic_launcher"
android:showAsAction="always|withText"/>

<item android:id="@+id/BtnForm2"
android:title="Form2"
android:icon="@drawable/ic_launcher"
android:showAsAction="always|withText"/>

<item android:id="@+id/BtnForm3"
android:title="Form3"
android:icon="@drawable/ic_launcher"
android:showAsAction=" always|withText "/>

<item android:id="@+id/BtnExit"
android:title="Exit"
android:icon="@drawable/ic_launcher"
android:showAsAction=" always|withText "/>

</menu>

Add Java filename: Form1Fragment.java and enter the contents.


Form1Fragment.java
package com.BSU.ActionBar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Form1Fragment extends Fragment


{
//Data Members
private View view;
private Button BtnClose;

57
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState)
{

view = inflater.inflate(R.layout.form1, container, false);

return view;
}

@Override
public void onStart( )
{
super.onStart( );

BtnClose = (Button) view.findViewById(R.id.BtnClose);

BtnClose.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
view.setVisibility(View.INVISIBLE);
}
});
}
}
Add Java filename: Form2Fragment.java and enter the contents.
Form2Fragment.java
package com.BSU.ActionBar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Form2Fragment extends Fragment


{
//Data Members
private View view;
private Button BtnClose;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState)
{
view = inflater.inflate(R.layout.form2, container, false);

return view;
}

@Override
public void onStart( )
{
super.onStart( );

BtnClose = (Button) view.findViewById(R.id.BtnClose);

BtnClose.setOnClickListener(new View.OnClickListener( ) {
58
@Override
public void onClick(View v)
{
view.setVisibility(View.INVISIBLE);
}
});
}
}
Add Java filename: Form3Fragment.java and enter the contents.
Form3Fragment.java
package com.BSU.ActionBar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Form3Fragment extends Fragment


{
//Data Members
private View view;
private Button BtnClose;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState)
{
view = inflater.inflate(R.layout.form3, container, false);

return view;
}

@Override
public void onStart( )
{
super.onStart( );

BtnClose = (Button) view.findViewById(R.id.BtnClose);

BtnClose.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
view.setVisibility(View.INVISIBLE);
}
});
}
}
Main.java
package com.BSU.ActionBar;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
59
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.Toast;

public class Main extends Activity


{
//Data Members
private AlertDialog.Builder AskUserQuitMessageBox;
private ActionBar ActionBar;

private FragmentManager fragmentManager;


private FragmentTransaction fragmentTransaction;
private Form1Fragment Form1Fragment;
private Form2Fragment Form2Fragment;
private Form3Fragment Form3Fragment;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ActionBar = getActionBar( );
ActionBar.setDisplayHomeAsUpEnabled(true);

//Setup the Alert Message Box


AskUserQuitMessageBox = new AlertDialog.Builder(this);
AskUserQuitMessageBox.setTitle("Programmer: John B. Lacea");
AskUserQuitMessageBox.setIcon(R.drawable.ic_launcher);
AskUserQuitMessageBox.setMessage("Do you want to quit?");
AskUserQuitMessageBox.setPositiveButton("Yes",AskUserQuitMessageBoxListener);
AskUserQuitMessageBox.setNegativeButton("No", AskUserQuitMessageBoxListener);
//Set Modal Dialog
AskUserQuitMessageBox.setCancelable(false);

//Hide the Soft Keyboard


getWindow( ).setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

//AskUserQuitMessageBox Click Listener


DialogInterface.OnClickListener AskUserQuitMessageBoxListener = new
DialogInterface.OnClickListener( )
{
@Override
public void onClick(DialogInterface dialog, int i)
{
if(i == DialogInterface.BUTTON_POSITIVE)
{
//Terminate the application
finish( );
}
else if(i == DialogInterface.BUTTON_NEGATIVE)
{

}
}
};

60
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId( ))
{
case android.R.id.home:
Toast.makeText(getApplicationContext( ), "Your have
pressed the Application Icon.", Toast.LENGTH_LONG).show( );
return true;
case R.id.BtnForm1:
Form1Fragment = new Form1Fragment( );

//get an instance of FragmentTransaction from your Activity


fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction( );

//add a fragment

fragmentTransaction.replace(R.id.Panel, Form1Fragment);
fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit( );
Toast.makeText(getApplicationContext( ),
"Form 1 selected", Toast.LENGTH_LONG).show( );
return true;
case R.id.BtnForm2:
Form2Fragment = new Form2Fragment( );

// get an instance of FragmentTransaction from your Activity


fragmentManager = getFragmentManager( );
fragmentTransaction = fragmentManager.beginTransaction( );

//add a fragment

fragmentTransaction.replace(R.id.Panel, Form2Fragment);
fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit( );
Toast.makeText(getApplicationContext( ),
"Form 2 selected", Toast.LENGTH_LONG).show( );
return true;
case R.id.BtnForm3:
Form3Fragment = new Form3Fragment( );

// get an instance of FragmentTransaction from your Activity


fragmentManager = getFragmentManager( );
fragmentTransaction = fragmentManager.beginTransaction( );

//add a fragment

fragmentTransaction.replace(R.id.Panel, Form3Fragment);
fragmentTransaction.addToBackStack(null);
61
fragmentTransaction.commit( );
Toast.makeText(getApplicationContext( ),
"Form 3 selected", Toast.LENGTH_LONG).show( );
return true;
case R.id.BtnExit:
AskUserQuitMessageBox.show( );
return true;
}

return true;
}
}

Press Ctrl – F11 to simulate rotating the Android device to Landscape


Laboratory Hands-On Activity No.7 normal screen Output

62
LABORATORY HANDS-ON EXERCISE NO.5 – SET A
1. Linear Function:
𝒚𝟐 − 𝒚𝟏
𝑺𝒍𝒐𝒑𝒆 𝒎 =
𝒙𝟐 − 𝒙𝟏
Parallel and Perpendicular Lines
𝑻𝒘𝒐 (𝟐) 𝑳𝒊𝒏𝒆𝒔 𝒂𝒓𝒆 𝒑𝒂𝒓𝒂𝒍𝒍𝒆𝒍 𝒊𝒇 𝒂𝒏𝒅 𝒐𝒏𝒍𝒚 𝒊𝒇 𝒎𝟏 = 𝒎𝟐
𝑻𝒘𝒐 (𝟐) 𝑳𝒊𝒏𝒆𝒔 𝒂𝒓𝒆 𝒑𝒆𝒓𝒑𝒆𝒏𝒅𝒊𝒄𝒖𝒍𝒂𝒓 𝒊𝒇 𝒂𝒏𝒅 𝒐𝒏𝒍𝒚 𝒊𝒇 𝒎𝟏 𝒎𝟐 = −𝟏

Angle between two (2) lines with slopes 𝒎𝟏 𝒂𝒏𝒅 𝒎𝟐


𝒎𝟐 − 𝒎𝟏
𝑹𝒂𝒅𝒊𝒂𝒏 = 𝒕𝒂𝒏−𝟏 ( )
𝟏 + 𝒎𝟏 𝒎𝟐
Convert Radians to Degrees
𝟏𝟖𝟎°
𝜽 = 𝑹𝒂𝒅𝒊𝒂𝒏 𝒙
𝝅
Write a program that prompts the user to input two (2) lines. Each line has two (2) points and
each point has x and y coordinates as integers. Determine if the two (2) lines are parallel or
perpendicular and compute and display the angle between the two (2) lines in degrees with two (2)
decimal places.

2. Richter Scale
The National Earthquake Information Center has asked you to write a program implementing
the following decision table to characterize an earthquake based on its Richter scale number.
The user must input the Richter Scale as decimal number and display the characterization.

Richter Scale Number (n) Characterization


n < 5.0 Little or no damage
5.0 ≤ n < 5.5 Some damage
5.5 ≤ n < 6.5 Serious damage: walls may crack or fall
6.5 ≤ n < 7.5 Disaster: houses and buildings may collapse

63
LABORATORY HANDS-ON EXERCISE NO.5 – SET B
1. Linear Function:
Standard Linear Equation
𝒂𝒙 + 𝒃𝒚 + 𝒄 = 𝟎
−𝒂
𝑺𝒍𝒐𝒑𝒆 𝒎 =
𝒃
Parallel and Perpendicular Lines
𝒍𝒊𝒏𝒆 𝟏: 𝒂𝟏 𝒙 + 𝒃𝟏 𝒚 + 𝒄𝟏 = 𝟎
𝒍𝒊𝒏𝒆 𝟐: 𝒂𝟐 𝒙 + 𝒃𝟐 𝒚 + 𝒄𝟐 = 𝟎
𝑻𝒘𝒐 (𝟐) 𝑳𝒊𝒏𝒆𝒔 𝒂𝒓𝒆 𝒑𝒂𝒓𝒂𝒍𝒍𝒆𝒍 𝒊𝒇 𝒂𝒏𝒅 𝒐𝒏𝒍𝒚 𝒊𝒇 𝒂𝟏 𝒃𝟐 = 𝒃𝟏 𝒂𝟐
𝑻𝒘𝒐 (𝟐) 𝑳𝒊𝒏𝒆𝒔 𝒂𝒓𝒆 𝒑𝒆𝒓𝒑𝒆𝒏𝒅𝒊𝒄𝒖𝒍𝒂𝒓 𝒊𝒇 𝒂𝒏𝒅 𝒐𝒏𝒍𝒚 𝒊𝒇 𝒂𝟏 𝒂𝟐 + 𝒃𝟏 𝒃𝟐 = 𝟎
Angle between two (2) lines with slopes 𝒎𝟏 𝒂𝒏𝒅 𝒎𝟐
𝒎𝟐 − 𝒎𝟏
𝑹𝒂𝒅𝒊𝒂𝒏 = 𝒕𝒂𝒏−𝟏 ( )
𝟏 + 𝒎𝟏 𝒎𝟐
Convert Radians to Degrees
𝟏𝟖𝟎°
𝜽 = 𝑹𝒂𝒅𝒊𝒂𝒏 𝒙
𝝅
Write a program that prompts the user to input two (2) lines and each line contains a, b
and c as integers. Determine if the two (2) lines are parallel or perpendicular and compute and
display the angle between the two (2) lines in degrees with two (2) decimal places.
2. Cartesian Plane

Cartesian Plane
Write a program that takes the (x, y) coordinates of a point in the Cartesian plane as decimals and
prints a message telling either an axis on which the point lies or the quadrant in which it is found.
Example:
Please enter the x-axis of the point> – 1.0
Please enter the y-axis of the point> – 2.5
The point (– 1.0, – 2.5) is in quadrant III
Please enter the x-axis of the point> 0.0
Please enter the y-axis of the point> 4.8
The point (0.0, 4.8) is on the y-axis

64
LABORATORY HANDS-ON ACTIVITY NO.8
ACCELEROMETER
//Application Name: Accelerometer
//Project Name: Accelerometer
//Package Name: com.BSU.Accelerometer
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

Under res>layout folder, edit main.xml and enter its contents.


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:background="#0000ff"
android:orientation="vertical"
tools:context=".Main" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ACCELEROMETER"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#00ffff"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblAccelerationLEFT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#00ff00"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblAccelerationRIGHT"
65
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#00ff00"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblAccelerationUP"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#00ff00"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblAccelerationDOWN"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#00ff00"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblAccelerationFORWARD"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#00ff00"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblAccelerationBACKWARD"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#00ff00"
android:gravity="center"
android:visibility="visible"/>

</LinearLayout>

66
values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
</style>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.Accelerometer"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<uses-feature
android:required="true"
android:name="android.hardware.sensor.accelerometer"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.Accelerometer.Main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>
Main.java
package com.BSU.Accelerometer;

import java.util.Formatter;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class Main extends Activity implements SensorEventListener


{
//Data Members
private static final int MAX = 3;
private TextView LblMessage;
private TextView LblAccelerationLEFT;
private TextView LblAccelerationRIGHT;
67
private TextView LblAccelerationUP;
private TextView LblAccelerationDOWN;
private TextView LblAccelerationFORWARD;
private TextView LblAccelerationBACKWARD;
private SensorManager SensorMngr;
private Sensor Accelerometer;
private float[ ] Gravity;
private float[ ] LinearAcceleration;
private StringBuilder Result;
private Formatter Format;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Result = new StringBuilder( );


Format = new Formatter(Result);

Gravity = new float[MAX];

for(int i = 0; i < MAX; i++)


{
Gravity[i] = 9.81f;
}

LinearAcceleration = new float[MAX];

LblMessage = (TextView) findViewById(R.id.LblMessage);


LblAccelerationLEFT = (TextView) findViewById(R.id.LblAccelerationLEFT);
LblAccelerationRIGHT = (TextView) findViewById(R.id.LblAccelerationRIGHT);
LblAccelerationUP = (TextView) findViewById(R.id.LblAccelerationUP);
LblAccelerationDOWN = (TextView) findViewById(R.id.LblAccelerationDOWN);
LblAccelerationFORWARD = (TextView) findViewById(R.id.LblAccelerationFORWARD);
LblAccelerationBACKWARD = (TextView) findViewById(R.id.LblAccelerationBACKWARD);

SensorMngr = (SensorManager) this.getSystemService(SENSOR_SERVICE);

if (SensorMngr.getSensorList(Sensor.TYPE_ACCELEROMETER).size( ) == 0)
{
LblMessage.setText("No accelerometer sensor is installed!");
}
else
{
Accelerometer = SensorMngr.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);

if (!SensorMngr.registerListener(this, Accelerometer, SensorManager.SENSOR_DELAY_GAME))


{
LblMessage.setText("Couldn't register sensor listener");
}
}
}

68
@Override
protected void onResume( )
{
// Register a listener for the sensor.
super.onResume( );
SensorMngr.registerListener(this, Accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause( )
{
// Be sure to unregister the sensor when the activity pauses.
super.onPause( );
SensorMngr.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event)
{
// In this example, alpha is calculated as t / (t + dT),
// where t is the low-pass filter's time-constant and
// dT is the event delivery rate.

final float Alpha = 0.8f;

// Isolate the force of gravity with the low-pass filter.


Gravity[0] = Alpha * Gravity[0] + (1 - Alpha) * event.values[0];
Gravity[1] = Alpha * Gravity[1] + (1 - Alpha) * event.values[1];
Gravity[2] = Alpha * Gravity[2] + (1 - Alpha) * event.values[2];

// Remove the gravity contribution with the high-pass filter.


LinearAcceleration[0] = event.values[0] - Gravity[0];
LinearAcceleration[1] = event.values[1] - Gravity[1];
LinearAcceleration[2] = event.values[2] - Gravity[2];

Result.setLength(0);
Format.format("x: %.5f",LinearAcceleration[0]);
Format.format(" y: %.5f",LinearAcceleration[1]);
Format.format(" z: %.5f",LinearAcceleration[2]);

LblMessage.setText(Result.toString( ));

LblAccelerationLEFT.setText(null);
LblAccelerationRIGHT.setText(null);
LblAccelerationUP.setText(null);
LblAccelerationDOWN.setText(null);
LblAccelerationFORWARD.setText(null);
LblAccelerationBACKWARD.setText(null);

if(LinearAcceleration[0] > 0f)


{
LblAccelerationLEFT.setText("Moves to the LEFT.");
}
else if(LinearAcceleration[0] < 0f)
{
LblAccelerationRIGHT.setText("Moves to the RIGHT.");
}

69
if(LinearAcceleration[1] > 0f)
{
LblAccelerationUP.setText("Moves UP.");
}
else if(LinearAcceleration[1] < 0f)
{
LblAccelerationDOWN.setText("Moves DOWN.");
}

if(LinearAcceleration[2] > 0f)


{
LblAccelerationFORWARD.setText("Moves FORWARD.");
}
else if(LinearAcceleration[2] < 0f)
{
LblAccelerationBACKWARD.setText("Moves BACKWARD.");
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

Laboratory Hands-On Activity No.8 Fullscreen Output


70
LABORATORY HANDS-ON ACTIVITY NO.9
COMPASS
//Application Name: Compass
//Project Name: Compass
//Package Name: com.BSU.Compass
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

Under res>layout folder, edit main.xml and enter its contents.


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:background="#0000ff"
android:orientation="vertical"
tools:context=".Main" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Digital Compass"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#0000ff"
android:background="#00ffff"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblValues"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:gravity="left"
android:visibility="visible"/>

<TextView
android:id="@+id/LblDirection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:gravity="left"
android:visibility="visible"/>

</LinearLayout>

71
values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
</style>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.Compass"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<uses-feature
android:required="true"
android:name="android.hardware.sensor.compass"/>

<uses-feature
android:required="true"
android:name="android.hardware.sensor.accelerometer"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.Compass.Main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Main.java
package com.BSU.Compass;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.TextView;

72
public class Main extends Activity implements SensorEventListener
{
//Data Members
private static final int MAX = 3;
private SensorManager SensorMngr;
private Sensor Compass;
private Sensor Accelerometer;
private TextView LblValues, LblDirection;

private float[ ] Gravity;


private float[ ] MagneticField;
private float[ ] Values;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Values = new float[MAX];


LblValues = (TextView) findViewById(R.id.LblValues);
LblDirection = (TextView) findViewById(R.id.LblDirection);

SensorMngr = (SensorManager) this.getSystemService(SENSOR_SERVICE);

//Digital Compass - Magnetometer


if (SensorMngr.getSensorList(Sensor.TYPE_MAGNETIC_FIELD).size( ) == 0)
{
LblValues.setText("No magnetometer sensor is installed!");
}
else
{
Compass = SensorMngr.getSensorList(Sensor.TYPE_MAGNETIC_FIELD).get(0);

if (!SensorMngr.registerListener(this, Compass, SensorManager.SENSOR_DELAY_GAME))


{
LblValues.setText("Couldn't register sensor listener");
}
}

//Accelerometer
if (SensorMngr.getSensorList(Sensor.TYPE_ACCELEROMETER).size( ) == 0)
{
LblDirection.setText("No accelerometer sensor is installed!");
}
else
{
Accelerometer = SensorMngr.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);

if (!SensorMngr.registerListener(this, Accelerometer, SensorManager.SENSOR_DELAY_GAME))


{
LblDirection.setText("Couldn't register sensor listener");
}
}

//Hide the Soft Keyboard


getWindow( ).setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

73
@Override
protected void onResume( )
{
super.onResume( );

SensorMngr.registerListener(this, Compass, SensorManager.SENSOR_DELAY_NORMAL);


SensorMngr.registerListener(this, Accelerometer, SensorManager.SENSOR_DELAY_NORMAL);

@Override
protected void onPause( )
{
super.onPause( );
SensorMngr.unregisterListener(this, Compass);
SensorMngr.unregisterListener(this, Accelerometer);
}

@Override
public void onSensorChanged(SensorEvent event)
{
switch(event.sensor.getType( ))
{
case Sensor.TYPE_ACCELEROMETER:
Gravity = event.values.clone( );
break;
case Sensor.TYPE_MAGNETIC_FIELD:
MagneticField = event.values.clone( );
break;
default:
return;
}

if(Gravity != null && MagneticField != null)


{
UpdateDirection( );
}
}

private void UpdateDirection( )


{
float[ ] temp = new float[9];
float[ ] R = new float[9];

//Load rotation matrix into R


SensorManager.getRotationMatrix(temp, null, Gravity, MagneticField);

//Remap to camera's point-of-view


SensorManager.remapCoordinateSystem(temp, SensorManager.AXIS_X, SensorManager.AXIS_Z, R);

//Return the orientation values


SensorManager.getOrientation(R, Values);

//Convert to degrees
for (int i=0; i < Values.length; i++)
{
Double degrees = (Values[i] * 180) / Math.PI;
Values[i] = degrees.floatValue( );
}

74
//Display the compass direction
LblDirection.setText( "Direction: " + GetDirectionFromDegrees(Values[0]) );

//Display the raw values


LblValues.setText(String.format("Azimuth: %1$1.2f, Pitch: %2$1.2f, Roll: %3$1.2f",Values[0],
Values[1], Values[2]));
}

private String GetDirectionFromDegrees(float degrees)


{
if(degrees >= -22.5 && degrees < 22.5) { return "North"; }
if(degrees >= 22.5 && degrees < 67.5) { return "North East"; }
if(degrees >= 67.5 && degrees < 112.5) { return "East"; }
if(degrees >= 112.5 && degrees < 157.5) { return "South East"; }
if(degrees >= 157.5 && degrees < -157.5) { return "South"; }
if(degrees >= -157.5 && degrees < -112.5) { return "South West"; }
if(degrees >= -112.5 && degrees < -67.5) { return "West"; }
if(degrees >= -67.5 && degrees < -22.5) { return "North West"; }

return null;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

Laboratory Hands-On Activity No.9 Fullscreen Output


75
LABORATORY HANDS-ON ACTIVITY NO.10
BAROMETER
//Application Name: Barometer
//Project Name: Barometer
//Package Name: com.BSU.Barometer
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

Under res>layout folder, edit main.xml and enter its contents.


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:background="#0000ff"
android:orientation="vertical"
tools:context=".Main" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BAROMETER"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#0000ff"
android:background="#00ffff"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblAirPressure"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:gravity="left"
android:visibility="visible"/>

</LinearLayout>

values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
</style>
</resources>

76
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.Barometer"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<uses-feature
android:required="true"
android:name="android.hardware.sensor.pressure"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.Barometer.Main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Main.java
package com.BSU.Barometer;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.TextView;

public class Main extends Activity implements SensorEventListener


{
//Data Members
private TextView LblAirPressure;
private SensorManager SensorMngr;
private Sensor Barometer;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

77
LblAirPressure = (TextView) findViewById(R.id.LblAirPressure);

SensorMngr = (SensorManager) this.getSystemService(SENSOR_SERVICE);

//Barometer
if (SensorMngr.getSensorList(Sensor.TYPE_PRESSURE).size( ) == 0)
{
LblAirPressure.setText("No barometer sensor is installed!");
}
else
{
Barometer = SensorMngr.getSensorList(Sensor.TYPE_PRESSURE).get(0);

if (!SensorMngr.registerListener(this, Barometer, SensorManager.SENSOR_DELAY_GAME))


{
LblAirPressure.setText("Couldn't register sensor listener");
}
}

//Hide the Soft Keyboard


getWindow( ).setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

@Override
protected void onResume( )
{
super.onResume( );

SensorMngr.registerListener(this, Barometer, SensorManager.SENSOR_DELAY_NORMAL);


}

@Override
protected void onPause( )
{
super.onPause( );

SensorMngr.unregisterListener(this, Barometer);
}

@Override
public void onSensorChanged(SensorEvent event)
{
float MillibarsOfPressure = event.values[0];

LblAirPressure.setText(String.format("Ambient Air Pressure: %.2f mbars",


MillibarsOfPressure));
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
78
return true;
}
}

Laboratory Hands-On Activity No.10 Fullscreen Output

LABORATORY HANDS-ON ACTIVITY NO.11


THERMOMETER
//Application Name: Thermometer
//Project Name: Thermometer
//Package Name: com.BSU.Thermometer
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

Under res>layout folder, edit main.xml and enter its contents.


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:background="#0000ff"
android:orientation="vertical"
tools:context=".Main" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="THERMOMETER"
android:textSize="16sp"
79
android:textStyle="bold"
android:textColor="#0000ff"
android:background="#00ffff"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblTemperature"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:gravity="left"
android:visibility="visible"/>

</LinearLayout>

values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
</style>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.Thermometer"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<uses-feature
android:required="true"
android:name="android.hardware.sensor.temperature"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.Thermometer.Main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

80
Main.java
package com.BSU.Thermometer;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.TextView;

public class Main extends Activity implements SensorEventListener


{
//Data Members
private TextView LblTemperature;
private SensorManager SensorMngr;
private Sensor Thermometer;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

LblTemperature = (TextView) findViewById(R.id.LblTemperature);

SensorMngr = (SensorManager) this.getSystemService(SENSOR_SERVICE);

//Barometer
if (SensorMngr.getSensorList(Sensor.TYPE_AMBIENT_TEMPERATURE).size( ) == 0)
{
LblTemperature.setText("No thermometer sensor is installed!");
}
else
{
Thermometer = SensorMngr.getSensorList(Sensor.TYPE_AMBIENT_TEMPERATURE).get(0);

if (!SensorMngr.registerListener(this, Thermometer, SensorManager.SENSOR_DELAY_GAME))


{
LblTemperature.setText("Couldn't register sensor listener");
}
}

//Hide the Soft Keyboard


getWindow( ).setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

@Override
protected void onResume( )
{
super.onResume( );

SensorMngr.registerListener(this, Thermometer, SensorManager.SENSOR_DELAY_NORMAL);


}

81
@Override
protected void onPause( )
{
super.onPause( );

SensorMngr.unregisterListener(this, Thermometer);
}

@Override
public void onSensorChanged(SensorEvent event)
{
float Temperature = event.values[0];

LblTemperature.setText(String.format("Ambient Temperature: %.2f °C", Temperature));


}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

Laboratory Hands-On Activity No.11 Fullscreen Output

82
LABORATORY HANDS-ON ACTIVITY NO.12
LIGHT SENSOR
//Application Name: Light
//Project Name: Light
//Package Name: com.BSU.Light
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

Under res>layout folder, edit main.xml and enter its contents.


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:background="#0000ff"
android:orientation="vertical"
tools:context=".Main" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="LIGHT SENSOR"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#0000ff"
android:background="#00ffff"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblIlluminance"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:gravity="left"
android:visibility="visible"/>

</LinearLayout>

values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
</style>
</resources>

83
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.Light"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<uses-feature
android:required="true"
android:name="android.hardware.sensor.light"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.Light.Main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Main.java
package com.BSU.Light;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.TextView;

public class Main extends Activity implements SensorEventListener


{
//Data Members
private TextView LblIlluminance;
private SensorManager SensorMngr;
private Sensor Light;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

84
LblIlluminance = (TextView) findViewById(R.id.LblIlluminance);

SensorMngr = (SensorManager) this.getSystemService(SENSOR_SERVICE);

//Light
if (SensorMngr.getSensorList(Sensor.TYPE_LIGHT).size( ) == 0)
{
LblIlluminance.setText("No light sensor is installed!");
}
else
{
Light = SensorMngr.getSensorList(Sensor.TYPE_LIGHT).get(0);

if (!SensorMngr.registerListener(this, Light, SensorManager.SENSOR_DELAY_GAME))


{
LblIlluminance.setText("Couldn't register sensor listener");
}
}

//Hide the Soft Keyboard


getWindow( ).setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

@Override
protected void onResume( )
{
super.onResume( );

SensorMngr.registerListener(this, Light, SensorManager.SENSOR_DELAY_NORMAL);


}

@Override
protected void onPause( )
{
super.onPause( );

SensorMngr.unregisterListener(this, Light);
}

@Override
public void onSensorChanged(SensorEvent event)
{
float Illuminance = event.values[0];

LblIlluminance.setText(String.format("Light: %.2f lx", Illuminance));


}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
85
}
}

Laboratory Hands-On Activity No.12 Fullscreen Output

LABORATORY HANDS-ON ACTIVITY NO.13


PROXIMITY SENSOR
//Application Name: Proximity
//Project Name: Proximity
//Package Name: com.BSU.Proximity
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

Under res>layout folder, edit main.xml and enter its contents.


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:background="#0000ff"
android:orientation="vertical"
tools:context=".Main" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Proximity Sensor"
android:textSize="16sp"
android:textStyle="bold"
86
android:textColor="#0000ff"
android:background="#00ffff"
android:gravity="center"
android:visibility="visible"/>

<TextView
android:id="@+id/LblValues"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:gravity="left"
android:visibility="visible"/>

<TextView
android:id="@+id/LblMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:gravity="left"
android:visibility="visible"/>

</LinearLayout>

values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
</style>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.Proximity"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<uses-feature
android:required="true"
android:name="android.hardware.sensor.proximity"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.Proximity.Main"
android:label="@string/app_name"
87
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Main.java
package com.BSU.Proximity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class Main extends Activity implements SensorEventListener


{
//Data Members
private SensorManager SensorMngr;
private Sensor Proximity;

private TextView LblMessage;


private TextView LblValues;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

LblValues = (TextView) findViewById(R.id.LblValues);


LblMessage = (TextView) findViewById(R.id.LblMessage);

SensorMngr = (SensorManager) this.getSystemService(SENSOR_SERVICE);

if (SensorMngr.getSensorList(Sensor.TYPE_PROXIMITY).size( ) == 0)
{
LblMessage.setText("No proximity sensor is installed!");
}
else
{
Proximity = SensorMngr.getSensorList(Sensor.TYPE_PROXIMITY).get(0);

if (!SensorMngr.registerListener(this, Proximity, SensorManager.SENSOR_DELAY_GAME))


{
LblMessage.setText("Couldn't register sensor listener");
}
}
}

88
@Override
protected void onResume( )
{
// Register a listener for the sensor.
super.onResume( );
SensorMngr.registerListener(this, Proximity, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause( )
{
// Be sure to unregister the sensor when the activity pauses.
super.onPause( );
SensorMngr.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event)
{
float Distance = event.values[0];
float Max = Proximity.getMaximumRange( );

LblValues.setText(String.format("Distance: %1$.2f cm. Max distance: %2$.2f cm.", Distance,


Max));

if(Distance > Max)


{
LblMessage.setText("The Object is Far to your device.");
}
else
{
LblMessage.setText("The Object is Near to your device.");
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

89
Laboratory Hands-On Activity No.13 Fullscreen Output

LABORATORY HANDS-ON ACTIVITY NO.14


GPS
//Application Name: GPS
//Project Name: GPS
//Package Name: com.BSU.GPS
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

Under res>layout folder, edit main.xml and enter its contents.


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:background="#0000ff"
android:orientation="vertical"
tools:context=".Main" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="GPS Location"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#0000ff"
android:background="#00ffff"
android:gravity="center"
90
android:visibility="visible"/>
<TextView
android:id="@+id/LblLatitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude: 0.0000000000"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:gravity="left"
android:visibility="visible"/>

<TextView
android:id="@+id/LblLongitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude: 0.0000000000"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:background="#0000ff"
android:gravity="left"
android:visibility="visible"/>

</LinearLayout>

values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
</style>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.GPS"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.GPS.Main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


91
</intent-filter>
</activity>
</application>

</manifest>
Main.java
package com.BSU.GPS;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.TextView;

public class Main extends Activity implements LocationListener


{
//Data Members
private TextView LblLatitude;
private TextView LblLongitude;
private LocationManager LocationMngr;
private Criteria Crit;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

LblLatitude = (TextView) findViewById(R.id.LblLatitude);


LblLongitude = (TextView) findViewById(R.id.LblLongitude);

LocationMngr = (LocationManager)getSystemService(LOCATION_SERVICE);
Crit = new Criteria( );
Crit.setAccuracy(Criteria.ACCURACY_FINE);

//Hide the Soft Keyboard


getWindow( ).setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

@Override
public void onResume( )
{
super.onResume( );

try
{
LocationMngr.requestLocationUpdates(0L, 0.0f, Crit, this, null);
}
catch(Exception ex)
{

}
}

92
@Override
public void onPause( )
{
super.onPause( );

try
{
LocationMngr.removeUpdates(this);
}
catch(Exception ex)
{

}
}

@Override
public void onLocationChanged(Location location)
{
LblLatitude.setText(String.format("Latitude: %.10f", location.getLatitude( )));
LblLongitude.setText(String.format("Longitude: %.10f", location.getLongitude( )));
}

@Override
public void onProviderDisabled(String provider)
{

@Override
public void onProviderEnabled(String provider)
{

@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}

93
Laboratory Hands-On Activity No.14 Fullscreen Output

LABORATORY HANDS-ON ACTIVITY NO.15


GRAPHICS
//Application Name: Graphics
//Project Name: Graphics
//Package Name: com.BSU.Graphics
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
</style>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.Graphics"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
94
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BSU.Graphics.Main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Add Java filename: GraphicsView.java and enter the contents.


GraphicsView.java
package com.BSU.Graphics;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GraphicsView extends SurfaceView implements Runnable


{
//Data Members
private static final int TICKS_PER_SECOND = 30;
private static final int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
private static final int MAX_FRAMESKIP = 5;

private Thread RenderThread = null;


private SurfaceHolder Holder;
private volatile boolean Running = false;
private int Width, Height;
private Paint paint;
private RectF Oval;

//Constructor
public GraphicsView(Context context)
{
super(context);

Holder = getHolder( );
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Oval = new RectF( );
}

public void resume( )


{
Running = true;
RenderThread = new Thread(this);
RenderThread.start( );
}
95
public void pause( )
{
Running = false;

while(true)
{
try
{
RenderThread.join( );
return;
}
catch (InterruptedException e)
{
// retry
}
}
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
Width = w;
Height = h;
}

private void Draw(Canvas canvas)


{
float densityMultiplier = getContext( ).getResources( ).getDisplayMetrics( ).density;
int WidthBox = 50;
int HeightBox = 50;

canvas.drawRGB(0, 0, 255);

paint.setColor(Color.GREEN);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setTextSize(20.0f * densityMultiplier);

int TextHeight = Math.abs(paint.getFontMetricsInt( ).ascent) +


Math.abs(paint.getFontMetricsInt( ).descent);
canvas.drawText("Width: " + Width + " x Height: " + Height, 0, TextHeight, paint);
canvas.drawText("Frames per second: " + Integer.toString(TICKS_PER_SECOND), 0,
TextHeight * 2, paint);

//Graphics Primitive Drawing Commands


paint.setTypeface(Typeface.SERIF);
paint.setTextSize(12.0f * densityMultiplier);

//Draw Unfilled Circle


paint.setColor(Color.YELLOW);
paint.setStyle(android.graphics.Paint.Style.STROKE);
canvas.drawCircle(30, 100, 30, paint);

//Draw Filled Circle


paint.setColor(Color.YELLOW);
paint.setStyle(android.graphics.Paint.Style.FILL);
canvas.drawCircle(100, 100, 30, paint);

paint.setColor(Color.CYAN);
canvas.drawText("Circle", 15, 145, paint);

96
//Draw Unfilled Rectangle
paint.setColor(Color.YELLOW);
paint.setStyle(android.graphics.Paint.Style.STROKE);
canvas.drawRect(10, 160, 10+WidthBox, 160+HeightBox, paint);

//Draw Filled Rectangle


paint.setColor(Color.YELLOW);
paint.setStyle(android.graphics.Paint.Style.FILL);
canvas.drawRect(70, 160, 70+WidthBox, 160+HeightBox, paint);

paint.setColor(Color.CYAN);
canvas.drawText("Rectangle", 15, 225, paint);

//Draw Line
paint.setColor(Color.YELLOW);
canvas.drawLine(15, 230, 70, 255, paint);

paint.setColor(Color.CYAN);
canvas.drawText("Line", 15, 265, paint);

//Draw Unfilled Arc


paint.setColor(Color.YELLOW);
paint.setStyle(android.graphics.Paint.Style.STROKE);
Oval.left = 30;
Oval.top = 280;
Oval.right = 60;
Oval.bottom = 310;
canvas.drawArc(Oval, 0, -90, true, paint);

//Draw Filled Arc


paint.setColor(Color.YELLOW);
paint.setStyle(android.graphics.Paint.Style.FILL);
Oval.left = 70;
Oval.top = 280;
Oval.right = 100;
Oval.bottom = 310;
canvas.drawArc(Oval, 0, -90, true, paint);

paint.setColor(Color.CYAN);
canvas.drawText("Arc", 15, 325, paint);
}

@Override
public void run( )
{
long next_game_tick = System.currentTimeMillis( );
int loops;

while(Running)
{
if(!Holder.getSurface( ).isValid( ))
{
continue;
}

Canvas canvas = Holder.lockCanvas( );

97
/************************ Delay to maintain 30 FPS at any hardware ************/
loops = 0;
while( System.currentTimeMillis( ) > next_game_tick && loops < MAX_FRAMESKIP)
{
next_game_tick += SKIP_TICKS;
loops++;
}
/*****************************************************************************/

Draw(canvas);

Holder.unlockCanvasAndPost(canvas);
}
}
}
Main.java
package com.BSU.Graphics;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.WindowManager;

public class Main extends Activity


{
//Data Members
private GraphicsView Graphics;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Graphics = new GraphicsView(this);
setContentView(Graphics);

//Hide the Soft Keyboard


getWindow( ).setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

@Override
public void onResume( )
{
super.onResume( );
Graphics.resume( );
}

@Override
public void onPause( )
{
super.onPause( );
Graphics.pause( );
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}
}
98
Laboratory Hands-On Activity No.15 Fullscreen Output

LABORATORY HANDS-ON ACTIVITY NO.16


2D SPRITE ANIMATION
//Application Name: 2D Sprite Animation
//Project Name: 2D Sprite Animation
//Package Name: com.BSU.2DSpriteAnimation
//Minimum Required SDK: API 14: Android 4.0 (IceCreamSandwich)
//Target SDK: API 16: Android 4.1 (Jelly Bean)
//Compile With: API 17: Android 4.2 (Jelly Bean)
//Theme: Holo Light
//Activity Name: Main
//Layout Name: main
//Navigation Type: None

Copy the images & sounds and paste inside the folder assets. The filenames must be written in
small case. Examples: filipinoship.png, music.mid, missile.png, shot.ogg etc.

99
values-v14
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen">
</style>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BSU.2DSpriteAnimation"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=" com.BSU.2DSpriteAnimation.Main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Add Java filename: GraphicsView.java and enter the contents.


GraphicsView.java
package com.BSU.spriteanimation;

import java.io.InputStream;

100
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GraphicsView extends SurfaceView implements Runnable


{
//Data Members
private static final int TICKS_PER_SECOND = 30;
private static final int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
private static final int MAX_FRAMESKIP = 5;
private Context context;
private static final int SPEED = 2;
private Thread RenderThread = null;
private SurfaceHolder Holder;
private volatile boolean Running = false;
private int Width, Height;
private Paint paint;

private Bitmap FilipinoShip, Missile1, Missile2;


private boolean IsTouch;
private float Touchx, Touchy;
private float FilipinoShipX, FilipinoShipY;
private float Missile1X, Missile1Y, Missile2X, Missile2Y;
private boolean IsFire;

public Sound Sound;


public Sound Music;

//Constructor
public GraphicsView(Context context)
{
super(context);

this.context = context;
Holder = getHolder( );
paint = new Paint(Paint.ANTI_ALIAS_FLAG);

IsTouch = false;
IsFire = false;
Touchx = Touchy = 0f;
FilipinoShipX = 100f;
FilipinoShipY = 250f;
Missile1X = 0f;
Missile1Y = 0f;
Missile2X = 0f;
Missile2Y = 0f;

try
{
AssetManager assetManager = context.getAssets( );

//Load Bitmap
101
InputStream inputStream = assetManager.open("filipinoship.png");
FilipinoShip = BitmapFactory.decodeStream(inputStream);
inputStream.close( );

//Load Bitmap
inputStream = assetManager.open("missile.png");
Missile1 = BitmapFactory.decodeStream(inputStream);
inputStream.close( );

//Load Bitmap
inputStream = assetManager.open("missile.png");
Missile2 = BitmapFactory.decodeStream(inputStream);
inputStream.close( );
}
catch (Exception e)
{
}
}
public void resume( )
{
try
{
//Load Music Background
Music = new Sound(context,"music.mid");

//Load Sound Effects


Sound = new Sound(context,"shot.ogg");

//Play Background Music forever


Music.setLooping(true);
Music.play( );
}
catch (Exception e)
{
}

Running = true;
RenderThread = new Thread(this);
RenderThread.start( );
}

public void pause( )


{
Running = false;

while(true)
{
try
{
RenderThread.join( );
return;
}
catch (InterruptedException e)
{
// retry
}
}
}

@Override
102
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
Width = w;
Height = h;
}

private void Draw(Canvas canvas)


{
float densityMultiplier = getContext( ).getResources( ).getDisplayMetrics( ).density;
canvas.drawRGB(0, 0, 255);

paint.setColor(Color.GREEN);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setTextSize(20.0f * densityMultiplier);

int TextHeight = Math.abs(paint.getFontMetricsInt( ).ascent) +


Math.abs(paint.getFontMetricsInt( ).descent);
canvas.drawText("Width: " + Width + " x Height: " + Height, 0, TextHeight, paint);
canvas.drawText("Frames per second: " + Integer.toString(TICKS_PER_SECOND), 0,
TextHeight * 2, paint);
//Draw the Bitmap
canvas.drawBitmap(FilipinoShip, FilipinoShipX, FilipinoShipY, null);

if(IsFire == true)
{
canvas.drawBitmap(Missile1, Missile1X, Missile1Y, null);
canvas.drawBitmap(Missile2, Missile2X, Missile2Y, null);

//Move up the two (2) missiles


Missile1Y –= 5;
Missile2Y –= 5;

if((Missile1Y + 128) < 0 && (Missile2Y + 128) < 0)


{
IsFire = false;
}
}

//Draw Filled Circle


paint.setColor(Color.YELLOW);
paint.setStyle(android.graphics.Paint.Style.FILL);
canvas.drawCircle(50, Height-60, 30, paint);

paint.setColor(Color.BLUE);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setTextSize(16.0f * densityMultiplier);
canvas.drawText("Fire", 37, Height-60 + 5, paint);
}

@Override
public void run( )
{
long next_game_tick = System.currentTimeMillis( );
int loops;

while(Running)
{
if(!Holder.getSurface( ).isValid( ))
{
continue;
103
}

Canvas canvas = Holder.lockCanvas( );

/************************ Delay to maintain 30 FPS at any hardware ************/


loops = 0;
while( System.currentTimeMillis( ) > next_game_tick && loops < MAX_FRAMESKIP)
{
next_game_tick += SKIP_TICKS;
loops++;
}
/*****************************************************************************/

Draw(canvas);

Holder.unlockCanvasAndPost(canvas);
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction( );

if (action == MotionEvent.ACTION_MOVE)
{
//Get the Touch coordinates
float x = event.getX( );
float y = event.getY( );

if((x > Touchx) && (Touchx != x) )


{
// Move Right
Touchx = x;

FilipinoShipX += SPEED;
}
else if((x < Touchx) && (Touchx != x) )
{
// Move Left
Touchx = x;

FilipinoShipX –= SPEED;
}

if((y > Touchy) && (Touchy != y) )


{
//Move Down
Touchy = y;

FilipinoShipY += SPEED;
}
else if((y < Touchy) && (Touchy != y) )
{
// Move Up
Touchy = y;

FilipinoShipY –= SPEED;
}
}
104
else if (action == MotionEvent.ACTION_DOWN)
{
IsTouch = true;
}
else if (action == MotionEvent.ACTION_UP)
{
//Single Touch or Tap
if(IsTouch == true)
{
IsTouch = false;

//Get the Touch or Tap coordinates


float x = event.getX( );
float y = event.getY( );

//Touch or tap the Fire Circle


if(PtInCircle(x, y, 50, Height-60, 30) == true)
{
if(IsFire == false)
{
IsFire = true;

//Attach the 1st Missile


Missile1X = FilipinoShipX – 4;
Missile1Y = FilipinoShipY + 40;

//Attach the 2nd Missile


Missile2X = FilipinoShipX + 94;
Missile2Y = FilipinoShipY + 40;

//Play Sound Effect


Sound.play( );
}
}
}
}

return true;
}

private boolean PtInCircle(float x, float y, float cx, float cy, float radius)
{
double distance = Math.sqrt((double) (cx – x) * (cx – x) + (cy – y) * (cy – y));

if(distance <= radius)


{
return true;
}
else
{
return false;
}
}
}

Add Java filename: Sound.java and enter the contents.


Sound.java
package com.BSU.spriteanimation;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
105
import android.content.res.AssetManager;
import android.media.MediaPlayer;

public class Sound implements MediaPlayer.OnCompletionListener


{
//Data Members
public MediaPlayer MediaPlayer;
private boolean isPrepared = false;

//Constructor
public Sound(Context context, String Filename)
{
MediaPlayer = new MediaPlayer( );

try
{
AssetManager assetManager = context.getAssets( );
AssetFileDescriptor descriptor = assetManager.openFd(Filename);
MediaPlayer.setDataSource(descriptor.getFileDescriptor( ), descriptor.getStartOffset( ),
descriptor.getLength( ));
MediaPlayer.setOnCompletionListener(this);

MediaPlayer.prepare( );
}
catch (Exception e)
{
MediaPlayer = null;
}
}
public void dispose( )
{
if (MediaPlayer.isPlaying( ))
{
MediaPlayer.stop( );
}

MediaPlayer.release( );
}

public boolean isPlaying( )


{
return isPrepared;
}

public boolean isStopped( )


{
return !isPrepared;
}

public void pause( )


{
if (MediaPlayer.isPlaying( ))
{
MediaPlayer.pause( );
}
}

public void play( )


{
106
if (isPrepared == true)
{
return;
}

try
{
synchronized (this)
{
MediaPlayer.start( );
isPrepared = true;
}
}
catch (Exception e)
{

}
}
public void setLooping(boolean isLooping)
{
MediaPlayer.setLooping(isLooping);
}

public void setVolume(float volume)


{
MediaPlayer.setVolume(volume, volume);
}

public void stop( )


{
MediaPlayer.stop( );

synchronized (this)
{
isPrepared = false;
}
}

public void onCompletion(MediaPlayer player)


{
synchronized (this)
{
isPrepared = false;
}
}
}

Main.java
package com.BSU.spriteanimation;

import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.WindowManager;

public class Main extends Activity


{
//Data Members
private GraphicsView Graphics;
107
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setVolumeControlStream(AudioManager.STREAM_MUSIC);

Graphics = new GraphicsView(this);


setContentView(Graphics);

//Hide the Soft Keyboard


getWindow( ).setSoftInputMode(WindowManager.LayoutParams.
SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

@Override
public void onResume( )
{
super.onResume( );

Graphics.resume( );
}

@Override
public void onPause( )
{
super.onPause( );

//Sound
if (Graphics.Sound.MediaPlayer != null)
{
Graphics.Sound.MediaPlayer.pause( );

if (isFinishing( ))
{
Graphics.Sound.MediaPlayer.stop( );
Graphics.Sound.MediaPlayer.release( );
}
}

//Music Background
if (Graphics.Music.MediaPlayer != null)
{
Graphics.Music.MediaPlayer.pause( );

if (isFinishing( ))
{
Graphics.Music.MediaPlayer.stop( );
Graphics.Music.MediaPlayer.release( );
}
}

Graphics.pause( );
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
108
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater( ).inflate(R.menu.main, menu);
return true;
}

Laboratory Hands-On Activity No.16 Fullscreen Output

109
HOW TO ADD ICON
1. Create 4 similar icons but different sizes. Important name them in all small letters and do not
use space character. The filenames must be similar names.

lacea.png lacea.png lacea.png lacea.png


72 x 72 36 x 36 48 x 48 96 x 96

2. Browse the location of the icon with a size 72 x 72 and right-click then copy.

3. In the Java IntelliJ IDE, right-click the drawable-hdpi folder then paste.

Browse the icon file, copy and paste in the drawable folder of Java IntelliJ IDE.

Folder: drawable-hdpi = lacea.png 72 x 72


Folder: drawable-ldpi = lacea.png 36 x 36
Folder: drawable-mdpi = lacea.png 48 x 48
Folder: drawable-xhdpi = lacea.png 96 x 96

HOW TO SOLVE DEBUG CERTIFICATE EXPIRED PROBLEM in Using Eclipse ADT IDE?

Note: Debug certificate last for 365 days.

1. Delete the filename: debug.keystore


Sample Location: C:\Users\JOHNBLACEA\.android

2. Click the Project Menu> Clean.

110
3. Select projects selected below and check the project to be clean then click OK button.

111

You might also like