web viewmobile devices such as smartphones. and tablet computers. developed by google and later the...

Post on 30-Jan-2018

227 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

PRACTICAL-1

AIM: Introduction to Android

Q-1 What is Android?

Ans: Android is such an operating system for low powered devices, that run on battery and are full of hardware like Global Positioning System (GPS) receivers, cameras, light and orientation sensors, Wi-Fi and UMTS (3G telephony) connectivity and a touchscreen.

Android is a software platform or OS for mobile device. Based on Linux kernel. Primarily design for touchscreen mobile devices such as smartphones and tablet

computers. Developed by Google and later the open Handset Alliance (OHA). Android was unveiled in 2007 along with the founding of the Open Handset

Alliance: a consortium of hardware, software, and telecommunication companies devoted to advancing open standards for mobile devices.

The first Android-powered phone was sold in October 2008.Android is open source and Google releases the code under the Apache License.

These factors have contributed towards making Android the world's most widely used smart phone platform, overtaking Symbian in the fourth quarter of 2010, and the software of choice for technology companies who require a low-cost, customizable, lightweight operating system for high tech devices without developing one from scratch.

As a result, despite being primarily designed for phones and tablets, it has seen additional applications on televisions, games consoles, digital cameras and other electronics.

Android's open nature has further encouraged a large community of developers and enthusiasts to use the open source code as a foundation for community-driven projects, which add new features for advanced users or bring Android to devices which were officially released running other operating systems.

Additionally, Android has a large community of developers writing applications ("apps") that extend the functionality of devices. In October 2012, there were approximately 700,000 apps available for Android.

A report in July 2013 stated that Android's share of the global smartphone market, led by Samsung products, was 64% in March 2013.

Q-2 Architecture of Android?

Ans: The Android OS can be referred to as a software stack of different layers, where each layer is a group of several program components. Together it includes operating system, middleware and important applications. Each layer in the architecture provides different services to the layer just below it.

1

Linux kernel The basic layer is the Linux kernel. The whole Android OS is built on top of the

Linux 2.6 Kernel with some further architectural changes made by Google.

It is this Linux that interacts with the hardware and contains all the essential hardware drivers.

Fig 2.1 Architecture of Android

The Linux kernel also acts as an abstraction layer between the hardware and other software layers.

Android uses the Linux for all its core functionality such as Memory management, process management, networking, security settings etc.

Libraries The next layer is the Android’s native libraries. It is this layer that enables the

device to handle different types of data. These libraries are written in c or c++ language and are specific for a particular hardware.

Some of the important native libraries: Surface Manager: It is used for compositing window manager with off-screen

buffering. Off-screen buffering means you can’t directly draw into the screen, but your drawings go to the off-screen buffer. This off screen buffer is the reason behind the transparency of windows.

2

Media framework: Media framework provides different media codecs allowing the recording and playback of different media formats.

SQLite: SQLite is the database engine used in android for data storage purposes. WebKit: It is the browser engine used to display HTML content. OpenGL: Used to render 2D or 3D graphics content to the screen.

Android Runtime Android Runtime consists of Dalvik Virtual machine and Core Java libraries.

Dalvik Virtual Machine It is a type of JVM used in android devices to run apps and is optimized for low

processing power and low memory environments. Unlike the JVM, the Dalvik Virtual Machine doesn’t run .class files, instead it

runs .dex files. .dex files are built from .class file at the time of compilation and provide higher

efficiency in low resource environments.

Core Java Libraries These are different from Java SE and Java ME libraries. However these libraries

provide most of the functionalities defined in the Java SE libraries.

Application Framework These are the blocks that our applications directly interact with. These programs

manage the basic functions of phone like resource management, voice call management etc.

Applications Applications are the top layer in the Android architecture and this is where our

applications are going to fit. Several standard applications comes pre-installed with every device, such as:

o SMS client appo Dialero Web browsero Contact manager

As a developer we are able to write an app which replaces any existing system app. That is, you are not limited in accessing any particular feature. Thus Android is opening endless opportunities to the developer.

3

Q-3 How to setup Android?Ans: These are the components you require to setup your Android IDE:

Java Development Kit Android SDK Eclipse

 Now it’s time to download these one by one:

JDK (Java Development Kit)It is the most important one as it forms the base for all other tools. It consists of Java Run time Environment and developer tools. For everything we are going to do here, JDK is required.  To download Java SE JDK go to the Java SE download page of Oracle here:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Fig 3.1 Java SE JDK Download

Accept the License agreement and select the platform.

Android SDK

4

Android SDK is the collection of various tools and libraries which is designed to be linked with the development software we are using and make the development environment Android-specific.

In our case it is to be linked with the Eclipse (Eclipse is not the only development environment for Android. We use it here because it is the most popular and simple to work with).

Download the latest Android SDK from Android Developers websiteYou can download either the executable installation file or the compressed archive. Both are same.

http://developer.android.com/sdk/index.html

Fig 3.2 SDK Download

EclipseEclipse is the highly popular IDE for Java and here we are going to make it an Android specific IDE by installing the ADT plugin and linking with the Android SDK.

That is, you are going to do all your coding, testing and debugging works in the Eclipse.

Before downloading the Eclipse, make sure that it is compatible with the Android SDK you’ve just downloaded before.

5

Fig 3.3 Eclipse Download 

Installation:We’ve completed downloading the entire tools essential to start with, now we’ll see how to install these components.

JDKInstallation of the Java SE is a relatively painless task. I think it doesn’t need much explanation. It is an executable installation package and installs it like any other software packages. Leave the default packages and installation location unchanged and proceeds.

After it completes installing JDK, will ask the location for JRE to be installed. No need to change the default location. Click next. After the successful completion of the installation, it will show you a window like this. Click continue Android SDK

6

If you’ve downloaded the zip file of Android SDK, then you need to unzip the archive into the C: drive of your computer such that the software goes into C: / android-sdk-windows folder (in case of Windows platform). If you downloaded the installation package of Android SDK then install it by setting the installation location to your C:/ root folder.

EclipseEclipse is also zip archive and doesn’t need installation. Just extract the zip file and place it in your C: drive such that the Eclipse software is in C:/Eclipse folder. Then go to that Eclipse folder and find a file named Eclipse.exe. Right click on the file and select Create Shortcut. Copy the shortcut and place it in your desktop.

ADT Plugin for EclipseADT is a custom plugin for Eclipse to give a powerful integrated IDE for Android Development. It allows user to develop, run and debug Android Applications.Open Eclipse by double clicking the Eclipse shortcut on the desktop. It will ask for the default workspace location. Set it as C: / Eclipse workspace and continue.

Fig 3.4.1 Install new Software

In the new window click Add

7

Fig 3.4.2 ADD site Add Repository window opens In the name field enter ‘ADT’.In the location field add the URL

https://dlssl.google.com/android/eclipse/

Fig 3.4.3 Assign Name

8

Click OKIf any trouble occurs in downloading, use http in place of https in the URLIn the next window, you will see the list of files to be downloaded.

Click Next

Fig 3.4.4 Available SoftwareNext window shows the review of files to be installed. Click Next. Accept the Apache license and continue.

After the installation completes, it will ask you to whether restart the eclipse or not.

9

  Fig 3.4.5 ADT Notification

Click RestartNow

When Eclipse restarts, configure SDK window appears. Select Use existing SDK option and browse the folder C: / android-sdk-windows and click Finish.

Fig 3.5 SDK configure

Adding Platform Packages to the Android SDKWe’ve downloaded the Android SDK, but we can’t start development with the Android SDK as it is. This is because it doesn’t contain any packages. We need to add packages to the Android SDK.

Packages are specific to a particular version of Android, for example, if we are planning to develop for 2.3, then we want to install the platform packages for Android 2.3 that is API-10. Suppose we want to support the earlier versions of Android and want to test whether our app works without errors in the earlier versions or not. Then we need each of those platforms of the Android versions we are planning to add support to.

10

Open Android SDK managerYou can open it from its folder or by just clicking the Android SDK manager button on the SDK toolbar.

Fig 3.6.1 SDK Manager

When opens, it will automatically show the packages available for download.Expand tools on the left. Select SDK tools and SDK platform tools.

11

Fig 3.6.2 Install Packages

To select the required packages expand them. Select SDK packages and samples for SDK and Google API. You need not install the third party SDKs unless you are programming for a particular device. (Google API is also not mandatory, it is required only if we use any Google APIs in our application. For simple applications, it isn’t required).

As a beginner, you need to download SDK platform and samples for Android 2.3. Select Google USB driver package under extras, if you want to test your app on a physical device.

After selecting, click Install Packages.In the next window, the components we’ve selected appear as a list and we can make a review. To reject a package, select it and click Reject.

12

Fig 3.6.3 Package Selection Window

After making the final list of components, click install.

Setup Android Virtual DeviceThe final step is to configure an Android Virtual Device. Android Virtual Device is the Android emulator on which we are run our program. We can create an AVD to match with the configurations of any Android device available in the market. And there’s no limit in the number of AVDs, we can create as many AVDs as we want in different configurations.

To create an AVD, go to Android Virtual Device Manager button in the Eclipse toolbar. Click New on the Right side.

13

Fig 3.7.1 Android Virtual Device (AVD) Manager

Give any name in the name field. (The name should be such that you can distinguish it later, when multiple AVDs are created). Select the target API from the drop down list. In this case, I’ve selected 2.3 as target. Enter desired size for SD card.

Under skin section leave the default resolution, WVGA800 unchanged. Click Create AVD.

14

Fig 3.7.2 Create new AVD

The Android Virtual Devise you’ve just created appears on the list of AVDs. Select it and click Start

15

Fig 3.7.3 Start Created AVD

Now your AVD boots. It will take several minutes to load for the first time. After the loading is completed, it shows you an Android Home screen. This is your virtual Android devise where you are going to test- run your programs.

Q-4 Benefits of Android?Ans: Android is opening endless opportunities to the developer.

Memory Management: Since Android devices are usually battery-powered, Android is designed to manage memory (RAM) to keep power consumption at a minimum. When an Android app is no longer in use, the system will automatically suspend it in memory.

Open source community: Android is open source and Google releases the code under the Apache License.

Security and privacy: Android applications run in a sandbox, an isolated area of the system that does not have access to the rest of the system's resources, unless access permissions are explicitly granted by the user when the application is installed.

Licensing: The source code for Android is available under free and open-source software licenses.

One of the most important features of Android is the integration of other Google products and services into the platform like Gmail, Google Calendar, Google Maps and Google Docs etc.

Users also enjoy Multilanguage Supportamong other benefits such asMultitasking, Tethering and a Massive External Storage Capability.

16

PRACTICAL-2

AIM: Create Simple Hello World Application in Android.

Graphical Layout

Fig 2.1 Graphical Layout

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"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">

<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" />

17

</RelativeLayout>

MainActivity.java

package com.example.b1;

importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;

public class MainActivity extends Activity {

@Overrideprotected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

}

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;

}

}

18

Output

Fig 2.3 Output Window

19

PRACTICAL-3

AIM: Create an Android application to check which button you have clicked.

Graphical Layout

Fig 3.1 Graphical Layout

20

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"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">

<Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/button1"android:layout_alignBottom="@+id/button1"android:layout_marginLeft="23dp"android:layout_toRightOf="@+id/button1"android:onClick="onMyButton2Click"android:text= "Button2" />

<Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="28dp"android:layout_marginTop="64dp"android:onClick="onMyButton1Click"android:text= "Button1" />

</RelativeLayout>

21

MainActivity.javapackage com.example.b3;

importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.view.View;importandroid.widget.Toast;

public class MainActivity extends Activity {

@Overrideprotected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

}

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;

}public void onMyButton2Click(View view) { Toast.makeText(this, "Button 2 clicked!", Toast.LENGTH_SHORT).show(); }public void onMyButton1Click(View view) { Toast.makeText(this, "Button 1 clicked!", Toast.LENGTH_SHORT).show(); }

}

22

Output

Fig 3.3 Output Window 1

23

Fig 3.4 Output Window 2

24

PRACTICAL-4

AIM: Create a Dialog Box in Android.

Graphical Layout

Fig 4.1 Graphical Layout

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"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"></RelativeLayout>

25

MainActivity.java

package com.example.b4;

importandroid.os.Bundle;importandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.DialogInterface;importandroid.view.Menu;importandroid.widget.Toast;

publicclassMainActivityextends Activity{protectedvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

AlertDialog.Builderadb=newAlertDialog.Builder(this);adb.setTitle("Alert Dialog...");adb.setMessage("Are You Sure?");adb.setPositiveButton("Yes",newDialogInterface.OnClickListener() {

publicvoidonClick(DialogInterface arg0, int arg1){

Toast.makeText(getApplicationContext(), "You Pressed Yes..", Toast.LENGTH_LONG).show();

}});

adb.setNegativeButton("No",newDialogInterface.OnClickListener() {

publicvoidonClick(DialogInterface arg0, int arg1){

Toast.makeText(getApplicationContext(), "You Pressed No..", Toast.LENGTH_LONG).show();

}});

adb.show(); }publicbooleanonCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);returntrue; }

26

}Output

Fig 4.2.1 Output Window

27

Fig 4.2.2 Output Window

28

PRACTICAL-5

AIM: Generate a simple Calculator.

Graphical Layout

Fig 5.1 Graphical Layout

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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"

29

tools:context=".MainActivity">

<RadioGroupandroid:id="@+id/radioGroup1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true">

<RadioButtonandroid:id="@+id/add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text= "Addition" />

<RadioButtonandroid:id="@+id/sub"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text= "Substraction" />

<RadioButtonandroid:id="@+id/mul"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text= "Multiplication" />

<RadioButtonandroid:id="@+id/div"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text= "Division" /></RadioGroup>

<TextViewandroid:id="@+id/textView4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="20dp"android:text= "Simple Calculator" android:textAppearance="?android:attr/textAppearanceLarge"/>

<TextView

30

android:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/editText2"android:layout_alignRight="@+id/button1"android:layout_marginBottom="14dp"android:layout_marginRight="19dp"android:text= "Enter 1st number:" android:textAppearance="?android:attr/textAppearanceMedium"/>

<EditTextandroid:id="@+id/editText1"

android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/textView1"android:layout_alignBottom="@+id/textView1"android:layout_alignRight="@+id/editText2"android:layout_toRightOf="@+id/textView3"android:ems="10"android:inputType="number"/>

<TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/button1"android:layout_centerHorizontal="true"android:layout_marginTop="35dp"android:text= "RESULT" android:textAppearance="?android:attr/textAppearanceLarge"/>

<TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/editText2"android:layout_alignBottom="@+id/editText2"android:layout_alignLeft="@+id/textView1"android:text= "Enter 2nd number:" android:textAppearance="?android:attr/textAppearanceMedium"/>

<EditTextandroid:id="@+id/editText2"android:layout_width="wrap_content"android:layout_height="wrap_content"

31

android:layout_above="@+id/radioGroup1"android:layout_toRightOf="@+id/textView1"android:ems="10"android:inputType="number"/>

<Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/radioGroup1"android:layout_centerHorizontal="true"android:layout_marginTop="31dp"android:text= "Apply" />

</RelativeLayout>

MainActivity.java

package com.example.b5;

importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.*;importandroid.widget.TextView;importandroid.widget.Toast;

publicclassMainActivityextends Activity {ProtectedvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);finalEditText num1=(EditText)findViewById(R.id.editText1);finalEditText num2=(EditText)findViewById(R.id.editText2);finalTextView result=(TextView)findViewById(R.id.textView3);finalRadioGrouprg=(RadioGroup)findViewById(R.id.radioGroup1);final Button b1=(Button)findViewById(R.id.button1);b1.setOnClickListener(newOnClickListener(){

PublicvoidonClick(View arg0){

32

if(num1.getText().toString().isEmpty() || num2.getText().toString().isEmpty()){Toast.makeText(getApplicationContext(), "Please Enter Values..", Toast.LENGTH_LONG).show();}else{Double n1=Double.parseDouble(num1.getText().toString());Double n2=Double.parseDouble(num2.getText().toString());Intselectoperation=rg.getCheckedRadioButtonId();RadioButton op=(RadioButton)findViewById(selectoperation);String s=newString(op.getText().toString());

if(s.equals("Addition")){

result.setText(String.valueOf(n1+n2));}elseif(s.equals("Substraction")){

result.setText(String.valueOf(n1-n2));}elseif(s.equals("Multiplication")){

result.setText(String.valueOf(n1*n2));}elseif(s.equals("Division")){if(n2==0){

result.setText("DIVIDE BY 0 ERROR!!!");}else{

result.setText(String.valueOf(n1/n2));}}}

}});}PublicbooleanonCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);returntrue;} }

33

Output

Fig 5.2.1 Output Window Addition

34

Fig 5.2.2 Output Window Subtraction

35

Fig 5.2.3 Output Window Multiplication

36

Fig 5.2.4 Output Window Division

37

Fig 5.2.5 Output Window Divide/0

38

PRACTICAL-6

AIM:Create an android application that contains multiple tabs.

MainActivity.java

package com.example.tabapp1;

importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.widget.TabHost;importandroid.widget.TabHost.TabSpec;

public class MainActivity extends Activity {

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

TabHost tbhst1=(TabHost)findViewById(android.R.id.tabhost);tbhst1.setup();TabSpec spec1=tbhst1.newTabSpec("Tab 11");spec1.setIndicator("ANALOG CLOCK");spec1.setContent(R.id.analogClock1);TabSpec spec2=tbhst1.newTabSpec("Tab 22");spec2.setIndicator("DIGITAL CLOCK");spec2.setContent(R.id.digitalClock1);tbhst1.addTab(spec1);tbhst1.addTab(spec2);

}

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true; }

}

39

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"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">

<TabHostandroid:id="@android:id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_alignParentTop="true">

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">

<TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="wrap_content"></TabWidget>

<FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent">

<LinearLayoutandroid:id="@+id/tab1"android:layout_width="match_parent"android:layout_height="match_parent">

<AnalogClockandroid:id="@+id/analogClock1"android:layout_width="wrap_content"

40

android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayoutandroid:id="@+id/tab2"android:layout_width="match_parent"android:layout_height="match_parent"tools:ignore="Orientation">

<DigitalClockandroid:id="@+id/digitalClock1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="DigitalClock"tools:ignore="HardcodedText" />

</LinearLayout>

</FrameLayout></LinearLayout></TabHost>

</RelativeLayout>

41

Output:

42

PRACTICAL – 7

AIM: Create listview with multiple choice in android.

Activity_main.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.listviewexample"android:versionCode="1"android:versionName="1.0" >

<uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="15" />

<applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".MainActivity"android:label="@string/title_activity_main" ><intent-filter><action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

MainActivity.java

importandroid.app.ListActivity;importandroid.os.Bundle;importandroid.view.ActionMode;importandroid.view.Menu;importandroid.view.MenuInflater;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.AdapterView.OnItemLongClickListener;importandroid.widget.Toast;

43

public class MyListActivityActionbar extends ListActivity {

protected Object mActionMode;publicintselectedItem = -1;public void onCreate(Bundle icicle) {

super.onCreate(icicle);setContentView(R.layout.main);String[] values = new String[] { “Mercury",”Venus”,”Mars”,”Jupiter”,”Saturn”,”Uranus”,”Neptune”};

MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);setListAdapter(adapter);getListView().setOnItemLongClickListener(new OnItemLongClickListener() { @OverridepublicbooleanonItemLongClick(AdapterView<?> parent, View view,int position, long id) {if (mActionMode != null) {return false; }selectedItem = position;mActionMode = MyListActivityActionbar.this .startActionMode(mActionModeCallback);view.setSelected(true);return true; } }); }

privateActionMode.CallbackmActionModeCallback = new ActionMode.Callback() {publicbooleanonCreateActionMode(ActionMode mode, Menu menu) {MenuInflaterinflater = mode.getMenuInflater();inflater.inflate(R.menu.rowselection, menu);return true; }publicbooleanonPrepareActionMode(ActionMode mode, Menu menu) {return false; // Return false if nothing is done }publicbooleanonActionItemClicked(ActionMode mode, MenuItem item) {switch (item.getItemId()) {case R.id.menuitem1_show:show();mode.finish();return true;default:return false; } }

44

public void onDestroyActionMode(ActionMode mode) {mActionMode = null;selectedItem = -1; } };

private void show() {Toast.makeText(MyListActivityActionbar.this,String.valueOf(selectedItem), Toast.LENGTH_LONG).show(); }}

OUTPUT

Figure 7.1 – ListView

45

PRACTICAL – 8

AIM: Create an application in android for slideshow animation.

Graphical Layout

Figure 8.1 –Main layout

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?><TableLayoutandroid:id="@+id/TableLayout1" xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><TableRowandroid:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">

<Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"

46

android:layout_weight="1"android:text="Start Ani" ></Button>

<Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="Stop Ani" ></Button></TableRow><ImageViewandroid:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_width="wrap_content"></ImageView></TableLayout>

MainActivity.java

package sl.mc;

importandroid.app.Activity;importandroid.graphics.drawable.AnimationDrawable;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.ImageView;

public class AnimationActivity extends Activity {Button b1,b2;ImageView i1;AnimationDrawable ad;

@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);

b1=(Button)findViewById(R.id.button1); b2=(Button)findViewById(R.id.button2); i1=(ImageView)findViewById(R.id.imageView1);

i1.setBackgroundResource(R.drawable.framebyframe);

47

b1.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {ad=(AnimationDrawable)i1.getBackground();ad.start();

}});

b2.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {

.stop();}});

}}

OUTPUT

Figure 8.2.1 - First Slide

48

Figure 8.2.2–Second Slide

Figure 8.2.3 - Third Slide

49

PRACTICAL – 9

AIM:Create an application in android to find factorial of a number using OnClick event.

Graphical Layout

Activity_main.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" >

<TextViewandroid:id="@+id/txtView"android:layout_width="150dp"android:layout_height="150dp"android:text="@string/hello" />

<Button

50

android:id="@+id/mybtn"android:layout_width="50dp"android:layout_height="30dp" />

<TextViewandroid:id="@+id/viewwidth"android:layout_width="fill_parent"android:layout_height="wrap_content" />

<TextViewandroid:id="@+id/viewheight"android:layout_width="fill_parent"android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.TextView;

importcom.droidacid.apticalc.R;

public class AptiFactorial extends Activity implements android.view.View.OnClickListener{EditText number;TextView answer;Button calculate;

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.apti_factorial);initialize();}

private void initialize() {number = (EditText) findViewById(R.id.et_apti_number);

51

number.setHint("Enter number to be factorialized :P")answer = (TextView) findViewById(R.id.tv_apti_answer);calculate = (Button) findViewById(R.id.b_apti_calc);calculate.setOnClickListener(this);}

private long calcFactorial() {

long factorial = 1;try {factorial = Long.parseLong(number.getText().toString());for(int i=factorial-1; i>0; i--){factorial = i * factorial; } } catch (NumberFormatException e) {Toast.makeText(this, "Incorrect Input", Toast.LENGTH_LONG).show(); } finally {}

return factorial;}

@Overridepublic void onClick(View v) {answer.setText("Factorial of " + number.getText().toString() + " is : " + calcFactorial());}

OUTPUT

52

53

top related