Mad Lab Manual - Formatted
Mad Lab Manual - Formatted
16CS513
MOBILE APPLICATION DEVELOPMENT LAB MANUAL
1
COMPUTER SCIENCE AND ENGINEERING
ANDROID:
Android is an open source and Linux-based operating system for mobile devices such
as smartphones 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, smartphones, 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:
2
COMPUTER SCIENCE AND ENGINEERING
ANDROID APPLICATIONS:
Android applications are usually developed in the Java language using the Android
Software Development Kit.
Once 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.
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
3
COMPUTER SCIENCE AND ENGINEERING
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
The emulator is an application that re-creates the exact hardware environment of an Android
device: from its CPU and memory, through to the sound chips and the video display. The
emulator is built on an existing emulator called QEMU, which is similar to other virtual
machine applications you may have used, like VirtualBox or VMWare.
MANIFEST FILE
AndroidManifest.xml 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 Android apps
ANDROID STUDIO
Android Studio, includes all the tools you need to develop your Android apps. IntelliJ
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
4
COMPUTER SCIENCE AND ENGINEERING
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
bytecode. ART then runs the DEX code. ART can convert the DEX bytecode 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.
5
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 -> com.example.exno -> MainActivity.java 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="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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"
android:orientation="horizontal">
6
COMPUTER SCIENCE AND ENGINEERING
<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">
7
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
android:id="@+id/btnDiv"
android:layout_width="55dp"
8
COMPUTER SCIENCE AND ENGINEERING
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 com.example.naveenrajy.ex1nativecalc;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
9
COMPUTER SCIENCE AND ENGINEERING
result = findViewById(R.id.resultText);
editTextN1 = findViewById(R.id.editText1);
editTextN2 = findViewById(R.id.editText2);
10
COMPUTER SCIENCE AND ENGINEERING
if(TextUtils.isEmpty(editTextN1.getText().toString()) ||
TextUtils.isEmpty(editTextN2.getText().toString()))
return;
int mul;
String n1 = editTextN1.getText().toString();
int num1 = Integer.valueOf(n1);
String n2 = editTextN2.getText().toString();
int num2 = Integer.valueOf(n2);
mul = num1 * num2;
result.setText(num1+" * "+num2+" = "+mul);
}
11
COMPUTER SCIENCE AND ENGINEERING
SAMPLE OUTPUT
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
12
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 -> com.example.exno -> MainActivity.java 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>
13
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
android:id="@+id/textViewCountry"
14
COMPUTER SCIENCE AND ENGINEERING
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>
strings.xml
<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>
15
COMPUTER SCIENCE AND ENGINEERING
Activity:
MainActivity.java
package com.example.naveenrajy.ex2listviewflags;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
16
COMPUTER SCIENCE AND ENGINEERING
SecondActivity.java
package com.example.naveenrajy.ex2listviewflags;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
17
COMPUTER SCIENCE AND ENGINEERING
textView.setText("hello");
}
}
}
SAMPLE OUTPUT
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
18
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 -> com.example.exno -> MainActivity.java 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"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.javatpoint.com.activitylifecycle.MainActivity">
<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"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
19
COMPUTER SCIENCE AND ENGINEERING
</android.support.constraint.ConstraintLayout>
Activity:
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart invoked");
}
20
COMPUTER SCIENCE AND ENGINEERING
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
}
}
SAMPLE OUTPUT
21
COMPUTER SCIENCE AND ENGINEERING
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
22
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 -> com.example.exno -> MainActivity.java 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"
android:layout_y="110dp"
23
COMPUTER SCIENCE AND ENGINEERING
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"
android:layout_y="200dp"
24
COMPUTER SCIENCE AND ENGINEERING
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"
25
COMPUTER SCIENCE AND ENGINEERING
android:layout_x="100dp"
android:layout_y="500dp"
android:text="View All"
android:textSize="30dp" />
</AbsoluteLayout>
Activity:
MainActivity.java
package com.example.exno5;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
Rollno=(EditText)findViewById(R.id.Rollno);
Name=(EditText)findViewById(R.id.Name);
Marks=(EditText)findViewById(R.id.Marks);
Insert=(Button)findViewById(R.id.Insert);
Delete=(Button)findViewById(R.id.Delete);
Update=(Button)findViewById(R.id.Update);
View=(Button)findViewById(R.id.View);
26
COMPUTER SCIENCE AND ENGINEERING
ViewAll=(Button)findViewById(R.id.ViewAll);
Insert.setOnClickListener(this);
Delete.setOnClickListener(this);
Update.setOnClickListener(this);
View.setOnClickListener(this);
ViewAll.setOnClickListener(this);
27
COMPUTER SCIENCE AND ENGINEERING
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE rollno='"+Rollno.getText()+"'");
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(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst()) {
db.execSQL("UPDATE student SET name='" + Name.getText() + "',marks='" +
Marks.getText() +
"' WHERE rollno='"+Rollno.getText()+"'");
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(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
28
COMPUTER SCIENCE AND ENGINEERING
29
COMPUTER SCIENCE AND ENGINEERING
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}
SAMPLE OUTPUT
30
COMPUTER SCIENCE AND ENGINEERING
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
31
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 -> com.example.exno -> MainActivity.java 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>
32
COMPUTER SCIENCE AND ENGINEERING
AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<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="android.intent.action.MAIN" />
</manifest>
Activity:
MainActivity.java
package com.example.exno6;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
33
COMPUTER SCIENCE AND ENGINEERING
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
new MyAsyncTask().execute();
}
34
COMPUTER SCIENCE AND ENGINEERING
{
if (xpp.getName().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title"))
{
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
}
else if (xpp.getName().equalsIgnoreCase("link"))
{
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}
else if(eventType==XmlPullParser.END_TAG &&
xpp.getName().equalsIgnoreCase("item"))
{
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ArrayAdapter adapter)
{
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,
headlines);
35
COMPUTER SCIENCE AND ENGINEERING
setListAdapter(adapter);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
Uri uri = Uri.parse((links.get(position)).toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
36
COMPUTER SCIENCE AND ENGINEERING
SAMPLE OUTPUT
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
37
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 -> com.example.exno -> MainActivity.java 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"
38
COMPUTER SCIENCE AND ENGINEERING
<fragment
android:name="com.javacodegeeks.android.fragmentstest.FragmentOne"
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"
39
COMPUTER SCIENCE AND ENGINEERING
android:layout_height="match_parent"
android:text="This is fragment No.2"
android:textStyle="bold" />
</LinearLayout>
Activity:
FragmentOne.java:
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
return inflater.inflate(
R.layout.fragment_one, container, false);
}
}
FragmentTwo.java:
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
40
COMPUTER SCIENCE AND ENGINEERING
return inflater.inflate(
R.layout.fragment_two, container, false);
}
}
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
41
COMPUTER SCIENCE AND ENGINEERING
if(view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
}else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
SAMPLE OUTPUT
42
COMPUTER SCIENCE AND ENGINEERING
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
43
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 -> com.example.exno -> MainActivity.java 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:
44
COMPUTER SCIENCE AND ENGINEERING
android:textSize="24sp" />
<TextView
android:id="@+id/textlat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp" />
<TextView
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>
AndroidManifest.xml:
45
COMPUTER SCIENCE AND ENGINEERING
Activity:
MainActivity.java
package com.naveenraj.appeightgps;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener {
LocationManager lmanager;
String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = lmanager.getBestProvider(criteria, false);
// if (Build.VERSION.SDK_INT>=23 &&
ContextCompat.CheckSelfPermission(this,android.Manifest.Permission.ACCESS_FINE_LO
CATION)!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.CheckSelfPermission(this,android.Manifest.Permission.ACCESS_COARSE
_LOCATION)!= PackageManager.PERMISSION_GRANTED)
// {
// return; //use this condition for AppCompatActivity
// }
if (provider != null && !provider.equals("")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
46
COMPUTER SCIENCE AND ENGINEERING
// 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.
return;
}
}
Location location = lmanager.getLastKnownLocation(provider);
lmanager.requestLocationUpdates(provider,2000,1,this);
if (location!=null)
{
onLocationChanged(location);
}
else
{
Toast.makeText(getBaseContext(),"Location not available",Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getBaseContext(),"No Provider Found",Toast.LENGTH_LONG).show();
}
}
@Override
public void onLocationChanged(Location location) {
TextView tv1 = (TextView) findViewById(R.id.textlat);
TextView tv2 = (TextView) findViewById(R.id.textLong);
tv1.setText(""+location.getLatitude());
tv2.setText(""+location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
47
COMPUTER SCIENCE AND ENGINEERING
SAMPLE OUTPUT
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
48
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 -> com.example.exno -> MainActivity.java 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="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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" >
49
COMPUTER SCIENCE AND ENGINEERING
</Button>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/button1"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" >
</ImageView>
</RelativeLayout>
Activity:
MainActivity.java
package com.example.simplecamera;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
50
COMPUTER SCIENCE AND ENGINEERING
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
51
COMPUTER SCIENCE AND ENGINEERING
SAMPLE OUTPUT
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
52
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 -> com.example.exno -> MainActivity.java 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="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>
53
COMPUTER SCIENCE AND ENGINEERING
Activity:
MainActivity.java
package com.example.sensorsimple;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
import android.hardware.SensorEvent;
import android.hardware.Sensor;
import java.util.List;
public class MainActivity extends Activity {
SensorManager sm = null;
TextView textView1 = null;
List list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView)findViewById(R.id.textView1);
list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
if(list.size()>0){
sm.registerListener(sel, (Sensor) list.get(0),
SensorManager.SENSOR_DELAY_NORMAL);
}else{
54
COMPUTER SCIENCE AND ENGINEERING
@Override
protected void onStop() {
if(list.size()>0){
sm.unregisterListener(sel);
}
super.onStop();
}
}
SAMPLE OUTPUT
55
COMPUTER SCIENCE AND ENGINEERING
CONCLUSION
Thus the android application is developed and executed using android studio
successfully.
56