Android Development Tutorial | Eclipse (Software) | Android

October 30, 2017 | Author: Anonymous | Category: IDE
Share Embed


Short Description

It is based on Eclipse 3.7 (Indigo), Java 1.6 .... For example if you open the "res/layout/main.xml" file in the Package...

Description

http://www.vogella.com/articles/An http://www.vogella.com/articles/Android/article.htm droid/article.htmll

Home

Tuto rials

Trainings

Boo ks

Co nnect

Android Development Tutorial Based on Android 4.0 

Lars Vogel Version 10.3 by Lars Vogel 

Tutorial 2.8k

Copyright © 2009, 2010, 2011, 2012 Lars Vogel 17.04.2012 Revision History Revision 0.1

04.07.2009

Created Revision 0.2 - 10.3

07.07.2009 - 17.04.2012

bug fixing and enhancements

Development Development with Android and Eclipse This tutorial describes how to create Android applications with Eclipse. It is based on Eclipse 3.7 (Indigo), Java Java 1.6 and Android 4.0 (Ice Cream Sandwich).

Table of Contents 1. What is Android? 1.1. Android Operation System 1.2. Google Play (Android Market) 1.3. Security and permissions 2. Android components 2.1. Activity 2.2. Views and ViewGroups 2.3. Intents 2.4. Services 2.5. ContentProvider 2.6. BroadcastReceiver 2.7. (HomeScreen) Widgets W idgets 2.8. Other 3. Android Development Tools 3.1. What are the Android Development Tools? 3.2. Dalvik Virtual Machine 3.3. How to develop Android Applications 3.4. Resource editors e ditors 3.5. Logging 4. Android Application Architecture 4.1. AndroidManifest.xml 4.2. R.java and Resources 4.3. Assets 4.4. Activities and Layouts 4.5. Reference to resources in XML files 4.6. Activities and Lifecycle 4.7. Configuration Change 4.8. Context 5. Installation 5.1. Eclip E clipse se 5.2. Pre-requisites for using a 64bit Linux 5.3. Install ADT Plug-ins and Android SDK

http://www.vogella.com/articles/An http://www.vogella.com/articles/Android/article.htm droid/article.htmll

Home

Tuto rials

Trainings

Boo ks

Co nnect

5.6. Android Source Code 6. Android virtual device - Emulator 6.1. What is the Android Emulator? 6.2. Google vs. Android AVD 6.3. Emulato E mulatorr Shortcuts 6.4. Performance 6.5. Hardware button 7. Tutorial: Create and run Android Virtual Device 8. Error handling and typical problems 8.1. Workspace 8.2. Clean Project 8.3. Problems w ith Android Android Debug Bridge (adb) 8.4. LogCat 8.5. Android editor not opened 8.6. Emulator does not start 8.7. Timeout during deployment 8.8. Installation failed due to insufficient storage 8.9. Debug Certifi C ertificate cate expired 8.10. Error message for @override @ove rride 8.11. Missing Imports 8.12. Eclipse Tips 9. Conventions C onventions for the tutorials 9.1. API version, package and application name 9.2. Warning messages for Strings 10. Tutorial: Your first Android project 10.1. Install the demo application 10.2. Create Project 10.3. Modifying resources 10.4. Create attributes attributes 10.5. Add Views 10.6. Edit View properties properties 10.7. Change the Activity source code 10.8. Start Project 11. Starting an installed application 12. OptionMenu and ActionBar 12.1. ActionBar 12.2. OptionsMenu 12.3. Creating the menu 12.4. Reacting to menu entry selection 12.5. Using the home icon 12.6. ActionBar tabs 12.7. Custom Views View s in the ActionBar 12.8. Contextual action mode 12.9. Context menus 13. Tutorial: ActionBar 13.1. Project 13.2. Add a menu XML resource 14. Tutorial: Using the contextual action mode 15. Layout Manager and ViewGroups 15.1. Available Layout Manager 15.2. LinearLayout 15.3. RelativeLayout RelativeLayout 15.4. Gridlayout 15.5. ScrollView ScrollView 16. Tutorial: ScrollView 17. Drawables 17.1. Drawables 17.2. Shape Drawables 17.3. State Drawables 17.4. Transition Drawables 17.5. 9 Patch Drawables 18. Styles and Themes 18.1. Styles 18.2. Attributes Attributes 18.3. Themes 18.4. Android Design Page 19. Supporti S upporting ng different screen sizes s izes 19.1. Using Us ing device independent pixel 19.2. Using resource qualifiers 20. Fragments 20.1. Fragments Overview 20.2. When to use Fragments

http://www.vogella.com/articles/An http://www.vogella.com/articles/Android/article.htm droid/article.htmll

Home

Tuto rials

Trainings

Boo ks

Co nnect

5.6. Android Source Code 6. Android virtual device - Emulator 6.1. What is the Android Emulator? 6.2. Google vs. Android AVD 6.3. Emulato E mulatorr Shortcuts 6.4. Performance 6.5. Hardware button 7. Tutorial: Create and run Android Virtual Device 8. Error handling and typical problems 8.1. Workspace 8.2. Clean Project 8.3. Problems w ith Android Android Debug Bridge (adb) 8.4. LogCat 8.5. Android editor not opened 8.6. Emulator does not start 8.7. Timeout during deployment 8.8. Installation failed due to insufficient storage 8.9. Debug Certifi C ertificate cate expired 8.10. Error message for @override @ove rride 8.11. Missing Imports 8.12. Eclipse Tips 9. Conventions C onventions for the tutorials 9.1. API version, package and application name 9.2. Warning messages for Strings 10. Tutorial: Your first Android project 10.1. Install the demo application 10.2. Create Project 10.3. Modifying resources 10.4. Create attributes attributes 10.5. Add Views 10.6. Edit View properties properties 10.7. Change the Activity source code 10.8. Start Project 11. Starting an installed application 12. OptionMenu and ActionBar 12.1. ActionBar 12.2. OptionsMenu 12.3. Creating the menu 12.4. Reacting to menu entry selection 12.5. Using the home icon 12.6. ActionBar tabs 12.7. Custom Views View s in the ActionBar 12.8. Contextual action mode 12.9. Context menus 13. Tutorial: ActionBar 13.1. Project 13.2. Add a menu XML resource 14. Tutorial: Using the contextual action mode 15. Layout Manager and ViewGroups 15.1. Available Layout Manager 15.2. LinearLayout 15.3. RelativeLayout RelativeLayout 15.4. Gridlayout 15.5. ScrollView ScrollView 16. Tutorial: ScrollView 17. Drawables 17.1. Drawables 17.2. Shape Drawables 17.3. State Drawables 17.4. Transition Drawables 17.5. 9 Patch Drawables 18. Styles and Themes 18.1. Styles 18.2. Attributes Attributes 18.3. Themes 18.4. Android Design Page 19. Supporti S upporting ng different screen sizes s izes 19.1. Using Us ing device independent pixel 19.2. Using resource qualifiers 20. Fragments 20.1. Fragments Overview 20.2. When to use Fragments

http://www.vogella.com/articles/An http://www.vogella.com/articles/Android/article.htm droid/article.htmll

Home

Tuto rials

Trainings

Boo ks

Co nnect

21.2. Create project 21.3. Create layouts layouts for portrait mode 21.4. Create Fragment classes 21.5. Create layouts layouts for landscape mode 21.6. Activities Activities 21.7. Run 22. ActionBar navigation with w ith Fragments 23. DDMS perspective and important views 23.1. DDMS - Dalvik Debug Monitor Server 23.2. LogCat View 23.3. File explorer 24. Shell 24.1. Android Debugging Bridge - Shell 24.2. Uninstall an application via adb 24.3. Emulator Console via telnet 25. Deployment 25.1. Overview 25.2. Deploy De ployment ment via Eclipse 25.3. 25.4. Via external sources 25.5. Google Play (Market) 26. Thank you 27. Questions and Discussion 28. Links and Literature 28.1. Source Code 28.2. Android Android Resources 28.3. vogella Resources

1. What is Android? 1.1. Android Operation System Android is an operating system based on Linux with a Java programming programming int erface. The Android Software Development Development Kit (Android SDK) provides all necessary tools to develop Android applications. This includes a compiler, debugger and a device emulator, as well as its own virtual machine to run Android programs. Android is currently primarily developed by Google. Android allows background processing, provides a rich user interface library, supports 2-D and 3-D graphics using the OpenGL libraries, access to the file system and provides an embedded SQLite database. Android applications consist of different components and can re-use components of other applications. This leads to the concept of a task in Android; an application can re-use other Android components to archive a task. For example you can write an application which use the Android Gallery application to pick a photo.

1.2. Google Play (Android Market) Google offers the "Google Play" service in which programmers can offer their Android application to Android users. Google phones include the "Google Play" application which allows to install applications. Google Play also offers an update service, e.g. if a programmer uploads a new version of his application to Google Play, this service will notify existing users that an update is avaiable and allow to install it. Google Play used to be called "Android Market".

1.3. Security and permissions

http://www.vogella.com/articles/An http://www.vogella.com/articles/Android/article.htm droid/article.htmll

Home

Tuto rials

Trainings

Boo ks

Co nnect

every Android application. Each application file is private to this generated user, e.g. other applications cannot access these files. In addition each Android application will be started in its own process. Therefore by means of the underlying Linux operating system, every Android application is isolated from other running applications. If data should be shared, the application must do this explicitly, e.g. via a Service or a ContentProvider.

Android also contains a p ermission ermission system. Android pred efines permissions for certain tasks but every every application can define additional permissions. An Android application declare its required permissions in its AndroidManifest.xml configuration file.For example an application may declare that it requires Internet Permissions Permissions have different l evels. evels. Some permissions are automatically granted by the Android system, some are automatically rejected. In most cases the requested permissions will be presented to the user before installation of the application. The user needs to decided if these permissions should be given to the application. If the user denies a permission required by the application, this application cannot be installed. The check of the permission is only performed during installation, permissions cannot be denied or granted after the installation. While not all users pay attention to the required permissions during installation some some users do and they write bad reviews reviews on Google Googl e Play.

2. Android components The following gives a short overview overview of the most important Android components.

2.1. Activity Activity represents the presentation layer of an Android application. A simplified description is that an Activity represents a screen in your Android application. This is slightly incorrect as Activities can

be displayed as Dialogs or can be transparent. An Android appl ication can have several Activities.

2.2. Views and ViewGroups Views are user interface widgets, e.g. buttons or text fields. The base class for all Views is android.view.View. Views often have attributes which can be used to change their appearance and

behavior. A ViewGroup is responsible for arranging other Views. A ViewGroup is also called layout manager. The base class for a layout manager is android.view.ViewGroups which extends extends View. ViewGroups can be nestled to create complex layouts. You should not nestle ViewGroups too deeply

as this has a n egative impact on the p erformance.

2.3. Intents Intents are asynchronous messages which allow the application to request functionality from other

components of the Android system, e.g. from Services or Activities. An application can call a

http://www.vogella.com/articles/An http://www.vogella.com/articles/Android/article.htm droid/article.htmll

Home

Tuto rials

Trainings

Boo ks

Co nnect

certain Intent (implicit Intents ). For example the application could implement sharing of data via an Intent and all components which allow sharing of data would be available for the user to select.

Applications register themselves to an Intent via an IntentFilter. Intents allow to combine loosely coupled components to perform certain tasks.

2.4. Services Services perform background tasks without providing a user interface. They can notify the user via the

notification framework in Android.

2.5. ContentProvider ContentProvider provides a structured interface to application data. Via a ContentProvider your

application can share data with other applications. Android contains an SQLite database which is frequently used in conjunction with a ContentProvider to persist the data of the ContentProvider.

2.6. BroadcastReceiver BroadcastReceiver can be registered to receive system messages messages and Intents. A BroadcastReceiver will get notified by the Android system, if the specified situation happens. For

example a BroadcastReceiver could get called once the Android system completed the boot process or if a ph one call is received. received.

2.7. (HomeScreen) Widgets Widgets are interactive components components which are primarily used on t he Android homescreen. They

typically display some kind of data and allow the user to perform actions via them. For example a Widget could display a short summary of new emails and if the user selects an email, it could start the

email application with the selected email.

2.8. Other Android provide many more components but the list above describes the most important ones. Other Android components are "Live Folders" and "Live Wallpapers". Live Folders display data on the homescreen without launching the corresponding application.

3. Android Development Tools 3.1. What are the Android Development Tools? Google provides the Android Development Tools Tools (ADT) (ADT) to d evelop evelop Android appl ications with Eclipse. ADT is a set of components (plug-ins) which extend the Eclipse IDE with Android development capabilities. ADT contains all required functionalities to create, compile, debug and deploy Android applications from the Eclipse IDE and from the command line. Other IDE's, e.g. IntellJ, are also reusing components of ADT. ADT ADT also provides an Android device emulator, so that Android appl ications can be tested without a real Android phone.

3.2. Dalvik Virtual Machine The Android system uses a special virtual machine, i.e. the Dalvik Virtual Virtual Machine to ru n Java based applications. Dalvik uses an own b ytecode format which is different from Java Java bytecode.

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

bytecode format.

3.3. How to develop Android Applications Android applications are primarily written in the Java programming language. The Java source files are converted to Java class files by the Java compiler. Android provides a tool called "dx"" which converts Java class files into a dex (Dalvik Executable) fil e. All class files of one application are placed in one compressed .dex file. During this conversion process redundant information in the class files are optimized in the .dex file. For example if the same String is found in different class files, the .dex file contains only once reference of this String. These dex files are therefore much smaller in size than the corresponding class files. The .dex file and the resources of an Android project, e.g. the images and XML files, are packed into an .apk (Android Package) file. The program aapt (Android Asset Packaging Tool) performs this

packaging. The resulting .apk file contains all necessary data to run the Android application and can be deployed to an Android device via the "adb" tool. The Android Development Tools (ADT) allows that all these steps are performed transparentl y to the user; either within Eclipse or via the command line. If you use the ADT tooling you press a button or run a script and the whole Android application (.apk file) will be created and deployed.

3.4. Resource editors The ADT allows the developer to define certain artifacts, e.g. Strings and layout files, in two ways: via a rich editor, and directly via XML. This is done via multi-page editors in Eclipse. In th ese editors you can switch between both representations by clicking on the tab on the lower part of the screen. For example if you open the "res/layout/main.xml" file in the Package Explorer, you can switch between the two representations as depicted in the following screenshot.

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

3.5. Logging Android uses the android.util.Log class for logging with the Log.i(), Log.w() and Log.e() and Log.wtf() methods for logging. This list is sorted by severence.

The first parameter of these method is the category and the second is the message. Typically you create a Constants class in your Android application and provide your log flag as a public static final field.           

Android advises that a deployed application should not contain logging code. The Android development tools provide the BuildConfig.DEBUG flag for this purpose. This flag will be automatically set to false, if you export the Android application for deployment. During development it will be set to true, therefore allow you to see your logging statements during development. The following example show how to write an error log message. This message is visible in th e "LogCat" view in Eclipse. !"#$  %$ 

4. Android Application Architecture 4.1. AndroidManifest.xml The components and settings of an Android application are described in the file

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

declared in this file. It must also contain the required permissions for the application. For example if the application requires network access it must be specified h ere. &'()*+,-'. &) ()/0/110))111 ) /* /2)*+. & /341 /315). &6 /) /315). &,. & /)78921. &6 /)68#2:";1. &1,. &16. &1. &, /)00&1. & ).&1. &1.

10.5. Add Views Select "res/layout/main.xml" and open the Android editor via a double-click. This editor allows you to create the layout via drag and drop or via the XML source code. You can switch between both representations via the tabs at the bottom of the editor. For changing the position and grouping elements you can use the Eclipse "Outline" view. The following shows a screenshot of the "Palette" view from which you can drag and drop new user interface components into your layout. Please note that the "Palette" view changes frequently so your view might be a bit different.

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

You will now create your new layout. Right-click on the existing text object “Hello World, Hello!” in the layout. Select "Delete" from the popup menu to remove the text object. Then, from the “Palette” view, select Text Fields and locate "Plain Text". Drag this onto the layout to create a text input field. All object types in the section "Text Fields” derive from the class "EditText", they just specify via an additional attribute which text type can be used. Afterwards select the Palette section "Form Widgets" and drag a “RadioGroup” object onto the layout. The number of radio buttons added to the radio button group depends on your version of Eclipse. Make sure there are two radio buttons by deleting or adding radio buttons to the group. From the Palette section Form Widgets, drag a Button object onto the layout. The result should look like the following.

Switch to "main.xml" and verify that your XML looks like the following. &'()*+,-'. &6 ()/0/110))111 /65405 /65005

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

&"?( /3@1?(* /6540)05 /650045 /("?(. &1"?(. &; /3@1* /6540)05 /650045. &; /3@1+ /654045 /650045 /0 /(;. &1;. &; /3@1* /654045 /650045 /(;. &1;. &1;. & /3@1* /654045 /650045 /(. &1. &16.

10.6. Edit View properties If you select a user interface component (an instance of View ), you can change its properties via the Eclipse "Properties" view. Most of the properties can be chang ed via the right mouse menu. You can also edit properties of fields directly in XML. Changing properties in the XML file is much faster, if you know what you want to change. But the right mouse functionality is nice, if you are searching for a certain property. Open your file "main.xml". The EditText control shows currently a default text. We want to delete this initial text in the XML code. Switch to the XML tab called "main.xml" and delete the android:text="EditText" property from the EditText part. Switch back to the "Graphical Layout" tab

and check th at the text is removed. Use the right mouse click on the first radio button to assign the "celsius" String attribute to its "text" property. Assign the "fahrenheit" string attribute to the second radio button.

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

From now on, I assume you are able to use the properties menu on user interface components. You can always either edit the XML file or modify the properties via right mouse click. Set the property "Checked" to true for the first RadioButton. Assign "calc" to the text property of your button and assign "myClickHandler" to the "onClick" property. Set the "Input type" property to "numberSigned" and "numberDecimal" on your EditText. All your user interface components are contained in a LinearLayout. We want to assign a background color to this LinearLayout. Right-click on an empty space in Graphical Layout mode, then select Other Properties displayed.



All by Name



Background. Select “Color” and then select "myColor" "in the list which is

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

Switch to the "main.xml" tab and verify that the XML is correct. &'()*+,-'. &6 ()/0/110))111 /65405 /65005 /31)6 /. &"?( /3@1?(* /6540)05 /650045 /?6)!)B). &1"?(. &; /3@1* /6540)05 /650045. &; /3@1+ /654045 /650045 /0 /(31. &1;. &; /3@1* /654045 /650045 /(3100. &1;. &1;. & /3@1* /654045 /650045 /)6: /(31. &1. &16.

10.7. Change the Activity source code During the generation of your new Android project you specified that an Activity called ConvertActivity should be created. The project wizard created the corresponding Java class.

Change your code in ConvertActivity.java to the following. Note that the myClickHandler will be called based on the OnClick property of your button. ) 

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

) ) )400?0000?  00$  00,CD$HI1=$    1100   ?>00 $   H=$1I$@CD   

10.8. Start Project To start the Android Application, select your project, right click on it, and select Run-As



Android

Application. If an emulator is not yet running, it will be started. Be patient, the emulator starts up very slowly. You should get the following result.

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

Type in a number, select your conversion and press the button. The result should be displayed and the other option should get selected.

11. Starting an installed application After you run your application on the virtual device, you can start it again on the device. If you press the "Home" button you can select your application.

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

Download Google Chrome Searching is fast and easy with Google's web browser. www.Google.com/Chrome Hire Android Developers Hire us for unmatched cost & time to market. Free Quote within a day. www.evontech.com Android eBird App Enter your Sightings in the Field… The Power of eBird in your Pocket! market.android.com

12. OptionMenu and ActionBar 12.1. ActionBar The ActionBar is located at the top of the Activity that may display the Activity title, navigation modes, and other interactive items. The following picture show the ActionBar of a typical Google Application with interactive items and a nagivation bar.

12.2. OptionsMenu The application can also open a menu which shows actions via a popup menu. This OptionsMenu is only available if the phone has a hardware "Options" button. Even if the hardware button is available, it is recommended to use the ActionBar, which is available for phones as of Android 4.0. The following picture highlights the hardware button and the resulting menu as popup.

http://www.vogella.com/articles/Android/article.html

Home

Tutorials

Trainings

Books

Connect

One of the reasons why the ActionBar is superior to the OptionsMenu, if that it is clearly visible, while the OptionsMenu is only shown on request and the user may not recognize that options are available.

12.3. Creating the menu The OptionsMenu and the ActionBar is filled by the onCreateOptionsMenu() method of your Activity.

In the onCreateOptionsMenu() method you can create the menu entries. You can add menu entries via code or via the inflation of an existing XML resources. The MenuInflator class allows to inflate menu entries defined in XML to the menu. MenuInflator can get accessed via the getMenuInflator() method in your Activity. The onCreateOptionsMenu() method is only called once. If you want to influence the menu later you have to use the onPrepareOptionsMenu() method. onPrepareOptionsMenu() is not called for entries in the ActionBar for these entries you have to use the invalidateOptionsMenu() method.

12.4. Reacting to menu entry selection If a menu entry is selected then then onOptionsItemSelected() method is called. As parameter you receive the menu entry which was selected so that you can react differently to different menu entries.

12.5. Using the home icon The ActionBar also shows an icon of your application. You can also add an action to this icon. If you select this icon the onOptionsItemSelected() method will be called with the value android.R.id.home. The recommendation is to return to the main Activity in your program.

 1190))86  ;0)/  94 490 0%486 $ 4 0  >9>858?9
View more...

Comments

Copyright © 2017 DATENPDF Inc.