Professional Documents
Culture Documents
Android Tutorial
Android Tutorial
Lab Manual
LAB MANUAL
Android Lab
CSE-VII sem
CSE 7th Ssm
Andorid Lab Manual
Experiment 1:
CSE 7th Ssm
Andorid Lab Manual
Experiment 1:
Let's see the list of software required to setup android for eclipse IDE manually.
For creating android application, JDK must be installed if you are developing the android
application with Java language. download the JDK
For developing the android application using eclipse IDE, you need to install the Eclipse. you
can download it from this location download the Eclipse. Eclipse classic version is
recommended but we are using the Eclipse IDE for JavaEE Developers.
First of all, download the android SDK. In this example we have installed the android SDK for
windows (.exe version). Now double click on the exe file, it will be installed. I am using the
android 2.2 version here.
ADT (Android Development Tools) is required for developing the android application in the
eclipse IDE. It is the plugin for Eclipse IDE that is designed to provide the integrated
environment.
CSE 7th Ssm
Andorid Lab Manual
1) Start the eclipse IDE, then select Help > Install new software...
5) click finish
After the installing ADT plugin, now tell the eclipse IDE for your android SDK location. To do so:
CSE 7th Ssm
Andorid Lab Manual
2. Now select the android from the left panel. Here you may see a dialog box asking if you
want to send the statistics to the google. Click proceed.
3. Click on the browse button and locate your SDK directory e.g. my SDK location is
C:\Program Files\Android\android-sdk .
4. Click the apply button then OK.
For running the android application in the Android Emulator, you need to create and AVD. For
creating the AVD:
CSE 7th Ssm
Andorid Lab Manual
Experiment 2:
CSE 7th Ssm
Andorid Lab Manual
CSE 7th Ssm
Andorid Lab Manual
Now an android project have been created. You can explore the android project and see the
simple program, it looks like this:
For writing the message we are using the TextView class. Change the onCreate method as:
package com.example.helloandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
CSE 7th Ssm
Andorid Lab Manual
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview=new TextView(this);
textview.setText("Hello Android!");
setContentView(textview);
}
@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;
}
}
To run the android application: Right click on your project > Run As.. > Android Application
The android emulator might take 2 or 3 minutes to boot. So please have patience. After booting
the emulator, the eclipse plugin installs the application and launches the activity. You will see
something like this:
CSE 7th Ssm
Andorid Lab Manual
Experiment 3:
XML Files
a. AndroidManifest.xml
i. Edit the manifest and change min sdk and target
sdk of application.
ii. Add main activity entries in manifest.
iii. Add second activity entries in manifest.
iv. Add Entries for Service, Broadcast receivers.
v. Add uses permissions for reading files, internet,
camera.
CSE 7th Ssm
Andorid Lab Manual
This is the required xml file for all the android application and located inside the root directory.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javatpoint.hello"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
CSE 7th Ssm
Andorid Lab Manual
</application>
</manifest>
The elements used in the above xml file are described below.
<manifest>
manifest is the root element of the AndroidManifest.xml file. It has package attribute that
describes the package name of the activity class.
<application>
application is the subelement of the manifest. It includes the namespace declaration. This
element contains several subelements that declares the application component such as activity
etc. The commonly used attributes are of this element are icon, label, theme etc.
android:icon represents the icon for all the android application components.
android:label works as the default label for all the application components.
<activity>
activity is the subelement of application and represents an activity that must be defined in the
AndroidManifest.xml file. It has many attributes such as label, name, theme, launchMode etc.
<intent-filter>
intent-filter is the sub-element of activity that describes the type of intent to which activity,
service or broadcast receiver can respond to.
CSE 7th Ssm
Andorid Lab Manual
<action>
It adds an action for the intent-filter. The intent-filter must have at least one action element.
<category>
CSE 7th Ssm
Andorid Lab Manual
Layouts
i. Create Linear Layout in xml
ii. Create Relative Layout in xml
iii. Create frame layout in xml
iv. Create a complex mixed layout using all above
layouts
CSE 7th Ssm
Andorid Lab Manual
Android allows you to create view layouts using simple XML file (we can also create a layout
using java code). All the layouts must be placed in /re s / l a y o u t folder.
1. Linear Layout
In a linear layout, like the name suggests, all the elements are displayed in a linear
fashion(below is an example of the linear layouts), either H o r iz o n t a l l y or V e r t i c a l l y and
this behavior is set in a n d r o id : o r ie n t a t i o n which is an attribute of the node LinearLayout.
Example of Vertical layout snippet
CSE 7th Ssm
Andorid Lab Manual
Now that we know the two types of linear layouts, here are the steps you need to follow to
create them
CSE 7th Ssm
Andorid Lab Manual
</LinearLayout>
</LinearLayout>
4. To set this newly created view as the initial view of your app, Open your MainActivity.java file.
You would see the following line inside
the o n C r e a t e function s e tC o n t e n t V iew ( R .la y o u t . m a in ) .
Change R . l a yo u t . m a in toR . la y o u t . y o u r l i n e a r v i ew n a m e . In my case
its R . l a y o u t . l i n e a r _ la y o u t
package com.example.androidlayouts;
import android.app.Activity;
import android.os.Bundle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear_layout);
}
}
5. To run the application, right click on the project -> Run As -> 1. Android Application. You
should see your newly created linear layout in the emulator.
CSE 7th Ssm
Andorid Lab Manual
2. Relative Layout
In a relative layout every element arranges itself relative to other elements or a parent element.
As an example, lets consider the layout defined below. The “C a n c e l” button is placed
relatively, to the r ig h t o f the “L o g in ” button p a ra l le l y . Here is the code snippet that
achieves the mentioned alignment (Right of Login button parallely)
Example code snippet
<Button android:layout_toRightOf="@id/btnLogin"
android:layout_alignTop="@id/btnLogin" ..></Button>
CSE 7th Ssm
Andorid Lab Manual
CSE 7th Ssm
Andorid Lab Manual
4. Same like before open your MainActivity.java file and set the layout to your newly created
relative layout file. In my case its R . l a yo u t .r e l a t i v e _ la y o u t
setContentView(R.layout.relative_layout);
5. To run the application, right click on the project -> Run As -> 1. Android Application. You
should see your newly created relative layout in the emulator.
3. Table Layout
Table layouts in Android works in the same way HTML table layouts work. You can divide your
layouts into r ow sand c o lu m n s . Its very easy to understand. The image below should give
you an idea.
CSE 7th Ssm
Andorid Lab Manual
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*" android:stretchColumns="*" android:background="#ffffff">
<!-- Row 1 with single column -->
<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="match_parent" android:layout_height="wrap_content"
android:textSize="18dp" android:text="Row 1" android:layout_span="3"
android:padding="18dip" android:background="#b0b0b0"
android:textColor="#000"/>
</TableRow>
CSE 7th Ssm
Andorid Lab Manual
<!-- Row 2 with 3 columns -->
<TableRow
android:id="@+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/TextView04" android:text="Row 2 column 1"
android:layout_weight="1" android:background="#dcdcdc"
android:textColor="#000000"
android:padding="20dip" android:gravity="center"/>
<TextView
android:id="@+id/TextView04" android:text="Row 2 column 2"
android:layout_weight="1" android:background="#d3d3d3"
android:textColor="#000000"
android:padding="20dip" android:gravity="center"/>
<TextView
android:id="@+id/TextView04" android:text="Row 2 column 3"
android:layout_weight="1" android:background="#cac9c9"
android:textColor="#000000"
android:padding="20dip" android:gravity="center"/>
</TableRow>
<TextView
android:id="@+id/TextView04" android:text="Row 3 column 2"
android:layout_weight="1" android:background="#a09f9f"
android:textColor="#000000"
android:padding="20dip" android:gravity="center"/>
</TableRow>
</TableLayout>
4. Same like before open your MainActivity.java file and set the layout to your newly created
table layout file. In my case its R . l a y o u t .t a b l e _ la y o u t
setContentView(R.layout.table_layout);
CSE 7th Ssm
Andorid Lab Manual
5. To run the application, right click on the project -> Run As -> 1. Android Application. You
should see your newly created table layout in the emulator.
CSE 7th Ssm
Andorid Lab Manual
c. Drawables
i. Create xml drawable for rectangular, oval and other
basic shapes
ii. Create xml drawable with Layer list for complex
shapes.
CSE 7th Ssm
Andorid Lab Manual
There are at least 17 types of drawables but there are five that are most important to
understand:
1. Shape Drawables - Defines a shape with properties such as stroke, fill, and padding
2. StateList Drawables - Defines a list of drawables to use for different states
3. LayerList Drawables - Defines a list of drawables group together into a composite
result
4. NinePatch Drawables - A PNG file with stretchable regions to allow proper resizing
5. Vector Drawables - Defines complex XML-based vector images
Let's explore these drawable file types one by one and take a look at examples of usage.
Drawing Shapes
The Shape Drawable is an XML file that defines a geometric shape, including colors and
gradients. This is used to create a complex shape that can then be attached as the background
of a layout or a view on screen. For example, you can use a shape drawable to change the
shape, border, and gradient of a button background.
A shape is simply a collection of properties that are combined to describe a background. The
shape can be described with properties such as corners for rounding, gradient for
backgrounds, padding for spacing, solid for background colors, andstroke for border.
CSE 7th Ssm
Andorid Lab Manual
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/solid_color_shape"
android:textColor="#ffffff"
android:text="@string/hello_world" />
Note that drawables can be applied to any view and are usually set with
the background property referencing the drawable resource.
CSE 7th Ssm
Andorid Lab Manual
Using solid color shapes and gradients we can customize the appearance of buttons, layouts
and other views without requiring the use of any images. Note that custom shapes can be
created at runtime using other shape drawable types using PathShape andArcShape.
State List
A StateListDrawable is a drawable object defined in XML that uses a several different images to
represent the same graphic, depending on the state of the object. For example, a Button widget
can exist in one of several different states (pressed, focused, or neither) and, using a state list
drawable, you can provide a different background image for each state. The state list supports
different view states such
CSE 7th Ssm
Andorid Lab Manual
Now, when the view (i.e button) is pressed or focused, the drawable used for the view will
change accordingly. Note that any view can have a state selector, but the most common uses
are with buttons and listview items. There are also color state selectors which allow a color to be
chosen based on view state such as in a file named res/color/button_text.xml:
and applied to any field that accepts a color value such as the textColor property of a button in a
layout file:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />
Using state lists allows us to easily define dynamic views that respond to pressed, checked,
enabled or other related states.
CSE 7th Ssm
Andorid Lab Manual
Creating Vector Drawables
To create a vector image, you need to define pathData syntax which is located here. This
example defines the details of the shape inside a XML element such
as res/drawable/ic_heart.xml with:
Customizing a Button
Let's take a look at an end-to-end example of customizing a button using drawables to govern
the styling and the pressed states.
Creating a custom button require combining at least a state list drawable with a shape drawable.
First, let's create our shape drawable which represents the "default" button background
in res/drawable/nice_button_enabled.xml:
CSE 7th Ssm
Andorid Lab Manual
Let's also create a style (set of view properties) that includes setting the background
in res/values/styles.xml:
This now represents the shape and background of the button in the default state along with all
the other properties. We could apply this by setting the style of the button:
<Button
android:id="@+id/btnGo"
style="@style/NiceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
CSE 7th Ssm
Andorid Lab Manual
Customizing a ListView
Another common task is customizing the appearance of items in a ListView. First let's create the
basic ListView and populate String items inside. First, the layout XML for the item
in res/layout/item_simple.xml:
<ListView
android:id="@+id/lvTest"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
CSE 7th Ssm
Andorid Lab Manual
Now, let's add our own styling to the ListView. Let's add a default gradient and a pressed
gradient, change the divider color between items and add a border around the ListView. First,
let's add the shape gradient background for the default state inres/drawable/gradient_bg.xml:
CSE 7th Ssm
Andorid Lab Manual
android:shape="rectangle">
<gradient
android:startColor="#C1E1A6"
android:endColor="#118C4E"
android:angle="270" />
</shape>
and then let's create a state list which describes the drawables to use in various list states
inres/drawable/states_selector_list.xml:
<item android:state_pressed="true"
android:drawable="@drawable/gradient_pressed_bg" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/gradient_pressed_bg" />
</selector>
Next, let's setup the border for our ListView using a Shape drawable
in res/drawable/list_border.xml by setting the "stroke" property:
Let's now apply each of these XML drawables to the various elements. First, let's add the
background to the list item itself and tweak res/layout/item_simple.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
...
android:background="@drawable/states_selector_list" />
Notice that the background property has been set to the states list in order to apply the default
background for the item. Next, let's add the border and the selector states to the existing
ListView in the activity layout file:
CSE 7th Ssm
Andorid Lab Manual
<ListView
...
android:padding="1dp"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:background="@drawable/list_border"
android:listSelector="@drawable/states_selector_list" >
</ListView>
Here we have customized the divider color and dividerHeight as well as the background to apply
the border andlistSelector to manage the states when an item is pressed. With all this in place,
our customized ListView now looks like:
We've now successfully customized the appearance of our ListView and it's items using a series
of drawables. You can use these techniques to make a list look however you want based on the
needs for your app.
CSE 7th Ssm
Andorid Lab Manual
d. Values
A single string that can be referenced from the application or from other resource files (such as
an XML layout).
FILE LOCATION:
CSE 7th Ssm
Andorid Lab Manual
res/values/filename.xml
The filename is arbitrary. The <string> element's name will be used as the resource ID.
RESOURCE REFERENCE:
In Java: R.string.string_name
In XML:@string/string_name
SYNTAX:
ELEMENTS:
<resources>
No attributes.
<string>
A string, which can include styling tags. Beware that you must escape apostrophes and
quotation marks. For more information about how to properly style and format your
strings see Formatting and Styling, below.
attributes:
name
String. A name for the string. This name will be used as the resource ID.
CSE 7th Ssm
Andorid Lab Manual
EXAMPLE:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
You can use either getString(int) or getText(int) to retrieve a string. getText(int) will retain
any rich text styling applied to the string
CSE 7th Ssm
Andorid Lab Manual
Experimeny No. 4
Creating User Interface
a.Create application with Basic Views (Textview,
Button, ListView)
b. Create application with different Layouts (Linear,
Relative, Frame)
c.Create application to handle and respond on click
using Click Listeners
For doing this, first make a string array in a string file, this file is automatically generated in
the value folder in the res folder as in the following:
CSE 7th Ssm
Andorid Lab Manual
<string-array name="listdata">
<item>item 1</item>
<item>item 2</item>
<item>item 3</item>
<item>item 4</item>
<item>item 5</item>
<item>item 6</item>
<item>item 7</item>
<item>item 8</item>
<item>item 9</item>
<item>item 10</item>
<item>item 11</item>
<item>item 12</item>
<item>item 13</item>
<item>item 14</item>
<item>item 15</item>
<item>item 16</item>
<item>item 17</item>
<item>item 17</item>
CSE 7th Ssm
Andorid Lab Manual
<item>item 18</item>
<item>item 19</item>
<item>item 20</item>
</string-array>
Then make a layout for the activity with the code below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
Then make the child layout for the list, the code is below:
<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"
CSE 7th Ssm
Andorid Lab Manual
tools:context="com.example.articalonlistiner.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/childTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/childButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
</RelativeLayout>
CSE 7th Ssm
Andorid Lab Manual
ListAdapter adapter;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataItems.addAll(dataTemp);
adapter.setCustomButtonListner(MainActivity.this);
listView.setAdapter(adapter);
CSE 7th Ssm
Andorid Lab Manual
}
This code shows the error because you have not made the ListAdapter.java file in the src
folder. Please make that file and extend it with ArrayAdapter<String>.
this.data = dataItem;
this.context = context;
Now make an interface in ListAdapter, this interface works like a listener for the button.
customButtonListener customListner;
And make a method to set the instance of the activity that has a ListView.
this.customListner = listener;
This method shares the context of the MainActivity with the interface to use this.
TextView text;
Button button;
CSE 7th Ssm
Andorid Lab Manual
}
If you have more items in the listItem view then add all the reference in the ViewHolder class.
@Override
ViewHolder viewHolder;
if (convertView == null) {
.findViewById(R.id.childTextView);
.findViewById(R.id.childButton);
convertView.setTag(viewHolder);
} else {
viewHolder.text.setText(temp);
viewHolder.button.setOnClickListener(new OnClickListener() {
@Override
if (customListner != null) {
customListner.onButtonClickListner(position,temp);
CSE 7th Ssm
Andorid Lab Manual
}
});
return convertView;
Note: Here the yellow marked function is new for you. This is the main function to provide a
listener for the button of each item of the list view. This function returns a callback
forMainActivity because the MainActivity shares our context with the listener.
customButtonListener
After implementing, this interface overrides the method. The method gives you the position of
the list adapter.
@Override
Toast.LENGTH_SHORT).show();
CSE 7th Ssm
Andorid Lab Manual
You can implement your method, whatever you want to do with the button.
This article is over now. The following is the entire code of the MainActivity and List Adapter.
MainActivity
package com.example.articalonlistiner;
CSE 7th Ssm
Andorid Lab Manual
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.example.articalonlistiner.ListAdapter.customButtonListener;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import android.widget.Toast;
customButtonListener {
ListAdapter adapter;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataItems.addAll(dataTemp);
adapter.setCustomButtonListner(MainActivity.this);
listView.setAdapter(adapter);
CSE 7th Ssm
Andorid Lab Manual
@Override
Toast.LENGTH_SHORT).show();
ListAdapter
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
customButtonListener customListner;
CSE 7th Ssm
Andorid Lab Manual
public void setCustomButtonListner(customButtonListener listener) {
this.customListner = listener;
this.data = dataItem;
this.context = context;
@Override
ViewHolder viewHolder;
if (convertView == null) {
.findViewById(R.id.childTextView);
.findViewById(R.id.childButton);
convertView.setTag(viewHolder);
} else {
CSE 7th Ssm
Andorid Lab Manual
viewHolder.text.setText(temp);
viewHolder.button.setOnClickListener(new OnClickListener() {
@Override
if (customListner != null) {
customListner.onButtonClickListner(position,temp);
});
return convertView;
TextView text;
Button button;
CSE 7th Ssm
Andorid Lab Manual
Experiment No. 5
Assets and Images
a. Create application which will access files from Assets
folder (Images, sounds, Custom Fonts)
CSE 7th Ssm
Andorid Lab Manual
First, create a project as usual, then put files into ‘asset‘ like below:
Files in 'Assets'
Now, create a simple layout containing a TextView for displaying the content of ‘text.txt‘ and
an ImageView for displaying the image ‘avatar.jpg’, which are put in Asset.
The implementation quite easy using AssetManager as mentioned above.
package pete.android.study;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
ImageView mImage;
TextView mText;
@Override
public void onCreate(Bundle savedInstanceState) {
CSE 7th Ssm
Andorid Lab Manual
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImage = (ImageView)findViewById(R.id.image);
mText = (TextView)findViewById(R.id.text);
loadDataFromAsset();
}
// load image
try {
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
CSE 7th Ssm
Andorid Lab Manual
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
return;
}
}
}
CSE 7th Ssm