6138 Smart Device Programming Lab
6138 Smart Device Programming Lab
1
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Contents
1. Introduction
6. Android Applications
6.1 Create Hello World application
6.2 Linking Activities using Intent
6.3 Passing Data Using an Intent Object
6.4 Implicit intents Familiarization
9. References
2
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Introduction
3
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Chapter-1
Installing Android Studio on Ubuntu
With the advancement of smart phones in the recent years, Android has
become one of the biggest phone platforms and all the tools required to build
Android applications are also freely available. Android Studio is an Integrated
Development Environment (IDE) for developing Android applications.
The first methodis to set up the required repository and install it,
The second method is to download it from the official Android site and
install it locally.
In the first method, we will be setting up the repo using command line and install it. Before
proceeding, we need to make sure that we have JDK version1.6 or greater installed.
4
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Step4: $ /opt/android-studio/bin/studio.sh
This will invoke the setup screen. Following are the screen shots that follow to
set up Android studio:
5
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
6
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Once you press the Finish button, Licence agreement will be displayed. After you accept
the licence, it starts downloading the required components.
Android studio installation will be complete after this step. When you relaunch Android
studio, you will be shown the following welcome screen from where you will be
able to start working with your Android Studio.
7
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
8
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Chapter-2
Installing Android Studio on Windows
Android Studio is one of the best and most popular IDE for development of Android
application which is released by IntelliJ IDEA. Android studio has rich functionalists and
easy to work. Android studio has great and awesome user interface, it is also smart with
coding and will save your time.
To download Android Studio for you computer, you need to visit official site of android.
There you will see android studio download button.
9
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
After downloading android studio, follow below steps to install android studio on
your computer.
Step1: Open you downloaded android studio file and wait for few seconds, it
Step2: After that the window will appear on your screen, where it will show you welcome
to android studio.
Go for "Next"
Step3: Now choose what you want to install, all are most important component in
android programming.
Go for "Next"
10
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Step4: Go for "I Agree", It's for integrating SDK, it will confirm SDK installation.
11
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Step6: Now choose the location for Android Studio and SDK.
Go for "Next"
12
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Step7: Here you need to define the size of android emulator processor. You can define
up to 2GB and more than that, depends on your RAM capacity. The size will be
determined through Haxm. This will help your android studio to run faster.
Step8: Now choose setting for shortcut of android studio on your computer.
13
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Step9: After that it will take lots of time to copy all the files to the storage location you
14
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
15
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Chapter-3
Creating New Project on Android Studio
Android Studio is one of the best IDE (Integrated Development
Environment) for Android Developers with rich functionality and control
mechanism. It was announced on 2013 and today it is one of the most popular
IDE in the world for android development purpose.
In this chapter we will learn how to create new project on android studio.
Step1: First of all you need to start your Android Studio. Then it will show you the
default start window on your screen, where you can see various options.
In this window you have to click on Start a new Android Studio project, that will
16
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Step2: After clicking on new project will get another window on your screen which will
As you can see on the above windows, you need to put Application Name,
17
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Company Domain,
Package Name,
notation.
Project Location,
You must select appropriate location for your project. That might be default location
of C drive or you can select another drive location to save your special project.
Step3: After doing all the above tasks you must have to choose platform where you want
to run your android application. Run able factors for your project. You can choose as per
your need.
18
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Step4: Now here you have to choose the activity where you will get java classes and xml
files for designing and coding purpose. You can even choose no activity format. If you
choose that there will be no auto generated codes on your project. If you select Blank
Activity, you will get auto generated codes and that will save your lots of time.
Step5: In this you have to select layout name, activity name etc..
Than after you just have to click on Finish button. Now it will take little time to
create your new project on Android Studio
19
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Chapter-4
Open, Re-open and Close an Existing Project on
Android Studio
Opening an existing project
Steps1: After opening your android studio you will get start screen on your computer
screen.
Step2: Now click on Open an Existing Android Studio project option where you have
Step3: Now you have to select your project form your specific location. Choose the
location and open that project which you want to edit on android studio.
20
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
21
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Step2: After that you need to click or point on re-open projects option and you have to
select project that you want to re-open. After clicking on project you will see option to
open project on new window and this window that depends on you how you want to re-
22
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
23
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Closing of project
This closing of project can help you to close your opening project, after closing project it
will leads you to the main window of android studio where you will find recent projects and
various options.
Step1: Go for file menu and click on close project button that will directly lead you
24
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
25
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Chapter-4
Creating Android Virtual Devices
The AVD Manager is a tool you can use to create and manage Android virtual
devices (AVDs), which define device configurations for the Android Emulator.
● In Android Studio, select Tools > Android > AVD Manager, or click the AVD
● $ android avd
The AVD Manager main screen shows your current virtual devices, as shown in figure 1.
Figure 1. The AVD Manager main screen shows your current virtual devices.
Note: If you launch the AVD Manager from the command line, the UI is different than how
available, but the command-line version of the AVD Manager is currently not documented.
Creating an AVD
You can create as many AVDs as you would like to use with the Android Emulator. To
effectively test your app, you should create an AVD that models each device type for
which you have designed your app to support. For instance, you should create an AVD
for each API level equal to and higher than the minimum version you've specified in
your manifest <uses-sdk> tag.
26
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
1. From the main screen (figure 1), click Create Virtual Device.
3. Select the desired system version for the AVD and click Next.
To launch the AVD in the Android Emulator, click the launch button in the list of
AVDs.
27
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
In case the available device definitions do not match the device type you'd like to emulate, you can
1. From the main screen (figure 1), click Create Virtual Device.
2. To begin you custom device by using an existing device profile as a template, select
4. The following Configure Hardware Profile window (figure 3) allows you to specify
various configurations such as the screen size, memory options, input type, and
sensors.
5. When you're done configuring the device, click Finish.
Figure 3. The Configure Hardware window when creating a custom device configuration.
1. Your custom device configuration is now available in the list of device definitions
(shown after you click Create Virtual Device). To continue preparing an AVD with
your custom device configuration, select the new
28
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Hardware options
If you are creating a new AVD, you can specify the following hardware options for
the AVD to emulate:
Device ram size The amount of physical RAM on the device, in megabytes. hw.ramSize
Default value is "96".
Touch-screen support Whether there is a touch screen or not on the device. Default hw.touchScreen
value is "yes".
Trackball support Whether there is a trackball on the device. Default value is hw.trackBall
"yes".
Keyboard support Whether the device has a QWERTY keyboard. Default value hw.keyboard
is "yes".
DPad support Whether the device has DPad keys. Default value is "yes". hw.dPad
GSM modem support Whether there is a GSM modem in the device. Default value hw.gsmModem
is "yes".
Camera support Whether the device has a camera. Default value is "no". hw.camera
29
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
GPS support Whether there is a GPS in the device. Default value is "yes". hw.gps
Battery support Whether the device can run on a battery. Default value is hw.battery
"yes".
Audio recording Whether the device can record audio. Default value is "yes". hw.audioInput
support
Audio playback Whether the device can play audio. Default value is "yes". hw.audioOutput
support
Cache partition support Whether we use a /cache partition on the device. Default disk.cachePartition
value is "yes".
Abstracted LCD Sets the generalized density characteristic used by the AVD's hw.lcd.density
density screen. Default value is "160".
30
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Android Applications
31
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Aim
To create an “Hello World” application in android studio
Objectives
To familiarize simple android application
Problem Statement
Create a Hello world android application using android studio and compile it using
android sdk and run it on Android Virtual Device (AVD).
Apparatus/Tools/Equipments/Components
Android studio
Principle/Theory/Flow-chart/Algorithm
Procedure
Step 2: Fill out the fields on the screen, and click Next.
32
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Step 3: Select the form factors your app will run on, check the box for Phone and Tablet.
For Minimum SDK, select API 8: Android 2.2 (Froyo).
The Minimum Required SDK is the earliest version of Android that your app
supports, indicated using the API level. To support as many devices as possible,
you should set this to the lowest version available that allows your app to provide
its core feature set.
Leave all of the other options (TV, Wear, and Glass) unchecked and click Next
Step 4: Under Add an activity to <template>, select Blank Activity and click Next.
33
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
1. Under Customize the Activity, change the Activity Name to MainActivity. The
Layout Name changes to activity_main, and the Title to MainActivity. The Menu
Resource Name is menu_main.
2. Click the Finish button to create the project.
34
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Android project is now a basic "Hello World" app that contains the following default files.
app/src/main/res/layout/activity_main.xml
This XML layout file is for the activity you added when you created the project with
Android Studio. Following the New Project workflow, Android Studio presents this
file with both a text view and a preview of the screen UI.
app/src/main/java/com.mycompany.myfirstapp/MainActivity.java
A tab for this file appears in Android Studio when the New Project workflow finishes.
When you select the file you see the class definition for the activity you
created. When you build and run the app, the Activity class starts the activity and loads
the layout
app/src/main/AndroidManifest.xml
The manifest file describes the fundamental characteristics of the app and defines
each of its components. You'll revisit this file as you follow these lessons and add
app/build.gradle
Android Studio uses Gradle to compile and build your app. There is a build.gradle
file for each module of your project, as well as a build.gradle file for the entire
project. Usually, you're only interested in thebuild.gradle file for the module, in
Directory for files that define your app's user interface like activity_my.xml,
menu/
35
REVISION 2015 [SMART DEVIC E PROGEAMMING LAB-6 138]
mipmap/
Launcher icons reside in the mipmap/ folder rather than the drawable/ folders. This
folder contains the ic_launcher.png image that appears when you run the default
app.
values/
Directory for other XML files that contain a collection of resources, such
6 | MA’DINPOLYTECHNIC COLLEGE,
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Source Code
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present. getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{ int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity"
37
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
Observations
Input
In Android Studio, select Tools > Android > AVD Manager, or click
Figure 1. The AVD Manager main screen shows your current virtual devices.
To launch the AVD in the Android Emulator, click the launch button
in the list of AVDs.
38
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
OR
1. Select one of your project's files and click Run from the toolbar.
2. In the Choose Device window that appears, select the Choose a
running device radio button, select your device, and click OK .
Android Studio installs the app on your connected device and starts it.
Output
39
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objectives:
Create an android application and understand the application life cycle in
different scenarios.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
40
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Source Code
MainActivity.java
package test.sample.com.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
41
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
{
super.onStart();
Log.d(tag, "In the onStart() event");
}
public void onRestart()
{
super.onRestart();
Log.d(tag, "In the onRestart() event");
}
public void onResume()
{
super.onResume();
Log.d(tag, "In the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag, "In the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag, "In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag, "In the onDestroy()event");
}
Result
Logcat output
Scenario
42
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
1. Opening App
D/Activity Life Cycle﹕ In the onCreate() event
D/Activity Life Cycle﹕ In the onStart() event
D/Activity Life Cycle﹕ In the onResume()
event
4. close application
D/Activity Life Cycle﹕ In the onPause() event
D/Activity Life Cycle﹕ In the onStop() event
D/Activity Life Cycle﹕ In the onDestroy()event
43
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objective :
Create an android application to understand the use of Intent.
Problem statement
Create an android application with two activities. First activity with a “ENTER” button and
second screen with a welcome message. By clicking “ENTER” it should bring the use to
the second screen.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
Source Code
MainActivity.java
44
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="ENTER" />
</RelativeLayout>
SecondActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
activity_second.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textAppearance="?
android:attr/textAppearanceMedium" android:text="Welcome "
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
45
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Observations
Input-Output
Screenshots
46
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objective :
Create an android application for authentication process.
Problem Statement
Create an android application for authentication process. First activity should accept
the user email address and pass the email id to the second activity. In the second activity
it should show the user email id and ask for the password.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
Source Code
MainActivity.java
Button mButton;
EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mButton =
(Button) findViewById(R.id.button); mEditText =
(EditText) findViewById(R.id.editText);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
47
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
}
}
activity_main.xml
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_marginTop="150dp"
android:hint="Username"
android:inputType="textEmailAddress"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="NEXT"
android:textColor="#ffffff"
android:background="@color/colorPrimaryDark"
android:layout_below="@+id/editText"/>
</RelativeLayout>
SecondActivity.java
48
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mTextView = (TextView) findViewById(R.id.textView);
}
}
activity_second.xml
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="20dp"
49
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_marginTop="10dp"
android:background="@color/colorPrimaryDark"
android:text="SUBMIT"
android:textColor="#ffffff" />
</RelativeLayout>
Observations
Input-Output
screenshots
50
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objectives:
Create an android application to familiarize implicit intent
Problem Statement
Create an android application with four buttons like web browser,make calls,show
map and choose contact
Operations are
1. By clicking on we browser button , it should open
“www.google.com” in android built in browser application
2. By Clicking on make call button, it should open the phone dialer
with a predefined number.
3. By Clicking on show map button, it should plot a specified
location co ordinates in the built in google map application.
4. By clicking on choose contact button, it should allow the user to
select a particular contact and show the details as a toast message.
Apparatus/Tools/Equipments/Components
Android studio
Principle/Theory/Flow-chart/Algorithm
Procedure
Source Code
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
51
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnBrowser = (Button) findViewById(R.id.btn_web_browser);
mBtnCall = (Button) findViewById(R.id.btn_make_calls);
mBtnMaps = (Button) findViewById(R.id.btn_show_map);
mBtnContact = (Button) findViewById(R.id.btn_choose_contact);
mBtnBrowser.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(i);
}
});
mBtnCall.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:
+651234567")); startActivity(i);
}
});
mBtnMaps.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:37.827500,-122.481670"));
startActivity(i);
}
});
mBtnContact.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
Intent i = new
Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForObservations(i, 1);
}
});
}
public void onActivityObservations(int requestCode, int ObservationsCode, Intent data)
{ if (requestCode == 1) {
if (ObservationsCode == OBSERVATIONS_OK) {
52
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
53
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Observations
Input-Output
Screenshots
54
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Aim :
To create an android application with multiple screen. Each screen should use
different layout like LinearLayout, RelativeLayout, FrameLayout and TableLayout
Objective
Create an android application to understand the different layouts in android.
Problem Statement
Create an android application with four screen.
First screen should have three buttons such as relativeLayout,TableLayout
and Frame Layout. All three should be wrapped under a LinearLayout.
1. By clicking the “RelativeLayout”, it should bring the user to the
“RelativeLayout” screen.In this screen a sample login page should
be created using RelativeLayout.
2. By clicking “Table Layout”, it should bring the user to the Table
Layout screen. Here also a login page should be designed under a
table layout.
3. By Clicking “FrameLayout”, it should bring the user to the framelayout
screen. There should be an image and a button (“Set as wallpaper”)
above that image.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
Source Code
HomeActivity.java
55
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
mBtnRelative = (Button)
findViewById(R.id.btn_relative); mBtnTable = (Button)
findViewById(R.id.btn_table); mBtnFrame = (Button)
findViewById(R.id.btn_frame);
mBtnRelative.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
}
});
mBtnTable.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
}
});
mBtnFrame.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
Intent _Intent = new
Intent(HomePage.this,FrameLayout.class);
startActivity(_Intent);
}
});
56
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
activity_home_page.xml
<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:orientation="vertical"
tools:context="com.example.vishnums.myapplication.HomePage">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Layout Example"
android:textColor="#ffffff"
android:textSize="20sp"
/>
<Button
android:id="@+id/btn_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Relative Layout"
android:layout_marginTop="100dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"/>
<Button
android:id="@+id/btn_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Table Layout"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"/>
<Button
android:id="@+id/btn_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Frame Layout"
android:layout_marginLeft="30dp"
57
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
android:layout_marginRight="30dp"/>
</LinearLayout>
RelativeLogin.java
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_relative);
}
}
activity_relative.xml
<RelativeLayout 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:orientation="vertical"
tools:context="com.example.vishnums.myapplication.HomePage">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Relative Layout"
android:textColor="#ffffff"
android:textSize="20sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="80dp"
android:ems="10"
android:hint="User Name"
58
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:background="@color/colorPrimary"
android:text="Login"
android:textColor="#ffffff" />
</RelativeLayout>
TableLogin.java
59
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
activity_table.xml
<TableLayout 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:orientation="vertical"
android:padding="20dp">
<TableRow android:layout_marginTop="60dp">
<TextView
android:layout_width="150dp"
android:text="User Name" />
<EditText android:layout_width="200dp" />
</TableRow>
<TableRow>
<TextView android:text="Password" />
<EditText android:password="true" />
</TableRow>
<TableRow>
<TextView android:text="" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remember Password" />
</TableRow>
<TableRow>
<Button android:text="Log In" />
</TableRow>
</TableLayout>
60
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
FrameLayout.java
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame);
}
activity_frame.xml
<RelativeLayout 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:orientation="vertical"
tools:context="com.example.vishnums.myapplication.HomePage">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Layout Example"
android:textColor="#ffffff"
android:textSize="20sp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView2">
61
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
<ImageView
android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:scale
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_margin="20dp" android:text="Set as
wallpaper" />
</FrameLayout>
</RelativeLayout>
Observations
Input-Output
Screenshots
62
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objective
Create an android application to familiarise listview.
Problem Statement
List should contains the former presidents. By clicking on the listview should show
the corresponding president’s name in a Toast message.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
Source Code
ListViewActivity.java
ListView mListView;
String[] presidents =
{
"Dr. Rajendra Prasad (1884-1963)",
"Dr. Sarvepalli Radhakrishnan (1888-1975)",
"Dr. Zakir Husain (1897-1969)",
"Shri Varahagiri Venkata Giri (1894-
1980)", "Dr. Fakhruddin Ali Ahmed (1905-
1977)", "Shri Neelam Sanjiva Reddy (1913-
1996)", "Giani Zail Singh (1916-1994)",
"Shri R Venkataraman (1910-2009)",
"Dr Shankar Dayal Sharma (1918-
1999)", "Shri K. R. Narayanan (1920 -
2005)", "DR. A.P.J. Abdul Kalam (1931-
2015)",
"Smt Pratibha Devisingh Patil (birth - 1934)"
};
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_items)
63
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View container, int position, long
id) {
Toast.makeText(getBaseContext(),
presidents[position], Toast.LENGTH_SHORT).show();
}
};
mListView.setOnItemClickListener(itemClickListener);
}
}
activity_list_item.xml
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="Former Presidents"
android:padding="15dp"
android:background="@color/colorPrimary"
android:textColor="#ffffff"
android:textStyle="bold"/>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3" />
</RelativeLayout>
64
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Observations
Input-Output
Screenshots
65
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objective
Create an android applcation to familiarise list view with image and text.
Problem Statement
Create an android application with custom listview with image and text. List
country image , country name and currency.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
CustomListview.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
66
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
"Sri Lanka",
"China",
"Bangladesh",
"Nepal",
"Afghanistan",
"North Korea",
"South Korea",
"Japan"
};
67
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
"Renminbi",
"Bangladeshi Taka",
"Nepalese Rupee",
"Afghani",
"North Korean
Won", "South Korean
Won", "Japanese
Yen", "Indian
Rupee", "Pakistani
Rupee", "Sri Lankan
Rupee", "Renminbi",
"Bangladeshi Taka",
"Nepalese Rupee",
"Afghani",
"North Korean
Won", "South
Korean Won",
"Japanese Yen"
};
/**
* Called when the activity is
first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Each row in the list stores country name, currency and flag
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
68
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
main.xml
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Country List"
android:textColor="#ffffff"
android:textSize="20sp" />
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
69
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
listview_layout.xml
<ImageView
android:id="@+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="img"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp" />
<TextView
android:id="@+id/cur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
70
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Observations
Input-Output
Screenshots
71
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objective
To understand the usage of Webview for integrate the webpage inside our own application.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
Source Code
WebviewActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webView.loadUrl("http://www.amazon.com");
}
}
72
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
activity_webview.xml
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Webview Example"
android:textColor="#ffffff"
android:textSize="20sp" />
<WebView
android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView2" />
</RelativeLayout>
73
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Observations
Input-Output
Screenshots
74
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Aim
To create an android application to store the details of a student permanently and display
the details in another activity using shared preference.
Objective
To understand the use of Shared Preference in android.
Problem statement
1. Create an activity to store Student Roll No,name and branch in Shared preference.
2. Display the details in the next screen.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
Source Code
DetailsInsert.java
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_main_screen);
75
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
mBtnSave.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
String rllNo = mEtRollNo.getText().toString();
String name = mEtName.getText().toString();
String branch = mEtBranch.getText().toString();
prefs = getSharedPreferences("MyPref",
MODE_PRIVATE); SharedPreferences.Editor editor =
prefs.edit(); editor.putString("rollno", rllNo);
editor.putString("name", name); editor.putString("branch",
branch); editor.commit();
}
});
}
}
activity_details_insert.xml
<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:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
76
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Student Details"
android:textColor="#ffffff"
android:textSize="20sp" />
<EditText
android:id="@+id/et_rollno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Roll No"
android:inputType="number"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Name"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/et_branch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Branch"
android:paddingLeft="10dp" />
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:text="Save and Continue"
android:textColor="#ffffff" />
android:paddingLeft="10dp" />
<Button
android:id="@+id/btn_skip"
77
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:text="Skip"
android:textColor="#ffffff" />
</LinearLayout>
DetailsView.java
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
TextView tvObservations;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_view);
tvObservations = (TextView) findViewById(R.id.tv);
prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
String rlno = prefs.getString("rollno", "no values");
String name = prefs.getString("name", "no values");
String branch = prefs.getString("branch", "no
values");
tvObservations.setText(Observations);
}
}
78
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
activity_details_view.xml
<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:orientation="vertical"
tools:context="com.example.vishnums.myapplication.DetailsView">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Student Details"
android:textColor="#ffffff"
android:textSize="20sp" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:lineSpacingExtra="15sp"
android:paddingLeft="20dp"
android:text=""
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
79
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Observations
Input-Output
Screenshots
80
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Aim
To create an android application to save data in a text file.
Objective
Create an android application to save data in a text file. Load file from memory and
show in the view.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
FileActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
* Written by Vishnu
on 01/02/16. */
public class FileActivity extends Activity {
EditText mEtText;
Button mBtnSave, mBtnLoad;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
81
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
mBtnSave.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
osw.write(message);
osw.flush();
osw.close();
Toast.LENGTH_LONG).show();
mEtText.setText("");
} catch (Exception e)
{ e.printStackTrace(
);
}
}
});
mBtnLoad.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v)
{ try {
82
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
mEtText.setText(s);
Toast.makeText(getApplicationContext(), "File loaded
successfully", Toast.LENGTH_LONG).show();
} catch (Exception e)
{ e.printStackTrace
();
}
}
});
}
}
activity_file.xml
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Save text in Files"
android:textColor="#ffffff"
android:textSize="20sp" />
83
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:paddingLeft="15dp"
android:text="Enter some text to save" />
<EditText
android:id="@+id/et_message"
android:layout_width="match_parent"
android:layout_height="100dp"
android:ems="10"
android:hint="Message to save"
android:paddingLeft="15dp" />
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:text="Save"
android:textColor="#ffffff" />
<Button
android:id="@+id/btn_load"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:text="Load"
android:textColor="#ffffff" />
</LinearLayout>
84
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Observations
Input-Output
Screenshots
85
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objective
Create an android application to understand database operations
Problem Statement
Create an android application to store user details into a database table, and display the
total no of records while inserting.
1. Create a database with name “MyDb” with version number “1”.
2. Create a table “userdetails” with field (id,username,email and password).
3. Create a screen to allow the user to enter the details .
4. Display the last inserted record’s id in a toast message.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
Source Code
DataBase.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase db) {
86
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
DBAddDetailsOne.java
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
DataBase objdatabase = new DataBase(this);
db = objdatabase.getWritableDatabase();
setContentView(R.layout.activity_db_add_one);
87
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
}
});
}
}
activity_db_add_one.xml
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Add Details"
android:textColor="#ffffff"
android:textSize="20sp" />
88
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
<EditText
android:id="@+id/id_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/id_useremail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/id_user_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:paddingLeft="10dp" />
<Button
android:id="@+id/id_btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:text="Save"
android:textColor="#ffffff" />
</LinearLayout>
89
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Observations
Screenshots
90
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objective
Create an android application to store Students details and manipulate different
database operation.
Problem statement
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
Procedure
Source Code
DataBaseStudent.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
91
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_STUDENT + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ KEY_NAME + " TEXT," + KEY_CLASS + " TEXT,"
+ KEY_ROLL_NUMBER + " TEXT," + KEY_MARK + "
FLOAT)"; db.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
StudentHomePage.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_home); btnAdd =
(Button) findViewById(R.id.btnAdd); btnView = (Button)
findViewById(R.id.btnview);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent toRegister = new Intent(StudentHomePage.this,
StudentRegister.class); startActivity(toRegister);
92
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
}
});
btnView.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View view) {
Intent toDetails = new Intent(StudentHomePage.this,
ListAllStudentActivity.class); startActivity(toDetails);
}
});
}
}
activity_student_home.xml
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Student Manager"
android:textColor="#ffffff"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="30dp">
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
93
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="@color/colorPrimary"
android:padding="20dp"
android:text="ADD STUDENT"
android:textColor="#ffffff"
android:textSize="20sp" />
<Button
android:id="@+id/btnview" style="?
android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="@color/colorPrimary"
android:padding="20dp"
android:text="VIEW DETAILS"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
StudentRegister.java
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
94
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
@Override
public void onClick(View view)
{ registerUser();
}
}
else {
ContentValues values = new ContentValues();
values.put(DataBaseStudent.KEY_NAME, _stName);
values.put(DataBaseStudent.KEY_ROLL_NUMBER, _stRollNo);
values.put(DataBaseStudent.KEY_CLASS, _stClass);
values.put(DataBaseStudent.KEY_CLASS, _stClass);
values.put(DataBaseStudent.KEY_MARK, Float.parseFloat(_stMark));
}
c.close();
}
}
95
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
activity_register.xml
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Add Details"
android:textColor="#ffffff"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:id="@+id/id_rollno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Roll"
android:inputType="number" />
<EditText
android:id="@+id/id_student_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Username"
android:inputType="textEmailAddress" />
96
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
<EditText
android:id="@+id/id_class"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Branch"
android:inputType="text" />
<EditText
android:id="@+id/id_student_mark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Score"
android:inputType="number" />
</LinearLayout>
<Button
android:id="@+id/id_btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:text="Save"
android:textColor="#ffffff" />
</LinearLayout>
ListAllStudentActivity.java
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
97
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_all_student);
mListView = (ListView) findViewById(R.id.listView);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{ HashMap<String, String> student = students.get(i);
String rollNumber = student.get("rollno");
Intent toEdit = new Intent(ListAllStudentActivity.this,
EditStudentActivity.class); toEdit.putExtra("rollNumber", rollNumber);
startActivity(toEdit);
}
});
}
@Override
protected void onResume()
{ super.onResume();
getAllDetails();
}
private void getAllDetails() {
students.clear();
DataBaseStudent objdatabase = new DataBaseStudent(this);
SQLiteDatabase db = objdatabase.getReadableDatabase();
Cursor c = db.query(DataBaseStudent.TABLE_STUDENT, null, null, null, null,
null, null);
if (c.getCount() != 0) {
c.getString(c.getColumnIndex(DataBaseStudent.KEY_ROLL_NUMBER));
_student.put("name", studentName);
students.add(_student);
}
} else {
98
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
activity_list_all_student.xml
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Students List"
android:textColor="#ffffff"
android:textSize="20sp" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"></ListView>
</LinearLayout>
99
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
EditStudentActivity.java
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
edStName = (EditText)
findViewById(R.id.id_student_name); edStRollNo =
(EditText) findViewById(R.id.id_rollno); edStClass =
(EditText) findViewById(R.id.id_class); edStMark =
(EditText) findViewById(R.id.id_student_mark); btnRegister
= (Button) findViewById(R.id.id_btn_register); btnDelte =
(Button) findViewById(R.id.id_btn_delete);
btnDelte.setOnClickListener(this);
btnRegister.setOnClickListener(this); getDetails();
100
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Float Mark;
c.moveToFirst();
Name = c.getString(c.getColumnIndex(DataBaseStudent.KEY_NAME));
Class = c.getString(c.getColumnIndex(DataBaseStudent.KEY_CLASS));
Mark = c.getFloat(c.getColumnIndex(DataBaseStudent.KEY_MARK));
edStName.setText(Name);
edStMark.setText(Mark + "");
edStClass.setText(Class);
edStRollNo.setText(rollNumber);
edStRollNo.setFocusable(false);
}
c.close();
}
@Override
public void onClick(View view)
{ if (view == btnRegister) {
registerUser();
101
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
}
}
activity_edit.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="Update Details"
android:textColor="#ffffff"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/id_rollno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Roll No"
android:inputType="textPersonName" />
<EditText
android:id="@+id/id_student_name"
android:layout_width="match_parent"
102
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/id_class"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Branch"
android:inputType="text" />
<EditText
android:id="@+id/id_student_mark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Score" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp"
android:weightSum="2">
<Button
android:id="@+id/id_btn_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:text="Update"
android:textColor="#ffffff" />
<Button
android:id="@+id/id_btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
103
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
android:text="Delete"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
Observations
Input-Output
Screenshots
104
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
105
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Sample Questions
1. Create a library database using SQLite
a. Create a screen to allow user to input book details, and store the
details to table.
b. List the inserted book details using android listview.
c. Allow user to view the complete details by selecting the item in the list
d. Allow the user to edit or remove the details
106
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objective
Create an android application to integrate html using webview.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flow-chart/Algorithm
html,javascript,css and other asset files are included in the “assets” folder
in the project.
The path to the assets folder should be:<project>/src/main/assets
107
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Procedure
helloworld.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<b>Helllo World</b>
<br>
<br>
Welcome to html5 application development
</body>
</html>
WebviewSample.java
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview_sample);
108
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Objective
Create an android application using html5 for storing and displaying the Biodata of
the user.
Problem statement
1. Create a home screen with two options “Add Details “ and “View Details”.
2. Add the following input fields in the add details page first
name, last name,email,date of birth dnd blood group.
3. Save all the details using html5 local storage technology.
4. Read all details from local storage and display in ‘View Details’ screen.
Apparatus/Tools/Equipments/Components
Android Studio
Principle/Theory/Flowchart/Algorithm
Procedure
Source Code
WebviewMain.java
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings; import
android.webkit.WebView;
{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview_sample);
109
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
webSettings.setBuiltInZoomControls(false);//Disable zoom
webView.setWebChromeClient(new WebChromeClient());//To enable the alert
webView.loadUrl("file:///android_asset/index.html");//Set the initial html file
}
}
activity_webview_sample.xml
<?xml version="1.0" encoding="utf 8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView android:id="@+id/webView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
index.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="script.js"
></script> </head>
<body bgcolor="#F39B9B" style="text align:center;margin top:50px">
<div id="main_screen" style="margin top:40%">
<b>DIGITAL BIO DATA</b>
</br>
</br>
<button style="width:100px;70px"
onclick="openRegister()">ADD </br>DETAILS</button>
<button style="width:100px;70px" onclick="showHomepage()">VIEW
</br>DETAILS</button>
</div>
email = document.getElementById("email").value; var dob =
document.getElementById("dob").value;
var blood = document.getElementById("blood").value;
localStorage.setItem("fname",fname);
localStorage.setItem("lname",lname);
110
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
localStorage.setItem("email",email);
localStorage.setItem("dob",dob);
localStorage.setItem("blood",blood);
closeRegistration();
alert("Details added Successfull")
}
function showHomepage() {
var div =
document.getElementById('main_screen');
div.style.display = 'none';
var div =
document.getElementById('home_screen');
div.style.display = 'block';
div.innerHTML = "<br></br><b>DIGITAL BIO DATA</b>"
+"</br></br><b>First Name :</b> "+localStorage.getItem("fname")
+"</br><b>Last Name : </b>"+localStorage.getItem("fname")
+"</br><b>Email : </b>"+localStorage.getItem("email")
+"</br><b>Date of Birth : </b>"+localStorage.getItem("dob")
+"</br><b>Blood Group : </b>"+localStorage.getItem("blood");
}
Observations
Input-Output
Screenshots
111
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Sample Questions
1. Create an android application using html5 for storing and dispalying Employee details
2. Create an android application using html5 for storing and displaying
customers details of a telephone company
112
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
Chapter-7
Installing an application on a mobile device
To sign your app in release mode in Android Studio, follow these steps:
2. On the Generate Signed APK Wizard window, click Create new to create
a new keystore.
4. On the New Key Store window, provide the required information as shown
in figure 1.
5. Your key should be valid for at least 25 years, so you can sign app
updates with the same key through the lifespan of your app.
113
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
key, and enter the passwords for both. Then click Next.
114
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
7. On the next window, select a destination for the signed APK and click Finish.
115
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
General Outcomes:
1. To install software tools required for mobile technologies
2. To develop simple android applications
3. To design and implement user interfaces
4. To develop applications using data storage
5. To develop mobile applications using HTML 5.0 and JavaScript
Specific Outcomes:
1.1 Install Android studio
1.2 Install android sdk
1.3 Configure Android Studio for android
1.4 Create android virtual device(Emulator)
116
REVISION 2015 [SMART DEVICE PROGEAMMING LAB-6138]
References
1. http://developer.android.com/training/index.html
117