P. 1
JaysPatel Android

JaysPatel Android

|Views: 140|Likes:
Published by Jays Patel

More info:

Published by: Jays Patel on Oct 05, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less






  • A.Android Software Stack
  • B.1 Important Android terms
  • B.2 Activities and Layouts
  • B.3 Activities and Lifecycle
  • B.4 Context
  • E.1 Create Project Tip
  • E.2 Two faces of things
  • E.3 Create attributes
  • E.4 Add UI Elements
  • E.5 Maintain UI properties
  • E.6 Code your applicatioin
  • E.7 Start Project
  • E.8 Using the home menue
  • F.1. Overview
  • F.2. Simple ListActivities
  • F.3. ListActivities with own layout
  • F.4. ListActivities with flexible layout
  • G.1. Project
  • G.2. Add a menu
  • G.3. Using preferences
  • G.4. Run
  • H.1. Overview
  • H.2. Create contacts on your emulator
  • H.3. Example
  • J. Services and Broadcast Receivers
  • K.1. LogCat View
  • K.2. File explorer
  • L.1 Android Debugging Bridge - Shell
  • L.2 Uninstall an application via adb
  • L.3 Emulator Console via telnet

( Word to PDF Converter - Unregistered ) http://www.Word-to-PDF-Converter.


Android Easy View

Prepared By. Jays Patel Mobile: 9099340469 Email: jayesh.in_ahd@yahoo.com

A hands-on Guide to building mobile applications, this book features concise and compelling examples that show you how to quickly construct real-world mobile applications for Android phones. Fully up-to-date for version 1.0 of the Android SDK. It covers all the essential features and explores the advances capabilities of Android.

( Word to PDF Converter - Unregistered ) http://www.Word-to-PDF-Converter.net

Android Easy View Course Ware

A. Android Software Stack B. Android Operation System 1. Android Terms 2. Activities and Layouts 3. Activities and Lifecycle 4. Context C. Installation and Setup 1. Android SDK 2. Eclipse 3. Configuration 4. Devices D. Error Handling E. Your First Android Project 1. Create Project 2. Two faces of things 3. Create Attributes 4. Add UI Elements 5. Maintain UI Properties 6. Code your application 7. Start Project 8. Using Home Menu

( Word to PDF Converter - Unregistered ) http://www.Word-to-PDF-Converter.net
F. Lists 1. Overview 2. Simple List Activities 3. List Activities with own Layout 4. List Activities with flexible Layout G. Menu, Preference and Intents 1. Project 2. Add a menu 3. Using preference 4. Run H. Content Provider 1. Overview 2. Create Contents on your emulator 3. Example I. J. Scroll View Service and Broad cast Receiver

K. Important View 1. Log cat View 2. File Explorer L. Shell 1. Android Debugging Bridge Shell 2. Uninstall an application via adb 3. Emulator Console via telnet M. Deploy your Application on real Devices N. Thanks You

net A. Understanding the Android Software Stack .( Word to PDF Converter .Unregistered ) http://www.Word-to-PDF-Converter.

security.6 kernel. network. Libraries Running on top of the kernel. The kernel also provides an abstraction layer between the hardware and the remainder of the stack.Word-to-PDF-Converter. as well as: A media library for playback of audio and video media A Surface manager to provide display management Graphics libraries that include SGL and OpenGL for 2D and 3D graphics .net Linux Kernel Core services (including hardware drivers. and power management) are handled by a Linux 2. process and memory management.Unregistered ) http://www.( Word to PDF Converter . Android include various C/C++ core libraries such as libc and SSL.

a compiler.( Word to PDF Converter . Android Operation System Android is an operating system based on Linux with a Java programming interface. forms the basis for the application framework. debugger and a device emulator as well as its own Java Virtual machine (Dalvik Virtual Machine .Word-to-PDF-Converter. The application layer runs within the Android run time using the classes and services made available from the application framework. Android provides a tool "dx" which allows to . Application Framework The application framework provides the classes used to create Android applications.DVM). along with the libraries. Core Libraries While Android development is done in Java.g. Android is created by the Open Handset Alliance which is lead by Google.net SQLite for native database support SSL and WebKit for integrated web browser and Internet security Android Run Time What makes an Android phone an Android phone rather than a mobile Linux implementation is the Android run time. Dalvik is not a Java VM. the Android run time is the engine that powers your applications and. Including the core libraries and the Dalvik virtual machine. Android uses a special Java virtual machine (Dalvik) which is based on the Apache Harmony Java implementation. both native and third party. Dalvik Virtual Machine Dalvik is a register-based virtual machine that’s been optimized to ensure that a device can run multiple instances effi ciently. Dalvik uses special byte code. The core Android libraries provide most of the functionality available in the core Java libraries as well as the Android-specifi c libraries. e.Unregistered ) http://www. It provides tools. It also provides a generic abstraction for hardware access and manages the user interface and application resources. Application Layer All applications. are built on the application layer using the same API libraries. Therefore you cannot run standard Java byte code on Android. It relies on the Linux kernel for threading and low-level memory management. B.

Application register themself via an IntentFilter.A screen in the Android application Services . Android applications are then packed into an .Background activities without UI Content Provider . can be used to react to changed conditions in the system Intends allow the application to request and / or provide services . e. The Android system defines a life cycle for an activities via pre-defined methods. For example the application call ask via an intent for a contact application. This files contains all activities application and the required permissions for the application. For development Google provides the Android Development Tools (ADT) for Eclipse to develop Android applications.net convert Java Class files into "dex" (Dalvik Executable) files. B. "AndroidManifest. Intends are a powerful concept as they allow to create loosely coupled applications. The layout defines the UI elements. because of an incoming call.3 Activities and Lifecycle The operating system controls the life cycle of your application.1 Important Android terms An Android application consists out of the following parts: Activity . The XML way is usually preferred for a fixed layout while defining the layout via code is more flexible.Unregistered ) http://www. B. You can also mix both approaches.apk (Android Package) file.Word-to-PDF-Converter.g. Every Android applications runs in its own process and it isolated from other running applications. their properties and their arragement. Android contains a SQLLite DB which can serve as data provider Broadcast Receiver .receives system messages.xml" can be thought as the deployment descriptor for an Android application. For example if the application requires network access it must be specified here. An Android application is described the file "AndroidManifest.provides data to applications. B.( Word to PDF Converter . Therefore on misbehaving application cannot harm other Android applications.2 Activities and Layouts The user interface for Activities is defined via layouts. The most . Android supports 2-D and 3-D graphics using the OpenGL libraries and supports data storage in a SQLLite database.xml". At any time the Android system may stop or destroy your application. A layout can be defined via XML and via code at runtime.

Context provides the connections to the Android system.Word-to-PDF-Converter. C. Installation and Setup The following assume that you have already Eclipse installed. For details please see Eclipse Tutorial . .2 Eclipse Use the Eclipse update manager to install all available plugins for the Android Development Tools (ADT) from the URL https://dl-ssl. Used to save data so that the activity can restore its states if re-started onPause() .content.3 Configuration In Eclipse open the Preferences dialog via Windows -> Preferences. The download contains a zip file which you can extract to any place in your file system. C. Select Android and maintain the installation path of the Android SDK. e. can be used to release ressource or save data onResume() .1 Android SDK Download the Android SDK from the Android homepage under Android SDK download .4 Context The class android. C. Contexts provides the method getSystemService which allows to receive a manager object for the different hardware parts.always called if the Activity ends.com/android/eclipse/ .g.called if the Activity is re-started.( Word to PDF Converter . I placed it under "c:\android-sdk-windows" C.google. As Activities and Services extend this class you can directly access the context via "this". can be used to initiaze fields B.called if the activity is stopped.net important methods are: onSaveInstanceState() .Unregistered ) http://www.

. Select available packages and select the latest version of the SDK. Join me in starring at Bug 3210 to get this improved. Select now Window -> Android SDK and AVD Manager from the menu.Unregistered ) http://www.( Word to PDF Converter .Word-to-PDF-Converter.net Tip If you maintain the location the Android plugin will remind you frequently (and for every workspace).

4 Device You need to define a device which can be used for emulation. press "New" and maintain the following.( Word to PDF Converter .net Press "Install selected" and confirm the license for all package.Unregistered ) http://www. C. After the installation restart Eclipse. Press the device manager button. .Word-to-PDF-Converter.

Unregistered ) http://www. To test if you setup is correct.Word-to-PDF-Converter.This will create the device. eelect .net Press "Create AVD".( Word to PDF Converter .

Unregistered ) http://www.net your device and press "Start". .( Word to PDF Converter . Tip You can use the perspective "DDMS" to monitor your device.Word-to-PDF-Converter. After (a long time) your device should be started.

( Word to PDF Converter . If you having problems with your own code you can use the LogCat viewer as described in LogCat Viewer . To solve this error select from the menu Project -> Clean. Maintain the following. is missing required source folder: 'gen' 2. The project could not be built until build path errors are resolved.android.Word-to-PDF-Converter.1 Create Project Tip This app is also available on the Android Marketplace.net D. Search for "vogella" for find this example. Project . E. .Unregistered ) http://www. Several users report that get the following errors: 1..temperature".vogella. Select File -> New -> Other -> Android -> Android Project and create the Android project "de. Your First Android Project E.. Error Handling Things are not always working as they should be.

( Word to PDF Converter .Unregistered ) http://www.net Tip I think this wizard should have the option to add the project to an existing working set.Word-to-PDF-Converter. . This should create the following directory structure. Press "Finish". Please stare at Android New Project Wizard should have the option to add to Working set to get this functionality.

java" is a generated class which contains the text and the UI elements. strings and UI's. The following description tries to use the rich UI but for validation lists also the XML. For example: .Unregistered ) http://www.Word-to-PDF-Converter.g. via a rich editor and directly via XML. E.( Word to PDF Converter . Please do not try to modify this class manually. in two ways.2 Two faces of things The Android SDK allows to maintain certain artifacts.net "R. You can switch between the two things the the tab on the lower part of the screen. e.

g. These attributes can be used in your UI definition via XML or in your Java source code. .3 Create attributes Modifying "string. for strings and / or colors.Unregistered ) http://www.Word-to-PDF-Converter. As a workaround you can select Window -> Preferences -> XML -> XML Files -> Editor. e.net E. Uncheck "Use inferred grammar in absence of DTD/Schema".( Word to PDF Converter .xml" and press "Add". Select the file "res/values/string. Android allows to create attributes for resources. for details see the Bug report . Select "Color" and maintain "myColor" as the name and "#3399CC" as the value.xml" gives currently a NPE.

Table 1.Word-to-PDF-Converter. String Attributes Name Value buttonHandler myClickHandler celsius to Celsius fahrenheit to Fahrenheit Switch to the XML representation and validate that you maintained the values correctly.Unregistered ) http://www. . String attributes allow to translate the application at a later point.net Add also the following "String" attributes.( Word to PDF Converter .

This editor allows to maintain the UI via drag and drop or directly via the XML source code.Word-to-PDF-Converter. Add from the layout a "RadioGroup" and then two RadioButtons.( Word to PDF Converter . Convert!</string> <string name="app_name">Temperature Converter</string> <color name="myColor">#3399CC</color> <string name="buttonHandler">myClickHandler</string> <string name="celsius">to Celsius</string> <string name="fahrenheit">to Fahrenheit</string> <string name="calc">Calculate</string> </resources> E. Delete the "Hello World. You can switch between both representations via the tabs at the bottom of the editor.Unregistered ) http://www.xml" and open the Android editor via double-click. . Make sure that your code is the same as listed below. add one "Button". The result should look like the following and the corresponding XML is listed below. drag in an "EditText".net <?xml version="1. From the "Views" bar.0" encoding="utf-8"?> <resources> <string name="hello">Hello World.4 Add UI Elements Select "res/layout/main. For changing the postion and grouping elements you can use the outline view. Hello!" via a right mouse click.

com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:text="@+id/EditText01" android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> <RadioGroup android:id="@+id/RadioGroup01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:text="@+id/RadioButton01" android:id="@+id/RadioButton01" android:layout_width="wrap_content" android:layout_height="wrap_content"></RadioButton> <RadioButton android:text="@+id/RadioButton02" android:id="@+id/RadioButton02" android:layout_width="wrap_content" .Unregistered ) http://www.net Switch to "main.( Word to PDF Converter .xml" and verify that your XML looks like the following.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas. <?xml version="1.android.Word-to-PDF-Converter.

net android:layout_height="wrap_content"></RadioButton> </RadioGroup> <Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> E.Word-to-PDF-Converter.5 Maintain UI properties If you select a UI element you can change its properties via the properties view. Select EditText and change the property "Layout Width" to "fill_parent".Unregistered ) http://www.( Word to PDF Converter . .

( Word to PDF Converter .net Assign the "celsius" string attribute to your "text" property of the first radio button and .Word-to-PDF-Converter.Unregistered ) http://www.

( Word to PDF Converter . Set the property "Checked" to true for the first RadioButton. Switch to the "main.xml" tab and verify that the XML is correctly maintained. Select the complete widget and use the Properties view to set the property "background" to the color attribute "@color/myColor". Delete the text property in the EditText (this means no text will be initially shown) and set the "Input type" property to "numberSigned" and "number decimal".Word-to-PDF-Converter.net "fahrenheit" to the second. .Unregistered ) http://www. Assign "calc" to the text property of your button and assign "buttonHandler" to the "onClick" property.

net <?xml version="1.Unregistered ) http://www.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/myColor"> <EditText android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:inputType="numberSigned|numberDecimal"></EditText> <RadioGroup android:id="@+id/RadioGroup01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/RadioButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/celsius" android:checked="true"></RadioButton> <RadioButton android:id="@+id/RadioButton02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/fahrenheit"></RadioButton> </RadioGroup> <Button android:id="@+id/Button01" android:layout_height="wrap_content" android:onClick="@string/buttonHandler" android:layout_width="wrap_content" android:text="@string/calc"></Button> </LinearLayout> .0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.Word-to-PDF-Converter.( Word to PDF Converter .android.

android.widget.( Word to PDF Converter . public class Convert extends Activity { private EditText text. import android. import android.widget.EditText01).os. Note that the "myClickHandler" will be called basedon the "On Click" property of your button.Activity.RadioButton.6 Code your applicatioin Change your code in "Convert.main). import android.widget. import android. import android.id. text = (EditText) findViewById(R.View.java" to the following.EditText.Toast.net E.Word-to-PDF-Converter.vogella.Unregistered ) http://www.Bundle. setContentView(R. import android. @Override public void onCreate(Bundle savedInstanceState) { super.temperature.layout.view. } // This method is called at button click because we assigned the name to the .app.onCreate(savedInstanceState). package de.

valueOf(convertCelciusToFahrenheit(inputValue))).( Word to PDF Converter .id. } float inputValue = Float.Button01: RadioButton celsiusButton = (RadioButton) findViewById(R.RadioButton02).length() == 0) { Toast. } else { text.parseFloat(text.id.toString()).getId()) { case R.LENGTH_LONG).getText().valueOf(convertFahrenheitToCelcius(inputValue))).makeText( this. return.getText(). Toast.net // "On Click property" of the button public void myClickHandler(View view) { switch (view.RadioButton01).isChecked()) { text. if (celsiusButton.id.Unregistered ) http://www. } // Switch to the other button .show().Word-to-PDF-Converter. "Please enter a valid number". RadioButton fahrenheitButton = (RadioButton) findViewById(R.setText(String . if (text.setText(String .

} } . } else { fahrenheitButton.isChecked()) { fahrenheitButton.setChecked(true).32) * 5 / 9). celsiusButton.setChecked(false).Unregistered ) http://www. celsiusButton.( Word to PDF Converter . } } // Converts to celcius private float convertFahrenheitToCelcius(float fahrenheit) { return ((fahrenheit .net if (fahrenheitButton. } break.Word-to-PDF-Converter.setChecked(false). } // Converts to fahrenheit private float convertCelciusToFahrenheit(float celsius) { return ((celsius * 9) / 5) + 32.setChecked(true).

right click on it.( Word to PDF Converter . the emulator starts up very slow. select your conversion and press the button. E. Type in a number.Unregistered ) http://www.net E.8 Using the home menue If you press the Home button you can also select your application. . The result should be displayed and the other option should get selected.7 Start Project To start the Android Application. Run-As-> Android Application Be patient.Word-to-PDF-Converter. You should get the following result. select your project.

callbacks for list events.android. Overview Lists List can be used to display a scrollable list of items. F. Simple ListActivities A ListActivity extends Activity and simplifies the approach to show several objects in a list. F. To test this create a new Android project "de.Word-to-PDF-Converter.( Word to PDF Converter .1. e. It extends the standard Activity with a standard ListView Elements. You can either use lists in your layout or if the purpose of your Activity is to show primary a list you can extend ListActivities which provides nice hocks for typical actions for lists.listactivity" with the .Unregistered ) http://www.net F.g for selecting a list element and helper methods to access the current list position and the selected element(s).vogella..2.

names)). android. "iPhone"}.onCreate(icicle).R. // Create an array of Strings.ListActivity. import android. "Solaris". "Ubuntu".ListView.setListAdapter(new ArrayAdapter<String>(this. // Create an ArrayAdapter.android.listactivity.layout.Unregistered ) http://www.app. "Eclipse".os. } . import android. You do not need to change the default layout "main.Toast.Bundle.( Word to PDF Converter . package de. Create the following activity.View.vogella. "Windows7".simple_list_item_checked.widget. that will actually make the Strings above // appear in the ListView this. "Android".xml". import android. */ public void onCreate(Bundle icicle) { super. import android. import android. public class MyList extends ListActivity { /** Called when the activity is first created.ArrayAdapter.view.Word-to-PDF-Converter.net activity "MyList". import android.widget.widget. "Suse". that will be put to our ListActivity String[] names = new String[] { "Linux".

View v.getListAdapter(). "You selected: " + keyword.( Word to PDF Converter . int position. id).getItem(position).LENGTH_LONG) .onListItemClick(l. Toast. long id) { super.Word-to-PDF-Converter.makeText(this.show().toString(). position. v.Unregistered ) http://www. Toast. // Get the item that was clicked Object o = this. String keyword = o. } } .net @Override protected void onListItemClick(ListView l.

0" encoding="utf-8"?> .Unregistered ) http://www.( Word to PDF Converter .xml" in the res/layout folder of your project "de. Create the following layout file "rowlayout.android. You can also define your own layout for the rows and assign this layout to your row adapter. <?xml version="1.net F. We will add a graphic to each list entry.listactivity".3.vogella.Word-to-PDF-Converter. ListActivities with own layout The example above is boring. as only text is shown.

the only difference is that we are using our own layout in the ArrayAdapter and telling the adapter which UI element should contains the text.Word-to-PDF-Converter. This is almost the same coding as in the previous example.widget.net <LinearLayout xmlns:android="http://schemas.Activity.widget.ArrayAdapter.android.Unregistered ) http://www.app. import android. import android.( Word to PDF Converter .widget.vogella. import android.listactivity2.ListActivity.ListView.view.View.os. public class MyLayoutList extends ListActivity { . import android.Toast. import android. package de.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/icon" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="22px" android:layout_marginTop="4px" android:layout_marginRight="4px" android:layout_marginLeft="4px"> </ImageView> <TextView android:text="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/label" android:textSize="30px"></TextView> </LinearLayout> Change your activity "MyList" to the following.app.Bundle. import android. import android.

getListAdapter(). Toast. } } . position.LENGTH_LONG) . "iPhone" }.Word-to-PDF-Converter. R. id). "Android". int position. */ public void onCreate(Bundle icicle) { super.show(). // Create an array of Strings.rowlayout.toString().( Word to PDF Converter .layout. "Solaris". R. "Eclipse".getItem(position). "Ubuntu".label. "Windows7".id. long id) { super. String keyword = o.makeText(this.net /** Called when the activity is first created. // Use your own layout and point the adapter to the UI elements which contains the label this. "You selected: " + keyword.setListAdapter(new ArrayAdapter<String>(this.Unregistered ) http://www. "Suse". // Get the item that was clicked Object o = this.onCreate(icicle).onListItemClick(l. } @Override protected void onListItemClick(ListView l. View v. names)). that will be put to our ListActivity String[] names = new String[] { "Linux". Toast. v.

net F.Word-to-PDF-Converter.LayoutInflater.Unregistered ) http://www. In this method we will read the pre-defined layout via LayoutInflator and return one individual view per row.listactivity. If you want to influence the display of the different rows you can also define your own adapter and implement your own getView() method. import android. .app.Activity.view.android. import android.4. ListActivities with flexible layout The above example uses one layout for all rows.java". This method is responsible for creating the listview. package de.( Word to PDF Converter .vogella. Create the following class "MyArrayAdapter.

println(names[position]).startsWith("Windows7") || .names = names.widget.findViewById(R. import android.id.view. null. public class MyArrayAdapter extends ArrayAdapter<String> { private final Activity context. import android. import android.label).( Word to PDF Converter . ViewGroup parent) { LayoutInflater inflater = context.View. label. View rowView = inflater. this. String[] names) { super(context. names). R.inflate(R.view. } @Override public View getView(int position. View convertView.Unregistered ) http://www.layout.rowlayout.net import android.getLayoutInflater().ArrayAdapter.context = context. private final String[] names. TextView label = (TextView) rowView.widget.widget.rowlayout.TextView.Word-to-PDF-Converter.layout. true). public MyArrayAdapter(Activity context. this.ImageView. import android.setText(names[position]).ViewGroup. // Change the icon for Windows and iPhone if (names[position]. System.out.

vogella. // Create an array of Strings.setImageResource(R.findViewById(R.widget.onCreate(icicle). import android. imageView.android. "Suse".( Word to PDF Converter .startsWith("iPhone") ) { ImageView imageView = (ImageView) rowView.Word-to-PDF-Converter. "Ubuntu". import android. */ public void onCreate(Bundle icicle) { super.id. "Android".app.icon). } return rowView. "Eclipse". . "Windows7".net names[position].Bundle. "iPhone"}.ListActivity. import android.Unregistered ) http://www.widget.Toast.drawable.alt_window_16). public class MyList extends ListActivity { /** Called when the activity is first created.ListView.View.os. import android. } } package de.view.listactivity. import android. "Solaris". that will be put to our ListActivity String[] names = new String[] { "Linux".

Unregistered ) http://www. Preferences and Intents .show().net this. } } G.onListItemClick(l.makeText(this. Toast. id).Word-to-PDF-Converter. long id) { super. Toast. "You selected: " + keyword. Menu.LENGTH_LONG) .( Word to PDF Converter . names)). v. position. // Get the item that was clicked Object o = this. View v.getListAdapter(). } @Override protected void onListItemClick(ListView l.toString().setListAdapter(new MyArrayAdapter(this. String keyword = o.getItem(position). int position.

1. right click on it and select New -> Other -> Android -> "Android XML File". how to define preferences and how to navigate between activities via an intent . Add a menu Menus can be defined via XML files.2.android.( Word to PDF Converter .preferences" with the activity "HelloPreferences". Change the UI in the file "/res/layout/main. Select your project. Create a project "de.xml" to the following: <?xml version="1.android. .Word-to-PDF-Converter.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.net G.vogella.Unregistered ) http://www. Project This chapter will demonstrate how to create and evaluate a menu.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Preferences"></Button> </LinearLayout> G.

( Word to PDF Converter .Unregistered ) http://www.Word-to-PDF-Converter. This defines the entries in your menu. We will have only one entry. Maintain the following value.net Press Add and select "Item". .

The OnCreateOptionsMenu method is used to create the menu.app.os.android.Activity.Menu. Please note that at the moment nothing happens if you select this menu.vogella. package de.Bundle. public class HelloPreferences extends Activity { /** Called when the activity is first created. The behavior will be later implemented in the method "onOptionsItemSelected".view.preferences.net Change your class "HelloPreferences" to the following.Word-to-PDF-Converter.( Word to PDF Converter . */ . import android.MenuInflater.Unregistered ) http://www. import android. import android. import android.view.

net @Override public void onCreate(Bundle savedInstanceState) { super. Your menu should be displayed.Word-to-PDF-Converter.menu.layout.main).Unregistered ) http://www. . inflater. return true.( Word to PDF Converter . } } Run your application and press "Menu" on the emulator.inflate(R. menu).onCreate(savedInstanceState). setContentView(R.menu. } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater().

( Word to PDF Converter .net G. . Using preferences Preference values can also be stored as a XML resource.3.xml" this time of type preferences. Create another Android XML File "preferences.Word-to-PDF-Converter.Unregistered ) http://www.

add a category and add two preferences "EditTextPreferences" to this category : "User" and "Password".net Press Add.( Word to PDF Converter .Word-to-PDF-Converter.Unregistered ) http://www. .

.preferences. import android.os.( Word to PDF Converter .vogella.PreferenceActivity.preference. import android. package de.xml".Bundle.net Create the class "Preferences" which will load the "preference.Word-to-PDF-Converter.android.Unregistered ) http://www.

Unregistered ) http://www.xml.xml" and the tab "Application". .net public class Preferences extends PreferenceActivity { /** Called when the activity is first created.( Word to PDF Converter . } } Select "AndroidManifest.preferences). Add the activity "Preferences". addPreferencesFromResource(R.onCreate(savedInstanceState). */ @Override public void onCreate(Bundle savedInstanceState) { super.Word-to-PDF-Converter.

import android.content.net To use the preferences add a button to your main.vogella. import android.Word-to-PDF-Converter.android.preferences.xml with the id "@+id/Button01" and change the coding of HelloPreferences to the following.app.content.Bundle. import android.Unregistered ) http://www.( Word to PDF Converter .Activity. .SharedPreferences.os.Intent. package de. import android.

OnClickListener. /** Called when the activity is first created.view.View. Button button = (Button) findViewById(R. public class HelloPreferences extends Activity { SharedPreferences preferences.getDefaultSharedPreferences(this). import android.view.view. import android.MenuInflater.widget. import android.Toast.Menu. import android.widget.net import android.onCreate(savedInstanceState). */ @Override public void onCreate(Bundle savedInstanceState) { super.Word-to-PDF-Converter.layout.view. import android.Button01).main). setContentView(R.Unregistered ) http://www. // Initialize preferences preferences = PreferenceManager.MenuItem.view.id. import android. import android.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { .Button.preference.( Word to PDF Converter .View. button.PreferenceManager.

show(). } }).Unregistered ) http://www.getItemId()) { // We have only one menu option case R.( Word to PDF Converter .getString("username".net String username = preferences.LENGTH_LONG). "n/a"). return true.preferences: // Launch Preference activity .inflate(R.id.getString("password". Toast. Toast.menu.menu. "You maintained user: " + username + " and password: " + password. String password = preferences. menu).makeText(HelloPreferences. "n/a").Word-to-PDF-Converter.this. inflater. } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(). } // This method is called once the menu is selected @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.

class). startActivity(i). Press the "menu" hardware button and then select your menu item "Preferences".".this.show(). "Here you can maintain your user credentials.Unregistered ) http://www. The saved values should be displayed in a small message windows (Toast).4.makeText(HelloPreferences. } return true. } } G.( Word to PDF Converter .net Intent i = new Intent(HelloPreferences.LENGTH_LONG).this. You should be able to enter your user settings then press the back hardware button to return to your main activity and press the button. Toast. Preferences.Word-to-PDF-Converter. // A toast is a view containing a quick little message for the user. Run Run your application. . break. Toast.

2. H. Overview ContentProvider are used to provide data from an application to another. Create contacts on your emulator Select the home menu and then the menu entry "Contacts" to create contacts. .Word-to-PDF-Converter.( Word to PDF Converter . Content Provider H.net H. ContentProvider do not store the data but provide the interface for other applications to access the data. The following example will use an existing context provider from "Contacts".Unregistered ) http://www.1.

Unregistered ) http://www. .net Press Menu and select "New Contact".( Word to PDF Converter .Word-to-PDF-Converter.

net As a result you should have a few new contacts. .( Word to PDF Converter .Unregistered ) http://www.Word-to-PDF-Converter.

Rename the id of the the existing TextView from the example wizard to "contactview".com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> .contentprovider" with the activity "ContactsView". The resulting main. Delete the default text.android. <?xml version="1.android.3.Word-to-PDF-Converter.net H. Also change the layout_height to "fill_parent".0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.Unregistered ) http://www.vogella. Example Create a new Android project "de.xml should look like the following.( Word to PDF Converter .

( Word to PDF Converter - Unregistered ) http://www.Word-to-PDF-Converter.net
<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/contactview" />


In AndroidManifest.xml add the User Permission that the application can use "android.permission.READ_CONTACTS". Change the coding of the activity.

package de.vogella.android.contentprovider;

import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.widget.TextView;

public class ContactsView extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

( Word to PDF Converter - Unregistered ) http://www.Word-to-PDF-Converter.net
TextView contactView = (TextView) findViewById(R.id.contactview);

Cursor cursor = getContacts();

while (cursor.moveToNext()) {

String displayName = cursor.getString(cursor

.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); contactView.append("Name: "); contactView.append(displayName); contactView.append("\n"); } }

private Cursor getContacts() { // Run query Uri uri = ContactsContract.Contacts.CONTENT_URI; String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }; String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'"; String[] selectionArgs = null; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

( Word to PDF Converter - Unregistered ) http://www.Word-to-PDF-Converter.net

return managedQuery(uri, projection, selection, selectionArgs, sortOrder); }



Scroll View

ScrollViews can be used to contain one child that might be to big to fit on one screen. If the child is to big the ScrollView will display a scroll bar to scroll the context. Of course the child can be a layout which can then contain other elements. Create an android project "de.vogella.android.scrollview" with the activity "ScrollView". Create the following layout and class.

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true">

<LinearLayout android:id="@+id/LinearLayout01" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a header" android:textAppearance="?android:attr/textAppearanceLarge" android:paddingLeft="8dip" android:paddingRight="8dip" android:paddingTop="8dip"></TextView> <TextView android:text="@+id/TextView02" android:id="@+id/TextView02"

onCreate(savedInstanceState).0"></Button> </LinearLayout> </LinearLayout> </ScrollView> package de. .( Word to PDF Converter .TextView.Bundle.0"></Button> <Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" android:layout_weight="1. import android.Word-to-PDF-Converter.scrollview.app. public class ScrollView extends Activity { /** Called when the activity is first created. import android.0"></TextView> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" android:layout_weight="1.widget.Unregistered ) http://www.Activity. import android.View.view.os.android. import android. */ @Override public void onCreate(Bundle savedInstanceState) { super.net android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1.vogella.

TextView02).de ". i++) { s += "vogella.Word-to-PDF-Converter.layout. for (int i=0.setText(s).main). The attribute "android:fillViewport="true"" ensures that the the scrollview is set to the full screen even if the elements are smaller then one screen and the "layout_weight" tell the android system that these elements should be extended. TextView view = String s="". } view. } } (TextView) findViewById(R.net setContentView(R.Unregistered ) http://www. . i < 100.( Word to PDF Converter .id.

Word-to-PDF-Converter. Create the following layout.alarm" with the activity "AlarmActivity".Unregistered ) http://www.vogella.0" encoding="utf-8"?> . Create a new project "de. usually exposed via a Manager class. In this chapter we will use the AlertManager and VibratorManager. The alarm manager will in our example our own broadcast receiver.android.( Word to PDF Converter . <?xml version="1.net J. Services and Broadcast Receivers The Android platform provides a lot of pre-defined services.

import android.Unregistered ) http://www. package de.vogella.Word-to-PDF-Converter. import android. import android.Intent.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/time" android:hint="Number of seconds" android:inputType="numberDecimal"></EditText><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ok" android:onClick="startAlert" android:text="Start Counter"></Button> </LinearLayout> Create the following broadcast receiver class.os.Context. import android.( Word to PDF Converter .widget.alarm. import android.content.Vibrator.BroadcastReceiver.android. This class will get the Vibrator service. public class MyBroadcastReceiver extends BroadcastReceiver { @Override .Toast.android.content.content.net <LinearLayout xmlns:android="http://schemas.

intent.vibrate(2000).VIBRATOR_SERVICE).LAUNCHER" /> .makeText(context.category.AlarmActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.Word-to-PDF-Converter.mf" and allow the vibrate authorization. Toast.intent. "Don't panik but your time is up!!!!. vibrator.MAIN" /> <category android:name="android. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.com/apk/res/android" package="de. Intent intent) { Toast.alarm" android:versionCode="1" android:versionName="1. } } Maintain this class as broadcast receiver in "AndroidManifest.( Word to PDF Converter .LENGTH_LONG).Unregistered ) http://www.vogella. // Vibrate the mobile phone Vibrator vibrator = (Vibrator) context.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".android.android.".getSystemService(Context.action.show().net public void onReceive(Context context.

Unregistered ) http://www. import android. import android. This activity will create an Intent for the Broadcast receiver and get the AlarmManager service.( Word to PDF Converter .Word-to-PDF-Converter.PendingIntent.net </intent-filter> </activity> <receiver android:name=". import android.widget. import android.Activity. package de.content.EditText. .app.os. import android.Intent. import android.view.VIBRATE"></uses-permission> </manifest> Now define your main Activitiy.alarm.MyBroadcastReceiver" android:enabled="true"> </receiver> </application> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.android. import android.View.permission.Bundle.app.vogella.AlarmManager.app.

alarmManager.Word-to-PDF-Converter.makeText(this. int i = Integer.getApplicationContext(). MyBroadcastReceiver.time). public class AlarmActivity extends Activity { /** Called when the activity is first created.Toast.id. Toast.widget.class).RTC_WAKEUP. } } K. Intent intent = new Intent(this.layout. intent.onCreate(savedInstanceState). } public void startAlert(View view) { EditText text = (EditText) findViewById(R. pendingIntent).currentTimeMillis() + (i * 1000). 234324243.parseInt(text. AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE). Important Views . "Alarm set in " + i + " seconds". PendingIntent pendingIntent = PendingIntent.net import android. Toast. */ @Override public void onCreate(Bundle savedInstanceState) { super.show().Unregistered ) http://www.getText().LENGTH_LONG).getBroadcast( this.( Word to PDF Converter . setContentView(R.main). 0).toString()). System.set(AlarmManager.

net K.2. File explorer .print() statements) via the LogCat view. K.Unregistered ) http://www. LogCat View You can see the log (including System.( Word to PDF Converter .Word-to-PDF-Converter.out.1.

Shell L. switch to your "android-sdk" installation directory into the folder "tools".Unregistered ) http://www.1 Android Debugging Bridge . Open a shell.net The file explorer allows to see the files on the android simulator.( Word to PDF Converter .Shell You can access your Android emulator also via the console. L. Start the shell via the .Word-to-PDF-Converter.

adb shell This will connect you to your device and give you Linux command line access to the underlying file system. The application data is stored in the directory "/data/data/package_of_your_app". Use "telnet localhost 5554" to conntect to your simulated device. e.2 Uninstall an application via adb You can uninstall an android application via the shell. # Lists all devices adb devices #Result List of devices attached emulator-5554 attached emulator-5555 attached # Issue a command to a specific device adb -s emulator-5554 shell L. mkdir.Unregistered ) http://www. incoming call. e.3 Emulator Console via telnet Alternatively to adb you can also use telnet to connect to the device. set your current geocodes. etc. use the command "quit" or "exit". This allows you to simulate certain things. If you have several devices running you can issue commands to one individuel device.g. To exit the console session. rm.net following command "adb shell".g.Word-to-PDF-Converter. L. . ls. etc.( Word to PDF Converter . Switch the the data/app directory (cd /data/app) and simply delete your android application. change the network "stability".

.net For example to change the power settings of your phone.Unregistered ) http://www. For details please see Developing on a Device . You also need to install the driver for your mobile phone.( Word to PDF Converter . Please note that the Android version you are developing for must be the installed version on your phone. select "Manual" selection and select your device.Word-to-PDF-Converter. To select your phone. # connects to device telnet localhost 5554 # set the power level power status full power status charging # make a call to the device gsm call 012041293123 # send a sms to the device sms send 12345 Will be home soon # set the geo location For more information on the emulator console please see Emulator Console manual M. then enable USB debugging. select the "Run Configurations". Select in the settings Applications > Development. to receive an sms and to get an incoming call make the following. Deploy Application on Real Devices Turn on "USB Debugging" on your device in the settings.

Unregistered ) http://www. . If you like the information please help me by using flattr or donating or by recommending this tutorial to other people.Word-to-PDF-Converter.( Word to PDF Converter . I maintain this tutorial in my private time.net Thank You Thank you for practicing with this tutorial.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->