Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
LABORATORY MANUAL
1|Page
CHADALAWADA RAMANAMMA ENGINEERING COLLEGE
(AUTONOMOUS)
Chadalawada Nagar, Renigunta Road, Tirupati – 517 506
INDEX
S. No Name of the Experiment Page No
1 Setting Up the Development Environment 3-12
2 Create "Hello World" Application 13-22
Create Application by Using Widgets.
3 23-25
Creating the Application by using the Activity class
4 Creating the Application by using Tex Edit control 26-27
5 Creating the Application Choosing Options CheckBox 28-29
6 Creating the Application Choosing Options RadioButton. 30-31
7 Creating the Application Choosing Options RadioGroup 32-34
8 Creating the Application Choosing Options Spinner 35-36
Create Application by Using Building Blocks for Android Application
9 37-38
Design by using Linear Layout
Create Application by Using Building Blocks for Android Application
10 39-40
Design by using Relative Layout.
Create Application by Using Building Blocks for Android Application
11 41-42
Design by using Absolute Layout.
12 Create the Application to play the Audio and Video clips 43-46
13 Create Application by Using Building Menus and Storing Data. 47-49
14 Design the Application for Menus and Action Bar. 50-52
15 Design the application to display the Drop-Down List Action Bar. 53-54
2|Page
PROGRAM – 1
SETTING UP THE DEVELOPMENT ENVIRONMENT
Environment Used
Windows 7/8/10 (32- or 64-bit)
JDK 6 (Java SE 6). To install JDK, refer these links; in Windows, Ubuntu
o android-sdk_r18
Environment Used:
Eclipse Indigo (3.7.1) IDE.
Java SE (JDK/JRE) 6.
If you need to install Eclipse, you can download it from this location:
http://www.eclipse.org/downloads/
The latest version of Eclipse IDE for Java EE Developers is recommended. Choose 32-bit or 64-bit
depending on your OS version.
For Windows: The file extension will be .zip
For Linux: The file extension will be .tar.gz
Installing Eclipse
To install Eclipse, all you do is extract the zip (or tar.gz) file in the desired directory. No further
work is required. When you unzip the file, it creates a subdirectory called “eclipse”, with multiple
subdirectories under that. So, for example, in Windows you could unpack the zip file in the root
directory (e.g., C:\) and Eclipse would be installed in C:\eclipse.
3|Page
Launching Eclipse
Eclipse Application
In the eclipse folder you will find the eclipse.exe application (a big blue icon). You can double
click on this to launch Eclipse IDE. We recommend you to create a shortcut on the desktop to simplify
the launching of Eclipse.
Launch Eclipse
Double-click the “eclipse.exe” file you installed OR the shortcut you may have created.
Select workspace
After launching, Eclipse will ask you to specify the workspace to use. The workspace is a folder
used by eclipse to keep all your projects. Select the folder where you want the workspace to be created
or accept the default provided by Eclipse.
4|Page
Finish
Press OK to continue.
Welcome Screen
To start working on Eclipse, you can either click on the curved arrow (Workbench) on the top right
most corner or close the Welcome tab and you will see the following window.
Environment Used
Java SE 6 Update 29
Windows (7/8/10) OS
5|Page
Download the JDK suitable for your system requirement (Windows x86 for 32bit windows or
Windows x64 for 64bit windows) to a safe location on your machine.
Installing Java Development Kit (JDK):
Step 1:
Double click the downloaded JDK exe file. Click the “Next” button.
6|Page
Step 2:
The “Custom Setup” window allows you to select which parts of the JDK you want to install. If
you want to change the installation directory click on change and specify the location where jdk will be
installed. It is recommended to stick with the default settings. Click the “Next” button.
Step 3:
JDK “installing” window appears. It will take a few minutes to install the program features of the
JDK.
Step 4:
By installing JDK, JRE will also be installed. Select Java Runtime Environment (JRE) installation
location.
7|Page
Step 5:
When the “Complete” screen appears, click on the “Finish” button. Now you have successfully
installed the Java Development Kit!
8|Page
For Windows 7/8/10:
1. Click on Start -> Right click Computer -> Select Properties
2. Click on “Advanced system settings” from left side Tasks menu.
9|Page
1. Click OK -> OK -> OK. Now you have successfully configured JDK and ready to develop
applications in Java.
2. Now open Command prompt and type “javac -version” and it should show the version number
which you installed.
javac is the primary Java compiler, included in the Java Development Kit (JDK).
Also type “java -version” which is Java runtime included in JDK. Sometimes you may want to
restart the computer for the PATH settings to take effect.
Download and Install SDK Starter Package
Get the latest version of the SDK starter package from the link given below,
http://developer.android.com/sdk/index.html
The SDK starter package is not a full development environment – it includes only the core SDK
Tools. You can use Android SDK Manager, part of SDK tools to download the rest of the SDK
packages (such as the latest Android platform).
10 | P a g e
Using Windows Installer (.exe file)
If you downloaded the Windows installer (.exe file), run it to install the SDK Tools into a default
location (For simplicity and easy access, I have used C:\Android\android-sdk).
Installing Android platform
When you complete the installation wizard, it will launch the Android SDK Manager with a
default set of platforms and other packages selected for you to install. Simply click Install to accept
the recommended set of packages and install them.
This might take sometime to install the selected platforms and packages.
Using .zip or .tgz file
If you downloaded a .zip or .tgz (for Linux) package, extract it to a safe location on your machine,
for example, C:\Android\android-sdk.
After extracting the files, open SDK Manager to install the recommended platform and other
packages as shown in the above step.
Install ADT plugin for Eclipse
ADT plugin for Eclipse IDE lets you quickly set up new Android projects, create an application
UI, debug your applications using the Android SDK tools, and even export signed (or unsigned) APKs
in order to distribute your application.
Open Eclipse IDE
Select Help -> Install New Software…
Click Add, in the top-right corner.
In the Add Repository dialog that appears, enter the name as “ADT Plugin” and the Location as
https://dl-ssl.google.com/android/eclipse/
11 | P a g e
Click OK.
In the Available Software dialog, select the checkbox next to Developer Tools and click Next.
In the next window, you’ll see a list of the tools to be downloaded. Click Next.
Read and accept the license agreements, then click Finish.
When the installation completes, restart Eclipse.
Configuring the ADT Plugin in Eclipse
After installing the ADT plugin, you have to define the location of the Android SDK in Android
preferences in Eclipse:
Select Window -> Preferences… to open the Preferences panel.
Select Android from the left panel.
o You may see a dialog asking whether you want to send usage statistics to Google. If so,
make your choice and click Proceed. You cannot continue with this procedure until you
click Proceed.
For the SDK Location in the main panel, click Browse… and locate your downloaded SDK
directory. In my case, it is C:\Android\android-sdk
Click Apply, then OK.
Adding Platforms and Other Packages
.You can launch the Android SDK Manager in one of the following ways:
From within Eclipse, select Window -> Android SDK Manager.On Windows, double-click the
SDK Manager.exe file available at installed directory. On Mac or Linux, open a terminal and
navigate to the tools/ directory in the Android SDK, then execute:
android.On Mac or Linux, open a terminal and navigate to the tools/ directory in the Android
SDK,then execute: android.To download and install packages, use the graphical UI of the Android
SDK Manager to browse the SDK repository and select new or updated packages.
12 | P a g e
PROGRAM – 2
2. Create “Hello World“ Application ,Create a new Android Project, Run “Hello World “ on the
Emulator, on a Physical Device and Greeting the user.
Select Android Application Project
Go to File and then click on New.
In New, Click on Android Application Project .
13 | P a g e
Creating new Android Application Project :
Application name – Refers to the name of the application you want to give .
Project name – Refers to the Project name you want to give to your application .
Package name – Should be in a A.B format for ex: myapp.magnetbrains.co
Min. req. SDK – From which version you want to have access with. ex: Android 2.2 or above
Targeted SDK – Which particular version you are targeting. ex: Android 4.4 or above
Compile with – From which SDK you want to compile with
Theme – Any theme of your choice.
14 | P a g e
This icon will be representing your application later when we use it. Then click on NEXT.
4. Creating Activity:
Select any activity for ex : Blank activity. Blank activity refers to window that we will see when the
application will be launched.
15 | P a g e
Then you need to specify about your blank activity and then click on Finish.
In the package explore your app and its related file will be displayed. in that src/MainActivity.java and
layout/activity_main.xml are important files for performing and design the your application.
activity_main.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
16 | P a g e
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"/>
</RelativeLayout>
MainActivity.java
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
returntrue;
}
17 | P a g e
and finally click on OK button
18 | P a g e
Running the application
19 | P a g e
Double click on Android Application thenchange new_configuration to your app name --browse
your app nameselect the launch radio buttonclick on “apply”
20 | P a g e
Selecting AVD to initiate your application
Give AVD Name as like HelloDemo->select the device typetarget as Android 4.2 API Level17
press “OK” button.-->click refresh refresh->Click on “Run”
21 | P a g e
Creating Android virtual Device to run an application
Output:
22 | P a g e
PROGRAM – 3
Aim: Creating the Application by using the Activity class Life Cycle
Source code:
activity_main.xml
<RelativeLayoutxmlns: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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
MainActivity.java
packagecom.example.lifecycle;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;
importandroid.util.Log;
@Override
23 | P a g e
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onStart();
super.onResume();
super.onPause();
super.onStop();
24 | P a g e
super.onDestroy();
@Override
publicbooleanonCreateOptionsMenu(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;
Input&Output:
25 | P a g e
PROGRAM – 4
Source code:
activity_edit_text_app.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:id="@+id/user_name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/response"/>
</LinearLayout>
EditTextAppActivity.java:
package com.androidunleashed.edittextapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.KeyEvent;
public class EditTextAppActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text_app);
final TextView resp =(TextView)this.findViewById(R.id.response);
final EditText username = (EditText)
findViewById(R.id.user_name);
username.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
26 | P a g e
if ((event.getAction() == KeyEvent.ACTION_UP) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
resp.setText("Welcome "+username.getText()+"!");
return true;
}
return false;
}
});
}
}
Output:
27 | P a g e
PROGRAM – 5
Source code:
activity_check_box_app.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Items you want"/>
<CheckBox
android:id="@+id/checkbox_pizza"
android:layout_height="wrap_content"
android:text="Pizza $15"
android:layout_width="match_parent" />
<CheckBox
android:id="@+id/checkbox_hotdog"
android:layout_height="wrap_content"
android:text="Hot Dog $5"
android:layout_width="match_parent" />
<CheckBox
android:id="@+id/checkbox_burger"
android:layout_height="wrap_content"
android:text="Burger $10"
android:layout_width="match_parent" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bill_btn"
android:text="Calculate Bill"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/amount"/>
</LinearLayout>
CheckBoxAppActivity.java
package com.androidunleashed.checkboxapp;
28 | P a g e
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.CheckBox;
import android.view.View;
import android.view.View.OnClickListener;
public class CheckBoxAppActivity extends Activity implements
OnClickListener {
CheckBox c1,c2,c3;
TextView resp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box_app);
Button b = (Button)this.findViewById(R.id.bill_btn);
resp = (TextView)this.findViewById(R.id.amount);
c1 = (CheckBox)findViewById(R.id.checkbox_pizza);
c2 = (CheckBox)findViewById(R.id.checkbox_hotdog);
c3 = (CheckBox)findViewById(R.id.checkbox_burger);
b.setOnClickListener(this);
}
public void onClick(View v) {
int amt=0;
if (c1.isChecked()) {
amt=amt+15;
}
if (c2.isChecked()) {
amt=amt+5;
}
if (c3.isChecked()) {
amt=amt+10;
}
resp.setText("Bill is " +Integer.toString(amt));
}
}
Output:
29 | P a g e
PROGRAM – 6
Source code:
activity_radio_button_app.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select the type of hotel"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_fivestar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Five Star " />
<RadioButton android:id="@+id/radio_threestar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three Star" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hoteltype"/>
</LinearLayout>
RadioButtonAppActivity.java
package com.androidunleashed.radiobuttonapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.RadioButton;
30 | P a g e
import android.view.View;
import android.view.View.OnClickListener;
public class RadioButtonAppActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button_app);
RadioButton radioFivestar = (RadioButton)
findViewById(R.id.radio_fivestar);
RadioButton radioThreestar = (RadioButton)
findViewById(R.id.radio_threestar);
radioFivestar.setOnClickListener(radioListener);
radioThreestar.setOnClickListener(radioListener);
}
private OnClickListener radioListener = new
OnClickListener() {
public void onClick(View v) {
TextView selectedHotel = (TextView)
findViewById(R.id.hoteltype);
RadioButton rb = (RadioButton) v;
selectedHotel.setText("The hotel type selected is: "
+rb.getText());
}
};
Output:
31 | P a g e
PROGRAM – 7
Source code:
activity_radio_button_app.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select the type of hotel"/>
<RadioGroup android:id="@+id/group_hotel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_fivestar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Five Star " />
<RadioButton android:id="@+id/radio_threestar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three Star" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select the type of room"/>
<RadioGroup android:id="@+id/group_room"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_suite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grand Suite " />
<RadioButton android:id="@+id/radio_luxury"
32 | P a g e
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Luxury Room" />
<RadioButton android:id="@+id/radio_ordinary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ordinary Room" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hoteltype" />
</LinearLayout>
RadioButtonAppActivity.java
package com.androidunleashed.radiobuttonapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.RadioButton;
import android.view.View;
import android.view.View.OnClickListener;
public class RadioButtonAppActivity extends Activity {
String str1="";
String str2="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button_app);
RadioButton radioFivestar = (RadioButton)
findViewById(R.id.radio_fivestar);
RadioButton radioThreestar = (RadioButton)
findViewById(R.id.radio_threestar);
RadioButton radioSuite = (RadioButton)
findViewById(R.id.radio_suite);
RadioButton radioLuxury = (RadioButton)
findViewById(R.id.radio_luxury);
RadioButton radioOrdinary = (RadioButton)
findViewById(R.id.radio_ordinary);
radioFivestar.setOnClickListener(radioListener1);
radioThreestar.setOnClickListener(radioListener1);
33 | P a g e
radioSuite.setOnClickListener(radioListener2);
radioLuxury.setOnClickListener(radioListener2);
radioOrdinary.setOnClickListener(radioListener2);
}
private OnClickListener radioListener1 = new OnClickListener() {
public void onClick(View v) {
TextView selectedOptions = (TextView)
findViewById(R.id.hoteltype);
RadioButton rb = (RadioButton) v;
str1="The hotel type selected is: " +rb.getText();
selectedOptions.setText(str1+"\n"+str2);
}
};
private OnClickListener radioListener2 = new
OnClickListener() {
public void onClick(View v) {
TextView selectedOptions = (TextView)
findViewById(R.id.hoteltype);
RadioButton rb = (RadioButton) v;
str2="Room type selected is: " +rb.getText();
selectedOptions.setText(str1+"\n"+str2);
}
};
Output:
34 | P a g e
PROGRAM – 8
Source code:
strings.xml
<resources>
<string name="app_name">SpinnerApp</string>
<string name="menu_settings">Settings</string>
<string
name="title_activity_spinner_app">SpinnerAppActivity</string>
<string name="choose_msg">Choose a fruit</string>
</resources>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="fruits">
<item>Apple</item>
<item>Mango</item>
<item>Orange</item>
<item>Grapes</item>
<item>Banana</item>
</string-array>
</resources>
activity_spinner_app.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/choose_msg"
android:entries="@array/fruits"/>
<TextView
android:id="@+id/selectedopt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
35 | P a g e
SpinnerAppActivity.java
package com.androidunleashed.spinnerapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerAppActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_app);
final TextView selectedOpt= (TextView)findViewById(R.id.selectedopt);
Spinner spin=(Spinner)findViewById(R.id.spinner);
final String[] fruitsArray = getResources().getStringArray(R.array.fruits);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
selectedOpt.setText("You have selected " +fruitsArray[position]);
}
public void onNothingSelected(AdapterView<?> parent)
{
selectedOpt.setText("");
}
});
}
}
Output:
36 | P a g e
PROGRAM – 9
Aim: Create Application by Using Building Blocks for Android Application Design by using
Linear Layout
Source code:
activity_linear_layout_app.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/Apple"
android:text="Apple"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/Mango"
android:text="Mango"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/Banana"
android:text="Banana"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/Apple"
android:text="Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
37 | P a g e
<Button
android:id="@+id/Mango"
android:text="Mango"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/Banana"
android:text="Banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
38 | P a g e
PROGRAM – 10
Aim: Create Application by Using Building Blocks for Android Application Design by using
Relative Layout.
Source code:
activity_relative_layout_app.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/Apple"
android:text="Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginLeft="20dip" />
<Button
android:id="@+id/Mango"
android:text="Mango"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="28dip"
android:layout_toRightOf="@id/Apple"
android:layout_marginLeft="15dip"
android:layout_marginRight="10dip"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/Banana"
android:text="Banana"
android:layout_width="200dip"
android:layout_height="50dip"
android:layout_marginTop="15dip"
android:layout_below="@id/Apple"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/Grapes"
android:text="Grapes"
android:layout_width="wrap_content"
39 | P a g e
android:layout_height="match_parent"
android:minWidth="100dp"
android:layout_alignParentRight="true"
android:layout_below="@id/Banana" />
<Button
android:id="@+id/Kiwi"
android:text="Kiwi"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="@id/Banana"
android:paddingTop="15dip"
android:paddingLeft="25dip"
android:paddingRight="25dip" />
</RelativeLayout>
RelativeLayoutAppActivity.java
package com.androidunleashed.relativelayoutapp;
import android.app.Activity;
import android.os.Bundle;
public class RelativeLayoutDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_relative_layout_app);
}
OUTPUT:
40 | P a g e
PROGRAM – 11
Aim: Create Application by Using Building Blocks for Android Application Design by using
Absolute Layout.
Source code:
activity_absolute_layout_app.xml
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Product Form"
android:textSize="20sp"
android.textStyle="bold"
android:layout_x="90dip"
android:layout_y="2dip"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Code:"
android:layout_x="5dip"
android:layout_y="40dip" />
<EditText
android:id="@+id/product_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100dip"
android:layout_x="110dip"
android:layout_y="30dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Name:"
android:layout_x="5dip"
android:layout_y="90dip"/>
<EditText
android:id="@+id/product_name"
41 | P a g e
android:layout_width="200dip"
android:layout_height="wrap_content"
android:minWidth="200dip"
android:layout_x="110dip"
android:layout_y="80dip"
android:scrollHorizontally="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Price:"
android:layout_x="5dip"
android:layout_y="140dip" />
<EditText
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100dip"
android:layout_x="110dip"
android:layout_y="130dip" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/click_btn"
android:text="Add New Product"
android:layout_x="80dip"
android:layout_y="190dip" />
</AbsoluteLayout>
Output:
42 | P a g e
PROGRAM – 12
Aim: Create the Application to play the Audio and Video clips
Source code:
activity_play_audio_app.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/response"/>
<ToggleButton android:id="@+id/playstop_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Stop"
android:textOff="Play"
android:layout_gravity="center" />
</LinearLayout>
PlayAudioAppActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ToggleButton;
import android.view.View;
import android.widget.TextView;
import android.media.MediaPlayer;
import android.view.View.OnClickListener;
public class PlayAudioAppActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_audio_app);
final TextView response = (TextView)this.findViewById(R.id.response);
response.setText("Select Play button to play audio");
final MediaPlayer mp = MediaPlayer.create(PlayAudioAppActivity.this,R.raw.song1);
final ToggleButton playStopButton = (ToggleButton) findViewById(R.id.playstop_btn);
playStopButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
43 | P a g e
if (playStopButton.isChecked()) {
response.setText("Select Stop button to stop audio");
mp.start();
}
else {
response.setText("Select Play button to play audio");
mp.pause();
}
}
});
}
}
Output:
activity_play_video_app.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView android:id="@+id/video"
android:layout_width="320dip"
android:layout_height="240dip"/>
<Button android:id="@+id/playvideo"
android:text="Play Video"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</LinearLayout>
PlayVedioAppActivity.java
package com.androidunleashed.playvideoapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
44 | P a g e
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
public class PlayVideoAppActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video_app);
Button playVideoButton= (Button)findViewById(R.id.playvideo);
playVideoButton.setOnClickListener(new OnClickListener()
{
public void onClick(View view){
VideoView videoView= (VideoView)findViewById(R.id.video);
videoView.setMediaController(new MediaController(PlayVideoAppActivity.this));
videoView.setVideoPath("sdcard/video.mp4");
videoView.requestFocus();
videoView.start();
}
});
}
}
45 | P a g e
Output:
46 | P a g e
PROGRAM – 13
Aim: Create Application by Using Building Menus and Storing Data.
Source code:
activity_menu_app.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/selectedopt" />
</LinearLayout>
mymenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/create_datab"
android:title="Create Database"
android:icon="@drawable/ic_launcher" />
<item android:id="@+id/insert_rows"
android:title="Insert Rows"
android:icon="@drawable/ic_launcher" />
<item android:id="@+id/list_rows"
android:title="List Rows" />
<item android:id="@+id/search_row"
android:title="Search"
android:icon="@drawable/ic_launcher"/>
<item android:id="@+id/delete_row"
android:title="Delete" />
<item android:id="@+id/update_row"
android:title="Update"
android:icon="@drawable/ic_launcher" />
</menu>
MenuAppActivity.java
package com.androidunleashed.menuapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.TextView;
public class MenuAppActivity extends Activity {
47 | P a g e
private TextView selectedOpt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_app);
selectedOpt=(TextView)findViewById(R.id.selectedopt);
selectedOpt.setText("Please select MENU button to display menu");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu_app, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.create_datab:
selectedOpt.setText("You have selected Create
Database option");
break;
case R.id.insert_rows:
selectedOpt.setText("You have selected Insert Rows option");
break;
case R.id.list_rows:
selectedOpt.setText("You have selected List Rows option");
break;
case R.id.search_row:
selectedOpt.setText("You have selected Search Row option");
break;
case R.id.delete_row:
selectedOpt.setText("You have selected Delete Row option");
break;
case R.id.update_row:
selectedOpt.setText("You have selected Update Row option");
break;
}
return true;
}
}
48 | P a g e
Output:
49 | P a g e
PROGRAM – 14
Aim: Design the Application for Menus and Action Bar.
Source code:
activity_menu_app.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/create_datab"
android:title="Create Database"
android:icon="@drawable/ic_launcher" />
<item android:id="@+id/insert_rows"
android:title="Insert Rows"
android:icon="@drawable/ic_launcher" />
<item android:id="@+id/list_rows"
android:title="List Rows" />
<item android:id="@+id/search_row"
android:title="Search"
android:icon="@drawable/ic_launcher">
<menu>
<group android:checkableBehavior="single">
<item android:id="@+id/search_code"
android:title="Search on Code"
android:checked="true" />
<item android:id="@+id/search_name"
android:title="Search on Name"
android:alphabeticShortcut="n"
android:numericShortcut="6" />
<item android:id="@+id/search_price"
android:title="Search on Price" />
</group>
</menu>
</item>
<item android:id="@+id/delete_row"
android:title="Delete" />
<item android:id="@+id/update_row"
android:title="Update"
android:icon="@drawable/ic_launcher" />
<item android:id="@+id/sort_rows"
android:title="Sort Table"
android:checkable="true"
android:checked="true" />
<item android:id="@+id/merge_row"
android:title="Merge Rows"
android:alphabeticShortcut="m"
50 | P a g e
android:numericShortcut="4" />
</menu>
MenuAppActivity.java:
package com.androidunleashed.menuapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.TextView;
public class MenuAppActivity extends Activity {
private TextView selectedOpt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selectedOpt=(TextView)findViewById(R.id.selectedopt);
selectedOpt.setText("Please select MENU button to display menu");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.create_datab:
selectedOpt.setText("You have selected Create Database option");
break;
case R.id.insert_rows:
selectedOpt.setText("You have selected Insert Rows option");
break;
case R.id.list_rows:
selectedOpt.setText("You have selected List Rows option");
break;
case R.id.search_row:
selectedOpt.setText("You have selected Search Row option");
break;
case R.id.delete_row:
selectedOpt.setText("You have selected Delete Row option");
51 | P a g e
break;
case R.id.update_row:
selectedOpt.setText("You have selected Update Row option");
break;
case R.id.sort_rows:
selectedOpt.setText("You have selected Sort Table option");
item.setChecked(!item.isChecked());
break;
case R.id.merge_row:
selectedOpt.setText("You have selected Merge Rows option");
break;
case R.id.search_code:
selectedOpt.setText("You have selected Search on Code option");
break;
case R.id.search_name:
selectedOpt.setText("You have selected Search on Name option");
break;
case R.id.search_price:
selectedOpt.setText("You have selected Search on Price option");
break;
}
return true;
}
}
Output:
52 | P a g e
PROGRAM – 15
Aim: Design the application to display the Drop-Down List Action Bar.
Source code:
ListActionBarAppActivity.java
package com.androidunleashed.listactionbarapp;
import android.app.Activity;
import android.os.Bundle;
import android.app.ActionBar.OnNavigationListener;
import android.app.ActionBar;
import android.widget.ArrayAdapter;
import android.util.Log;
public class ListActionBarAppActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] items = new String[] { "Create", "Insert", "Update", "Search" };
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_dropdown_item, items);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter,
onNavigationItemSelected);
}
OnNavigationListener onNavigationItemSelected = new OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int
itemPosition, long itemId) {
Log.d("Option ", String.valueOf(itemId) + " is selected");
return true;
}
};
}
styles.xml
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="android:Theme.Light" />
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar" />
</resources>
Note: The log messages displayed on selecting the two items Update and Create from the
drop-down list. The ID of the selected drop-down item is displayed using the itemId parameter in the
onNavigationItemSelected() method.
53 | P a g e
Output:
54 | P a g e