P. 1
JaysPatel Android

JaysPatel Android

|Views: 139|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

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

Libraries Running on top of the kernel. network. security. Android include various C/C++ core libraries such as libc and SSL.6 kernel. process and memory management. and power management) are handled by a Linux 2.net Linux Kernel Core services (including hardware drivers. 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 .Unregistered ) http://www.Word-to-PDF-Converter.

debugger and a device emulator as well as its own Java Virtual machine (Dalvik Virtual Machine . a compiler. Application Layer All applications.( Word to PDF Converter . Android Operation System Android is an operating system based on Linux with a Java programming interface. e. Android provides a tool "dx" which allows to . Including the core libraries and the Dalvik virtual machine.Word-to-PDF-Converter. It relies on the Linux kernel for threading and low-level memory management. Android is created by the Open Handset Alliance which is lead by Google. 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. forms the basis for the application framework. The core Android libraries provide most of the functionality available in the core Java libraries as well as the Android-specifi c libraries. B. Application Framework The application framework provides the classes used to create Android applications. the Android run time is the engine that powers your applications and. Android uses a special Java virtual machine (Dalvik) which is based on the Apache Harmony Java implementation. Therefore you cannot run standard Java byte code on Android. Dalvik uses special byte code. It also provides a generic abstraction for hardware access and manages the user interface and application resources.DVM). It provides tools. The application layer runs within the Android run time using the classes and services made available from the application framework. are built on the application layer using the same API libraries. along with the libraries.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.Unregistered ) http://www.g. both native and third party. Core Libraries While Android development is done in Java.

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

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

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

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

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

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

vogella. is missing required source folder: 'gen' 2.Unregistered ) http://www.. Several users report that get the following errors: 1.android. The project could not be built until build path errors are resolved.Word-to-PDF-Converter. .temperature". Your First Android Project E. 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.1 Create Project Tip This app is also available on the Android Marketplace.. Maintain the following. Error Handling Things are not always working as they should be.( Word to PDF Converter . E. Project . Search for "vogella" for find this example. Select File -> New -> Other -> Android -> Android Project and create the Android project "de.net D.

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

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

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

( Word to PDF Converter . String attributes allow to translate the application at a later point.Unregistered ) http://www. .net Add also the following "String" attributes. 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. Table 1.Word-to-PDF-Converter.

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

xml" and verify that your XML looks like the following.Unregistered ) http://www.net Switch to "main. <?xml version="1.android.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" .Word-to-PDF-Converter.( Word to PDF Converter .0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.

5 Maintain UI properties If you select a UI element you can change its properties via the properties view.Unregistered ) http://www.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. Select EditText and change the property "Layout Width" to "fill_parent". .( Word to PDF Converter .

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

Assign "calc" to the text property of your button and assign "buttonHandler" to the "onClick" property.xml" tab and verify that the XML is correctly maintained. .net "fahrenheit" to the second. Set the property "Checked" to true for the first RadioButton. 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. Switch to the "main. Select the complete widget and use the Properties view to set the property "background" to the color attribute "@color/myColor".Unregistered ) http://www.( Word to PDF Converter .

Unregistered ) http://www.net <?xml version="1.( Word to PDF Converter .Word-to-PDF-Converter.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> .android.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.

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

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

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

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

Word-to-PDF-Converter.vogella. callbacks for list events. Simple ListActivities A ListActivity extends Activity and simplifies the approach to show several objects in a list. It extends the standard Activity with a standard ListView Elements.net F.1. To test this create a new Android project "de.. 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. F. Overview Lists List can be used to display a scrollable list of items. F.2.g for selecting a list element and helper methods to access the current list position and the selected element(s). e.( Word to PDF Converter .listactivity" with the .Unregistered ) http://www.android.

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

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

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

widget.os.ArrayAdapter. import android.Unregistered ) http://www.( Word to PDF Converter . 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.vogella.view.listactivity2.ListView.android. import android.widget.widget.Word-to-PDF-Converter.ListActivity. import android. public class MyLayoutList extends ListActivity { . package de.Toast.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. This is almost the same coding as in the previous example.Activity. import android.View.app.app.Bundle.android.net <LinearLayout xmlns:android="http://schemas. import android. import android. import android.

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

view.app. import android.Activity.vogella. package de.Unregistered ) http://www.android.4.java". Create the following class "MyArrayAdapter.net F.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. This method is responsible for creating the listview.Word-to-PDF-Converter. In this method we will read the pre-defined layout via LayoutInflator and return one individual view per row. ListActivities with flexible layout The above example uses one layout for all rows.( Word to PDF Converter .LayoutInflater. import android.

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

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

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

0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas. how to define preferences and how to navigate between activities via an intent .vogella.Word-to-PDF-Converter. .preferences" with the activity "HelloPreferences". Project This chapter will demonstrate how to create and evaluate a menu. Add a menu Menus can be defined via XML files.android. Change the UI in the file "/res/layout/main. Create a project "de.( Word to PDF Converter . Select your project.Unregistered ) http://www. right click on it and select New -> Other -> Android -> "Android XML File".android.1.2.net G.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.xml" to the following: <?xml version="1.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

setText(s). i < 100.Word-to-PDF-Converter.net setContentView(R.TextView02).Unregistered ) http://www.main). i++) { s += "vogella. for (int i=0. TextView view = String s="".de ". } } (TextView) findViewById(R.layout.id. 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. } view. .( Word to PDF Converter .

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

public class MyBroadcastReceiver extends BroadcastReceiver { @Override .content. package de. import android.Intent.android.widget.content. import android.( Word to PDF Converter .content.vogella.Word-to-PDF-Converter. import android. This class will get the Vibrator service.Vibrator.android. import android.BroadcastReceiver.os.net <LinearLayout xmlns:android="http://schemas.Context.alarm.Toast.Unregistered ) http://www.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. import android.

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

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

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

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

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

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

select the "Run Configurations".net For example to change the power settings of your phone. You also need to install the driver for your mobile phone. For details please see Developing on a Device . Select in the settings Applications > Development. To select your phone.( Word to PDF Converter . to receive an sms and to get an incoming call make the following.Word-to-PDF-Converter. Deploy Application on Real Devices Turn on "USB Debugging" on your device in the settings. # 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. 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.Unregistered ) http://www. then enable USB debugging. .

( Word to PDF Converter . I maintain this tutorial in my private time.Unregistered ) http://www.net Thank You Thank you for practicing with this tutorial. 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. .

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)//-->