You are on page 1of 11

Technological University of the Philippines Name: ___________________________________________

Manila Campus Course/Year/Section: _______________________________


College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______

ANDROID STUDIO
Finals (3) Laboratory 8
I. OBJECTIVES
At the end of the lesson, the students are expected to:
 Understand the Mobile Development environment to familiarize
 Design simple GUI application with activity and intents e.g. calculator.
 Create an android app for database creation using SQLite Database.
 To develop a native application that uses GPS location information
 To Implement an application that writes data to the SD card
1. MATERIALS
Pen and Notebook
Laptop
Cellphone
Android studio Others:____________________
2. GENERAL INSTRUCTION
Designing layout for the AndroidApplication:

▪ Click on app -> res -> layout -> activity_main.xml.

▪ Now click on Text as shown below.

Prepared by: Prof. Javi-Anne Interno Page 1

Copyright © 2021 by Electronics Engineering technology Department


Technological University of the Philippines Name: ___________________________________________
Manila Campus Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______

▪ Then delete the code which is there and type the code as given below.

Code forActivity_main.xml:

1 <?xml version="1.0" encoding="utf-8"?>


2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:layout_margin="20dp"
6 android:orientation="vertical">

8 <EditText
9 android:id="@+id/editText"
10 android:layout_width="match_parent"
11 android:layout_height="wrap_content"
12 android:singleLine="true"
13 android:textSize="30dp" />

14

15 <Button

Prepared by: Prof. Javi-Anne Interno Page 2

Copyright © 2021 by Electronics Engineering technology Department


Technological University of the Philippines Name: ___________________________________________
Manila Campus Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______

16 android:id="@+id/button"
17 android:layout_width="match_parent"
18 android:layout_height="wrap_content"
19 android:layout_margin="10dp"
20 android:text="Write Data"
21 android:textSize="30dp" />

22

23 <Button

24 android:id="@+id/button2"
25 android:layout_width="match_parent"
26 android:layout_height="wrap_content"
27 android:layout_margin="10dp"
28 android:text="Read data"
29 android:textSize="30dp"/>

30

31 <Button
32 android:id="@+id/button3"
33 android:layout_width="match_parent"
34 android:layout_height="wrap_content"
35 android:layout_margin="10dp"
36 android:text="Clear"
37 android:textSize="30dp"/>

38

39 </LinearLayout>

▪ Now click on Design and your application will look as given below.

Prepared by: Prof. Javi-Anne Interno Page 3

Copyright © 2021 by Electronics Engineering technology Department


Technological University of the Philippines Name: ___________________________________________
Manila Campus Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______

▪ So now the designing part is completed.

Adding permissions in Manifest for the Android Application:

▪ Click on app -> manifests -> AndroidManifest.xml

▪ Now include the WRITE_EXTERNAL_STORAGE permissionsin the

AndroidManifest.xml file as shown below

Prepared by: Prof. Javi-Anne Interno Page 4

Copyright © 2021 by Electronics Engineering technology Department


Technological University of the Philippines Name: ___________________________________________
Manila Campus Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______

Code for AndroidManifest.xml:

1 <?xml version="1.0" encoding="utf-8"?>


2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"

3 package="com.example.exno9">

5 <uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-
6 permission>

8 <application
9 android:allowBackup="true"
10 android:icon="@mipmap/ic_launcher"
11 android:label="@string/app_name"
12 android:supportsRtl="true"
13 android:theme="@style/AppTheme" >
14 <activity android:name=".MainActivity" >

15 <intent-filter>
16 <action android:name="android.intent.action.MAIN" />

Prepared by: Prof. Javi-Anne Interno Page 5

Copyright © 2021 by Electronics Engineering technology Department


Technological University of the Philippines Name: ___________________________________________
Manila Campus Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______

17

18 <category
android:name="android.intent.category.LAUNCHER" />
19 </intent-filter>
20 </activity>
21 </application>

</manifest>

▪ So now the Permissions are added in the Manifest.

Java Coding for the Android Application:

▪ Click on app -> java -> com.example.exno9 -> MainActivity.


▪ Then delete the code which is there and type the code as given below.

Code for MainActivity.java:

1 package com.example.exno9;

3 import android.os.Bundle;
4 import android.support.v7.app.AppCompatActivity;
5 import android.view.View;
6 import android.widget.Button;
7 import android.widget.EditText; 8import android.widget.Toast; 9

10 import java.io.BufferedReader;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;

14import java.io.InputStreamReader;

15

16 public class MainActivity extendsAppCompatActivity


17 {
18 EditText e1;

Prepared by: Prof. Javi-Anne Interno Page 6

Copyright © 2021 by Electronics Engineering technology Department


Technological University of the Philippines Name: ___________________________________________
Manila Campus Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______

19 Button write,read,clear;
20 @Override
21 protected void onCreate(Bundle savedInstanceState)
22 {
23 super.onCreate(savedInstanceState);
24 setContentView(R.layout.activity_main);

25

26 e1= (EditText) findViewById(R.id.editText);


27 write= (Button) findViewById(R.id.button); 28 read=

(Button) findViewById(R.id.button2);

29 clear= (Button) findViewById(R.id.button3);

30

31 write.setOnClickListener(new View.OnClickListener()
32 {
33 @Override
34 public void onClick(Viewv)
35 {
36 String message=e1.getText().toString();
37 try
38 {
39 File f=new File("/sdcard/myfile.txt");
40 f.createNewFile();
41 FileOutputStream fout=new FileOutputStream(f);
42 fout.write(message.getBytes());
43 fout.close();
44 Toast.makeText(getBaseContext(),"Data Written in
SDCARD",Toast.LENGTH_LONG).show();
45
}
46 catch (Exception e)
47
{
48
Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).sh
49
ow();

Prepared by: Prof. Javi-Anne Interno Page 7

Copyright © 2021 by Electronics Engineering technology Department


Technological University of the Philippines Name: ___________________________________________
Manila Campus Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______
50
}
51
}
52
});

53

54 read.setOnClickListener(new View.OnClickListener()
55 {
56 @Override
57 public void onClick(Viewv)
58 {
59 String message; 60 String buf = "";

61 try
62 {
63 File f = new File("/sdcard/myfile.txt");
64 FileInputStream fin = new FileInputStream(f);
65 BufferedReader br = new BufferedReader(new
InputStreamReader(fin));
66 while ((message = br.readLine()) != null)
67 {
68 buf += message;
69 }
70 e1.setText(buf);
71 br.close();
72 fin.close();
73 Toast.makeText(getBaseContext(),"Data Recived from
SDCARD",Toast.LENGTH_LONG).show();
74
}

75 catch (Exceptione)
76 {
77 Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
78
}
79
}
80
});

Prepared by: Prof. Javi-Anne Interno Page 8

Copyright © 2021 by Electronics Engineering technology Department


Technological University of the Philippines Name: ___________________________________________
Manila Campus Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______

81

82 clear.setOnClickListener(new
View.OnClickListener()
83
{
84
@Override
85 public void onClick(View v)
86
{
87 e1.setText("");
88
}
89
});
90
}

▪ So now the Coding part is also completed.


▪ Now run the application to see the output.

Output:

Prepared by: Prof. Javi-Anne Interno Page 9

Copyright © 2021 by Electronics Engineering technology Department


Technological University of the Philippines Name: ___________________________________________
Manila Campus Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______

Prepared by: Prof. Javi-Anne Interno Page 10

Copyright © 2021 by Electronics Engineering technology Department


Technological University of the Philippines Name: ___________________________________________
Manila Campus Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______

Prepared by: Prof. Javi-Anne Interno Page 11

Copyright © 2021 by Electronics Engineering technology Department

You might also like