Android Mobile Computing Lab Guide
Android Mobile Computing Lab Guide
1
COMPUTER SCIENCE AND ENGINEERING
ANDROID:
Android is an open source and Linux-based operating system for mobile devices such
as smart phones and tablet computers. Android was developed by the Open Handset Alliance,
led by Google, and other companies. It is a complete set of software for mobile devices such
as tablet computers, notebooks, smart phones, electronic book readers, set-top boxes etc.
It can be thought of as a mobile operating system. But it is not limited to mobile only.
It is currently used in various devices such as mobiles, tablets, televisions etc.
ANDROID ARCHITECTURE:
ANDROID APPLICATIONS:
Android applications are usually developed in the Java language using the Android
Software Development [Link] developed, Android applications can be packaged easily and
sold out either through a store such as Google Play, SlideME, Opera Mobile Store, Mobango,
F-droid and the Amazon Appstore.
2
COMPUTER SCIENCE AND ENGINEERING
Android powers hundreds of millions of mobile devices in more than 190 countries
around the world. It's the largest installed base of any mobile platform and growing fast.
Every day more than 1 million new Android devices are activated worldwide.
HISTORY OF ANDROID:
The code names of android ranges from A to N currently, such as Aestro, Blender,
Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice Cream Sandwitch, Jelly Bean,
KitKat, Lollipop and Marshmallow
API LEVEL
API Level is an integer value that uniquely identifies the framework API revision
offered by a version of the Android platform.
LAYOUT
A layout describes the appearance of the screen. Layouts are written as XML files and
they tell Android how the different screen elements are arranged. A layout defines the
structure for a user interface in your app, such as in an activity. All elements in the layout are
built using a hierarchy of View and ViewGroup objects. A View usually draws something the
user can see and interact with. A layout may contain any type of widgets such as buttons,
labels, textboxes, and so on.
ACTIVITY
An activity is a single, defined thing that your user can do. You might have an activity
to compose an email, take a photo, or find a contact. Activities are usually associated with
one screen, and they’re written in Java. An activity is a single, focused thing that the user can
do. Almost all activities interact with the user, so the Activity class takes care of creating a
window for you in which you can place your UI with Activity. SetContentView(View).
RESOURCES
There are many more items which you use to build a good Android application. Apart
from coding for the application, you take care of various other resources like static content
that your code uses, such as bitmaps, colors, layout definitions, user interface strings,
animation instructions, and more. These resources are always maintained separately in
various sub-directories under res/ directory of the project
3
COMPUTER SCIENCE AND ENGINEERING
The Android emulator allows you to run your app on an Android virtual device
(AVD). The AVD behaves just like a physical Android device. You can set up numerous
AVDs, each emulating a different type of device.
MANIFEST FILE
[Link] contains information about the app itself. It lives in the
app/src/main folder.
ANDROID SDK
The Android Software Development Kit contains the libraries and tools you need to
develop Androidapps
ANDROID STUDIO
Android Studio, includes all the tools you need to develop your [Link]
IDEA is one of the most popular IDEs for Java development. Android Studio is a version of
IDEA that includes a version of the Android SDK and extra GUI tools to help you with your
app development. Android Studio gives you templates you can use to help you create new
apps and classes, and it makes it easy to do things such as package your apps and run them.
ANDROID RUNTIME
Code doesn’t actually run inside an ordinary Java VM. It runs on the Android runtime
(ART)instead, and on older devices it runs in a predecessor to ART called Dalvik. This
means that you write your Java source code, compile it into .class files using the Java
compiler, and then the .class files get stitched into a single file in DEX format, which is
smaller, more efficient byte code. ART then runs the DEX code. ART can convert the DEX
byte code into native code that can run directly on the CPU of the Android device. This
makes the app run a lot faster, and use a lot less battery power.
CONCLUSION
Thus the study of Android Platform and Project Structure is learned and understand
successfully.
4
COMPUTER SCIENCE AND ENGINEERING
PROCEDURE
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name and click Next.
3. Then select the Minimum SDK and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. Click on app -> res -> layout -> activity_main.xml and edit it
7. Click on app -> java -> [Link] -> [Link] and edit it
8. Start the Android Virtual Device
9. Click on run button to start the application in AVD
PROGARAM:
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
5
COMPUTER SCIENCE AND ENGINEERING
android:orientation="horizontal">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:ems="5"
android:inputType="number"
android:textSize="20sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:ems="5"
android:inputType="number"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:orientation="horizontal">
6
COMPUTER SCIENCE AND ENGINEERING
<Button
android:id="@+id/btnAdd"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:onClick="Add"
android:text="+"
android:textSize="30sp" />
<Button
android:id="@+id/btnSub"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:onClick="Substract"
android:text="-"
android:textSize="30sp" />
<Button
android:id="@+id/btnMul"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:onClick="Multiply"
android:text="*"
android:textSize="30sp" />
<Button
7
COMPUTER SCIENCE AND ENGINEERING
android:id="@+id/btnDiv"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="/"
android:textSize="30sp" />
</LinearLayout>
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="180dp"
android:text="Answer is"
android:textSize="35sp" />
</RelativeLayout>
Activity:
package [Link].ex1nativecalc;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
8
COMPUTER SCIENCE AND ENGINEERING
[Link](savedInstanceState);
setContentView([Link].activity_main);
buttonDiv = findViewById([Link]);
result = findViewById([Link]);
editTextN1 = findViewById([Link].editText1);
editTextN2 = findViewById([Link].editText2);
9
COMPUTER SCIENCE AND ENGINEERING
10
COMPUTER SCIENCE AND ENGINEERING
SAMPLE OUTPUT
CONCLUSION
11
COMPUTER SCIENCE AND ENGINEERING
Thus the android application is developed and executed using android studio
successfully.
PROCEDURE
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name and click Next.
3. Then select the Minimum SDK and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. Click on app -> res -> layout -> activity_main.xml and edit it
7. Click on app -> java -> [Link] -> [Link] and edit it
8. Start the Android Virtual Device
9. Click on run button to start the application in AVD
PROGARAM:
Layout:
activity_main.xml:
<ListView
android:id="@+id/lview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
12
COMPUTER SCIENCE AND ENGINEERING
activity_list.xml:
<TextView
android:id="@+id/textViewList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="40sp" />
</LinearLayout>
activity_second.xml:
<ImageView
android:id="@+id/imageViewFlag"
android:layout_width="172dp"
android:layout_height="159dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="24dp"
android:layout_marginTop="18dp"
android:src="@drawable/india" />
<TextView
13
COMPUTER SCIENCE AND ENGINEERING
android:id="@+id/textViewCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="38dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:text="@string/India"
android:textSize="20sp" />
</LinearLayout>
[Link]
<resources>
<string name="app_name">Ex2ListViewFlags</string>
<string-array name="options">
<item>India</item>
<item>England</item>
<item>China</item>
</string-array>
<string name="India">The National Flag of India is a horizontal rectangular tricolour of
India saffron, white and India green; with the Ashoka Chakra, a 24-spoke wheel, in navy blue
at its centre
</string>
<string name="China">The flag of China, also known as the Five-star Red Flag, is a red field
charged in the canton (upper corner nearest the flagpole) with five golden stars.</string>
<string name="Japan">The Nisshōki flag is designated as the national flag in the Law
Regarding the National Flag and National Anthem, which was promulgated and became
effective on August 13, 1999.</string>
<string name="America">It consists of thirteen equal horizontal stripes of red (top and
bottom) alternating with white, with a blue rectangle in the canton (referred to specifically as
the "union") bearing fifty small, white, five-pointed stars arranged in nine offset horizontal
rows, where rows of six stars (top and bottom) alternate with rows of five stars.</string>
<string name="Brazil">The flag of Brazil (Portuguese: Bandeira do Brasil), known in
Portuguese as A Auriverde (The Yellow-and-green One), is a blue disc depicting a starry sky
(which includes the Southern Cross) spanned by a curved band inscribed with the national
motto "Ordem e Progresso" ("Order and Progress"), within a yellow rhombus, on a green
field</string>
</resources>
Activity:
[Link]
14
COMPUTER SCIENCE AND ENGINEERING
package [Link].ex2listviewflags;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]
package [Link].ex2listviewflags;
import [Link];
import [Link];
import [Link];
import [Link];
15
COMPUTER SCIENCE AND ENGINEERING
import [Link];
16
COMPUTER SCIENCE AND ENGINEERING
SAMPLE OUTPUT
17
COMPUTER SCIENCE AND ENGINEERING
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
EX NO:4 DEVELOPING AN APPLICATION WITH THE SUPPORT OF
ACTIVITY LIFECYCLE
PROCEDURE
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name and click Next.
3. Then select the Minimum SDK and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. Click on app -> res -> layout -> activity_main.xml and edit it
7. Click on app -> java -> [Link] -> [Link] and edit it
8. Start the Android Virtual Device
9. Click on run button to start the application in AVD
PROGARAM:
Layout:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
18
COMPUTER SCIENCE AND ENGINEERING
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
Activity:
[Link]
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
[Link]();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
[Link]();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
[Link]();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
[Link]();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
[Link]();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
19
COMPUTER SCIENCE AND ENGINEERING
[Link]();
Log.d("lifecycle","onDestroy invoked");
}
}
SAMPLE OUTPUT
CONCLUSION
20
COMPUTER SCIENCE AND ENGINEERING
Thus the android application is developed and executed using android studio
successfully.
EX NO:5 WRITE AN APPLICATION THAT USES SQLITE DATABASES
PROCEDURE
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name and click Next.
3. Then select the Minimum SDK and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. Click on app -> res -> layout -> activity_main.xml and edit it
7. Click on app -> java -> [Link] -> [Link] and edit it
8. Start the Android Virtual Device
9. Click on run button to start the application in AVD
PROGARAM:
Layout:
activity_main.xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
21
COMPUTER SCIENCE AND ENGINEERING
android:layout_y="110dp"
android:text="Enter Rollno:"
android:textSize="20sp" />
<EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="160dp"
android:text="Enter Name:"
android:textSize="20sp" />
<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="150dp"
android:inputType="text"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="210dp"
android:text="Enter Marks:"
android:textSize="20sp" />
<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
22
COMPUTER SCIENCE AND ENGINEERING
android:layout_y="200dp"
android:inputType="number"
android:textSize="20sp" />
<Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="300dp"
android:text="Insert"
android:textSize="30dp" />
<Button
android:id="@+id/Delete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="300dp"
android:text="Delete"
android:textSize="30dp" />
<Button
android:id="@+id/Update"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="400dp"
android:text="Update"
android:textSize="30dp" />
<Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="400dp"
android:text="View"
android:textSize="30dp" />
<Button
android:id="@+id/ViewAll"
android:layout_width="200dp"
android:layout_height="wrap_content"
23
COMPUTER SCIENCE AND ENGINEERING
android:layout_x="100dp"
android:layout_y="500dp"
android:text="View All"
android:textSize="30dp" />
</AbsoluteLayout>
Activity:
[Link]
package [Link].exno5;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Rollno=(EditText)findViewById([Link]);
Name=(EditText)findViewById([Link]);
Marks=(EditText)findViewById([Link]);
Insert=(Button)findViewById([Link]);
Delete=(Button)findViewById([Link]);
Update=(Button)findViewById([Link]);
View=(Button)findViewById([Link]);
24
COMPUTER SCIENCE AND ENGINEERING
ViewAll=(Button)findViewById([Link]);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
25
COMPUTER SCIENCE AND ENGINEERING
{
[Link]("DELETE FROM student WHERE rollno='"+[Link]()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Updating a record in the Student table
if(view==Update)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]()) {
[Link]("UPDATE student SET name='" + [Link]() + "',marks='" +
[Link]() +
"' WHERE rollno='"+[Link]()+"'");
showMessage("Success", "Record Modified");
}
else {
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Display a record from the Student table
if(view==View)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
26
COMPUTER SCIENCE AND ENGINEERING
if([Link]())
{
[Link]([Link](1));
[Link]([Link](2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
// Displaying all the records
if(view==ViewAll)
{
Cursor c=[Link]("SELECT * FROM student", null);
if([Link]()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while([Link]())
{
[Link]("Rollno: "+[Link](0)+"\n");
[Link]("Name: "+[Link](1)+"\n");
[Link]("Marks: "+[Link](2)+"\n\n");
}
showMessage("Student Details", [Link]());
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
[Link](true);
[Link](title);
[Link](message);
[Link]();
}
public void clearText()
{
[Link]("");
[Link]("");
[Link]("");
27
COMPUTER SCIENCE AND ENGINEERING
[Link]();
}
}
SAMPLE OUTPUT
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
28
COMPUTER SCIENCE AND ENGINEERING
PROCEDURE
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name and click Next.
3. Then select the Minimum SDK and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. Click on app -> res -> layout -> activity_main.xml and edit it
7. Click on app -> java -> [Link] -> [Link] and edit it
8. Start the Android Virtual Device
9. Click on run button to start the application in AVD
PROGARAM:
Layout:
activity_main.xml:
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
[Link]:
29
COMPUTER SCIENCE AND ENGINEERING
<uses-permission android:name="[Link]"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="[Link]" />
</manifest>
Activity:
[Link]
package [Link].exno6;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
30
COMPUTER SCIENCE AND ENGINEERING
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
new MyAsyncTask().execute();
}
31
COMPUTER SCIENCE AND ENGINEERING
}
catch (MalformedURLException e)
{
[Link]();
}
catch (XmlPullParserException e)
{
[Link]();
}
catch (IOException e)
{
[Link]();
}
return null;
}
protected void onPostExecute(ArrayAdapter adapter)
{
adapter = new ArrayAdapter([Link], [Link].simple_list_item_1,
headlines);
setListAdapter(adapter);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
Uri uri = [Link](([Link](position)).toString());
32
COMPUTER SCIENCE AND ENGINEERING
33
COMPUTER SCIENCE AND ENGINEERING
SAMPLE OUTPUT
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
EX NO:7 WRITE AN APPLICATION TO IMPLEMENT FRAGMENT
34
COMPUTER SCIENCE AND ENGINEERING
PROCEDURE
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name and click Next.
3. Then select the Minimum SDK and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. Click on app -> res -> layout -> activity_main.xml and edit it
7. Click on app -> java -> [Link] -> [Link] and edit it
8. Start the Android Virtual Device
9. Click on run button to start the application in AVD
PROGARAM:
Layout:
activity_main.xml:
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<fragment
android:name="[Link]"
35
COMPUTER SCIENCE AND ENGINEERING
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
fragment_one.xml:
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="This is fragment No.1"
android:textStyle="bold" />
</LinearLayout>
fragment_two.xml:
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is fragment No.2"
android:textStyle="bold" />
</LinearLayout>
36
COMPUTER SCIENCE AND ENGINEERING
Activity:
[Link]:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
return [Link](
[Link].fragment_one, container, false);
}
}
[Link]:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
37
COMPUTER SCIENCE AND ENGINEERING
return [Link](
[Link].fragment_two, container, false);
}
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
public void selectFrag(View view) {
Fragment fr;
if(view == findViewById([Link].button2)) {
fr = new FragmentTwo();
}else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = [Link]();
[Link]([Link].fragment_place, fr);
[Link]();
}
}
SAMPLE OUTPUT
38
COMPUTER SCIENCE AND ENGINEERING
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
EX NO:8 DEVELOP AN ANDROID APPLICATION THAT MAKES USE OF GPS
39
COMPUTER SCIENCE AND ENGINEERING
PROCEDURE
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name and click Next.
3. Then select the Minimum SDK and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. Click on app -> res -> layout -> activity_main.xml and edit it
7. Click on app -> java -> [Link] -> [Link] and edit it
8. Start the Android Virtual Device
9. Click on run button to start the application in AVD
PROGARAM:
Layout:
activity_main.xml:
40
COMPUTER SCIENCE AND ENGINEERING
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Longitude:"
android:textSize="24sp" />
<TextView
android:id="@+id/textLong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp" />
</LinearLayout>
</RelativeLayout>
[Link]:
Activity:
[Link]
package [Link];
41
COMPUTER SCIENCE AND ENGINEERING
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity implements LocationListener {
LocationManager lmanager;
String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
lmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = [Link](criteria, false);
// if ([Link].SDK_INT>=23 &&
[Link](this,[Link].ACCESS_FINE_LO
CATION)!= PackageManager.PERMISSION_GRANTED &&
[Link](this,[Link].ACCESS_COARSE
_LOCATION)!= PackageManager.PERMISSION_GRANTED)
// {
// return; //use this condition for AppCompatActivity
// }
if (provider != null && ) {
if ([Link].SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission([Link].ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
checkSelfPermission([Link].ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
42
COMPUTER SCIENCE AND ENGINEERING
return;
}
}
Location location = [Link](provider);
[Link](provider,2000,1,this);
if (location!=null)
{
onLocationChanged(location);
}
else
{
[Link](getBaseContext(),"Location not available",Toast.LENGTH_LONG).show();
}
}
else
{
[Link](getBaseContext(),"No Provider Found",Toast.LENGTH_LONG).show();
}
}
@Override
public void onLocationChanged(Location location) {
TextView tv1 = (TextView) findViewById([Link]);
TextView tv2 = (TextView) findViewById([Link]);
[Link](""+[Link]());
[Link](""+[Link]());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
SAMPLE OUTPUT
43
COMPUTER SCIENCE AND ENGINEERING
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
44
COMPUTER SCIENCE AND ENGINEERING
PROCEDURE
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name and click Next.
3. Then select the Minimum SDK and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. Click on app -> res -> layout -> activity_main.xml and edit it
7. Click on app -> java -> [Link] -> [Link] and edit it
8. Start the Android Virtual Device
9. Click on run button to start the application in AVD
PROGARAM:
Layout:
activity_main.xml:
<RelativeLayout xmlns:androclass="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Take a Photo" >
</Button>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
45
COMPUTER SCIENCE AND ENGINEERING
android:layout_height="fill_parent"
android:layout_above="@+id/button1"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" >
</ImageView>
</RelativeLayout>
Activity:
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link](savedInstanceState);
setContentView([Link].activity_main);
[Link](new [Link]() {
46
COMPUTER SCIENCE AND ENGINEERING
@Override
public void onClick(View v) {
Intent cameraIntent = new
Intent([Link].ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) [Link]().get("data");
[Link](photo);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate([Link].activity_main, menu);
return true;
}
SAMPLE OUTPUT
47
COMPUTER SCIENCE AND ENGINEERING
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
EX NO:10 SENSOR PROGRAMMING
PROCEDURE
48
COMPUTER SCIENCE AND ENGINEERING
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name and click Next.
3. Then select the Minimum SDK and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. Click on app -> res -> layout -> activity_main.xml and edit it
7. Click on app -> java -> [Link] -> [Link] and edit it
8. Start the Android Virtual Device
9. Click on run button to start the application in AVD
PROGARAM:
Layout:
activity_main.xml:
<RelativeLayout xmlns:androclass="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="92dp"
android:layout_marginTop="114dp"
android:text="TextView" />
</RelativeLayout>
Activity:
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
49
COMPUTER SCIENCE AND ENGINEERING
import [Link];
import [Link];
public class MainActivity extends Activity {
SensorManager sm = null;
TextView textView1 = null;
List list;
SensorEventListener sel = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event) {
float[] values = [Link];
[Link]("x: "+values[0]+"\ny: "+values[1]+"\nz: "+values[2]);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
SAMPLE OUTPUT
50
COMPUTER SCIENCE AND ENGINEERING
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
51