Sie sind auf Seite 1von 13

CSNB544 Lab 7 File

(2016)
Course Outcome
CO1:
Explain the development techniques, constraints and
challenges in developing a mobile application.
CO2: Design an application suitable to be used in a mobile device.
CO3: Develop a useable mobile application.
CO4: Work effectively in a development team.
CO5:
Communicate ideas and design decisions among team
members as well as to other interested parties.

Coverage

X
X
X

Our goal is to develop a BMI Calculator.


Pre-requisite: Lab 6
We want to make our calculator more interesting. Our goal this week is to record the BMI status
into a file.
1. Add Save button
Append the following highlighted codes into activity_calculator.xml
activity_calculator.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/result_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/na"
android:textColor="#00FFFFFF"
android:textSize="15pt"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/savebutton" />
</LinearLayout>

CSNB544 Lab 7 File


(2016)
2. Add Read button
Append the following codes within the same <LinearLayout> of the previous code.
activity_calculator.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record"
android:id="@+id/readbutton"/>

Both buttons should located next to each other.


3. Add a temporary TextView
This TextView is used to check the data is successfully stored or not. Append the following
<LinearLayout> after the previous <LinearLayout>
activity_calculator.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:padding="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read"
android:id="@+id/displaysaveditem"
android:textSize="10dp" />
</LinearLayout>

4. Declare a new variable to handle the buttons and TextView in Java


In CalculatorActivity.java, we have to acknowledge those 2 buttons and a TextView created in
the XML file. Add the following code after the following line
public class CalculatorActivity extends AppCompatActivity {

CalculatorActivity.java
Button bSave,bRead;
TextView tvStored;

Within
protected void onCreate(Bundle savedInstanceState) {

Paste the following highlighted codes


CalculatorActivity.java
tvResultStatus = (TextView) findViewById(R.id.result_status);
bSave =(Button)findViewById(R.id.savebutton);
bRead =(Button)findViewById(R.id.readbutton);
tvStored=(TextView)findViewById(R.id.displaysaveditem);

CSNB544 Lab 7 File


(2016)

5. Declare a new variable to handle File.


Right after the previous line, create a new variable of String with name data and a private String
file with name mydata. The code can be written as follows:
CalculatorActivity.java
String data;
private String file = "mydata";

6. Write data into File.


Place the following code within the following function
protected void onCreate(Bundle savedInstanceState) {

CalculatorActivity.java
bSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data=tvDisplayResult.getText().toString() + "\n";
try {
FileOutputStream fOut = openFileOutput(file, Context.MODE_APPEND);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

7. Read data from File.


Place the following code after the previous code.
CalculatorActivity.java
//Read
bRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
FileInputStream fin = openFileInput(file);
int c;
String temp="";
while( (c = fin.read()) != -1){

CSNB544 Lab 7 File


(2016)
temp = temp + Character.toString((char)c);
}
tvStored.setText(temp);
Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
}
catch(Exception e){
}
}
});

8. Deployment
Deploy the app. Your program should be able to save and display the stored values.
9. Create a new XML page for Record.
Create a new XML file name, activity_record.xml.
Paste the following code.
activity_record.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">

</LinearLayout>

Refer to activity_calculator.xml. Cut the <LinearLayout> lines that consist of TextView id


displaysaveditem and paste it into activity_record.xml in between the existing <LinearLayout>.
10. Create a new activity for Record.
Create a new file name RecordActivity.java. Copy the package from MainActivity.java. (The first
line of your java file).
Then, paste the following code.
RecordActivity.java
import android.support.v7.app.AppCompatActivity;
public class RecordActivity extends AppCompatActivity {
TextView tvStored;
private String file = "mydata";

CSNB544 Lab 7 File


(2016)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
tvStored=(TextView)findViewById(R.id.displaysaveditem);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_options, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_home:
startActivity(new Intent(RecordActivity.this, CalculatorActivity.class));
finish();
break;
case R.id.menu_info:
startActivity(new Intent(RecordActivity.this, InformationActivity.class));
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}

11. Code for Read data from file only.


Refer to CalculatorActivity.java. Copy the following lines that perform operation Read from file to
RecordActivity.java right after the highlighted line.
RecordActivity.java
tvStored=(TextView)findViewById(R.id.displaysaveditem);
//Read from file
try{
FileInputStream fin = openFileInput(file);
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}

CSNB544 Lab 7 File


(2016)
tvStored.setText(temp);
Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
}
catch(Exception e){
}
}

12. Modify CalculatorActivity.java.


Refer to CalculatorActivity.java. Remove the following lines
try{
FileInputStream fin = openFileInput(file);
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
tvStored.setText(temp);
Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
}
catch(Exception e){
}

and then replace it with


CalculatorActivity.java
startActivity(new Intent(CalculatorActivity.this, RecordActivity.class));
finish();

13. Update AndroidManifest.xml.


Update the AndroidManifest.xml with the newly created activity file, RecordActivity.java.
14. Deployment.
Deploy the app. Your program should be able to display the stored values in the Record Activity
page.

CSNB544 Lab 7 File


(2016)
Student ID
Student Name

Student 1
SN095632
Hexagon Jeyaram A/L Thangarasah

Student 2
SN094851
Surendar Rao A/L Kanisan

Exercise
Provide the following screen capture as a proof of the completion of the following activities.
Num.

Activity (Screen Capture)

Marks
Allocation

Add Save button (Design Preview)

Add Read button (Design Preview)

Add a temporary TextView (Code)

Marks

CSNB544 Lab 7 File


(2016)

Declare a new variable to handle the buttons and TextView in


Java (Code)

Declare a new variable to handle File. (Code)

Write data into File. (Code)

CSNB544 Lab 7 File


(2016)

Read data from File. (Code)

Deployment (Mobile Screen Capture)

CSNB544 Lab 7 File


(2016)

Create a new XML page for Record (Code)

10

Create a new activity for Record. (Code)

CSNB544 Lab 7 File


(2016)

11

Code for Read data from file only (Code)

CSNB544 Lab 7 File


(2016)
12

Modify CalculatorActivity.java (Code)

13

Update AndroidManifest.xml. (Code)

14

Deployment (Record Page)

CSNB544 Lab 7 File


(2016)
TOTAL

15

Rename your word document file to:


CSNB544 Lab 7 <student Name1> <student Name2>.docx before the submission.
Submission thru Moodle
Moodle > CSIT > SN > Nur Shakirah Md Salleh > Mobile Application Development > Lab 7

Das könnte Ihnen auch gefallen