IT WM 131.1 Mobile Application Development 1 Laboratory
IT WM 131.1 Mobile Application Development 1 Laboratory
LABORATORY MANUAL
1st Semester
Prepared by
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.
2
7. Click Next button.
3
9. Click Next button.
4
HOW TO COMPILE AND RUN THE ANDROID PROJECT
1. On the Package Explorer (located at the left), right-click the Project Name..
3. Please wait more or less 5 minutes to run the Android OS SmartPhone Emulator.
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.
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;
//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);
LblStatus.setText(Buffer.toString( ));
}
@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
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;
//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);
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);
}
}
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;
}
@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;
}
}
1. Browse the folder where you compiled the Time Deposit android project. Browse the sub-folder
bin.
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.
𝒔=𝒓𝜽
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.
𝒓𝟐 𝜽
𝑨𝒓𝒆𝒂 =
𝟐
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;
//Default Constructor
public Main( )
{
ChkMessage = new StringBuffer[4];
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ChkMessage[0].delete(0,ChkMessage[0].length( ));
ChkMessage[0].append("Selected Location: Restaurant\n");
LblStatusCheckBox.setText(ChkMessage[0].toString( ));
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");
}
}
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;
}
}
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.
30
3. Click Finish button.
<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" />
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.
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;
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)
{
return view;
}
@Override
public void onStart( )
{
super.onStart( );
BtnClose.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
view.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
}
});
BtnCompute.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
try
{
DecimalFormat Number = new DecimalFormat("#,##0.00");
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;
//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( );
BtnClose.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
view.setVisibility(View.GONE);
38
lv.setVisibility(View.VISIBLE);
}
});
BtnCompute.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
try
{
DecimalFormat Number = new DecimalFormat("#,##0.00");
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;
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);
getListView( ).setTextFilterEnabled(true);
switch(position)
{
case 0: //Addition
getListView( ).setVisibility(View.GONE);
//add a fragment
fragmentTransaction.replace(R.id.Panel, Form1Fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit( );
Toaster.show( );
break;
case 1: //Subtraction
getListView( ).setVisibility(View.GONE);
//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;
}
}
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
𝒙𝟐 𝒙𝟒 𝒙𝟔 (−𝒙)𝟐𝒏
𝒄𝒐𝒔 𝒙 = 𝟏 − + − +. . .
𝟐! 𝟒! 𝟔! 𝟐𝒏!
𝒙 𝒙𝟑 𝒙𝟓 𝒙𝟕 (−𝒙)(𝟐𝒏−𝟏)
𝒔𝒊𝒏 𝒙 = − + − + . . .
𝟎! 𝟑! 𝟓! 𝟕! (𝟐𝒏 − 𝟏)!
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" >
<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>
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;
@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);
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;
}
}
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
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<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" />
</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;
@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);
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);
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( ));
if(timePicker.is24HourView( ) == false)
{
String AmPm = null;
int Hour = 0;
timePicker.setIs24HourView(true);
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);
LblTimePicker.setText(f.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;
}
}
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
<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>
<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>
<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>
<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" />
</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>
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
57
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState)
{
return view;
}
@Override
public void onStart( )
{
super.onStart( );
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;
@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.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;
@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.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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar = getActionBar( );
ActionBar.setDisplayHomeAsUpEnabled(true);
}
}
};
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( );
//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( );
//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( );
//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;
}
}
62
LABORATORY HANDS-ON EXERCISE NO.5 – SET A
1. Linear Function:
𝒚𝟐 − 𝒚𝟏
𝑺𝒍𝒐𝒑𝒆 𝒎 =
𝒙𝟐 − 𝒙𝟏
Parallel and Perpendicular Lines
𝑻𝒘𝒐 (𝟐) 𝑳𝒊𝒏𝒆𝒔 𝒂𝒓𝒆 𝒑𝒂𝒓𝒂𝒍𝒍𝒆𝒍 𝒊𝒇 𝒂𝒏𝒅 𝒐𝒏𝒍𝒚 𝒊𝒇 𝒎𝟏 = 𝒎𝟐
𝑻𝒘𝒐 (𝟐) 𝑳𝒊𝒏𝒆𝒔 𝒂𝒓𝒆 𝒑𝒆𝒓𝒑𝒆𝒏𝒅𝒊𝒄𝒖𝒍𝒂𝒓 𝒊𝒇 𝒂𝒏𝒅 𝒐𝒏𝒍𝒚 𝒊𝒇 𝒎𝟏 𝒎𝟐 = −𝟏
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.
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
<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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (SensorMngr.getSensorList(Sensor.TYPE_ACCELEROMETER).size( ) == 0)
{
LblMessage.setText("No accelerometer sensor is installed!");
}
else
{
Accelerometer = SensorMngr.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
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.
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);
69
if(LinearAcceleration[1] > 0f)
{
LblAccelerationUP.setText("Moves UP.");
}
else if(LinearAcceleration[1] < 0f)
{
LblAccelerationDOWN.setText("Moves DOWN.");
}
@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;
}
}
<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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//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);
73
@Override
protected void onResume( )
{
super.onResume( );
@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;
}
//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]) );
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;
}
}
<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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
77
LblAirPressure = (TextView) findViewById(R.id.LblAirPressure);
//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);
@Override
protected void onResume( )
{
super.onResume( );
@Override
protected void onPause( )
{
super.onPause( );
SensorMngr.unregisterListener(this, Barometer);
}
@Override
public void onSensorChanged(SensorEvent event)
{
float MillibarsOfPressure = event.values[0];
@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;
}
}
<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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//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);
@Override
protected void onResume( )
{
super.onResume( );
81
@Override
protected void onPause( )
{
super.onPause( );
SensorMngr.unregisterListener(this, Thermometer);
}
@Override
public void onSensorChanged(SensorEvent event)
{
float Temperature = event.values[0];
@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;
}
}
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
<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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
84
LblIlluminance = (TextView) findViewById(R.id.LblIlluminance);
//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);
@Override
protected void onResume( )
{
super.onResume( );
@Override
protected void onPause( )
{
super.onPause( );
SensorMngr.unregisterListener(this, Light);
}
@Override
public void onSensorChanged(SensorEvent event)
{
float Illuminance = event.values[0];
@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
}
}
<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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (SensorMngr.getSensorList(Sensor.TYPE_PROXIMITY).size( ) == 0)
{
LblMessage.setText("No proximity sensor is installed!");
}
else
{
Proximity = SensorMngr.getSensorList(Sensor.TYPE_PROXIMITY).get(0);
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( );
@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
<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" />
<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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationMngr = (LocationManager)getSystemService(LOCATION_SERVICE);
Crit = new Criteria( );
Crit.setAccuracy(Criteria.ACCURACY_FINE);
@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
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" />
</manifest>
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;
//Constructor
public GraphicsView(Context context)
{
super(context);
Holder = getHolder( );
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Oval = new RectF( );
}
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;
}
canvas.drawRGB(0, 0, 255);
paint.setColor(Color.GREEN);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setTextSize(20.0f * densityMultiplier);
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);
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);
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;
}
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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Graphics = new GraphicsView(this);
setContentView(Graphics);
@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
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" />
</manifest>
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;
//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");
Running = true;
RenderThread = new Thread(this);
RenderThread.start( );
}
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;
}
paint.setColor(Color.GREEN);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setTextSize(20.0f * densityMultiplier);
if(IsFire == true)
{
canvas.drawBitmap(Missile1, Missile1X, Missile1Y, null);
canvas.drawBitmap(Missile2, Missile2X, Missile2Y, null);
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
}
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( );
FilipinoShipX += SPEED;
}
else if((x < Touchx) && (Touchx != x) )
{
// Move Left
Touchx = x;
FilipinoShipX –= SPEED;
}
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;
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));
//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( );
}
try
{
synchronized (this)
{
MediaPlayer.start( );
isPrepared = true;
}
}
catch (Exception e)
{
}
}
public void setLooping(boolean isLooping)
{
MediaPlayer.setLooping(isLooping);
}
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;
setVolumeControlStream(AudioManager.STREAM_MUSIC);
@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;
}
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.
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.
HOW TO SOLVE DEBUG CERTIFICATE EXPIRED PROBLEM in Using Eclipse ADT IDE?
110
3. Select projects selected below and check the project to be clean then click OK button.
111