Professional Documents
Culture Documents
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:
▪ Then delete the code which is there and type the code as given below.
Code forActivity_main.xml:
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
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.
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" />
17
18 <category
android:name="android.intent.category.LAUNCHER" />
19 </intent-filter>
20 </activity>
21 </application>
</manifest>
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
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
(Button) findViewById(R.id.button2);
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();
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
});
81
82 clear.setOnClickListener(new
View.OnClickListener()
83
{
84
@Override
85 public void onClick(View v)
86
{
87 e1.setText("");
88
}
89
});
90
}
Output: