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

net

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.( Word to PDF Converter .Unregistered ) http://www. Understanding the Android Software Stack .Word-to-PDF-Converter.

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

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

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

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

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

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

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

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

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

Press "Finish". This should create the following directory structure.Word-to-PDF-Converter. Please stare at Android New Project Wizard should have the option to add to Working set to get this functionality.( 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.

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

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

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

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

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

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

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 .

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

</LinearLayout>

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); }

}

I.

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"

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

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

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

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

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

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

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

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

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

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

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

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

Sign up to vote on this title
UsefulNot useful