Vous êtes sur la page 1sur 57

Android development with Android Studio - Tutorial Page 1 of 57

Search

QUICK
Table of Contents LINKS

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 2 of 57

Search

QUICK
LINKS

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 3 of 57

Search

QUICK
LINKS

1. Introduction into development of Android


applications
1.1. The Android operating system

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 4 of 57

Search

QUICK
LINKS
Table 1. Android versions

1.2. How to develop Android applications

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 5 of 57

Search

QUICK
LINKS

1.3. Android Developer Tools and Android Studio

Using Eclipse for Android development

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 6 of 57

Search

QUICK
LINKS

1.4. Conversion process from source code to Android


application

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 7 of 57

Search

QUICK
LINKS

2. Installation of Android Studio


2.1. System requirements

2.2. Requirements for using Linux

CONSOLE

2.3. Download Android Studio

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 8 of 57

Search

QUICK
LINKS

2.4. Installation of Android Studio

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 9 of 57

Search

QUICK
LINKS

3. Exercise: Getting started with Android


Studio

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 10 of 57

Search

QUICK
LINKS

Table 2. Setting for your Android project

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 11 of 57

Search

QUICK
LINKS

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 12 of 57

Search

QUICK
LINKS

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 13 of 57

Search

QUICK
LINKS


3.3. Create a virtual device (AVD)

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 14 of 57

Search

QUICK
LINKS

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 15 of 57

Search

QUICK
LINKS

3.4. Start your virtual device

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 16 of 57

Search

QUICK
LINKS

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 17 of 57

Search

4.1. Using the Android SDK manager QUICK


LINKS

4.2. Install selected Android version or library

4.3. Install support library

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 18 of 57

Search

QUICK
LINKS

5.2. Debug certificate and expire date

5.3. Google vs. Android AVD

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 19 of 57

Search

QUICK
5.4. Speed optimization with GPU rendering LINKS

5.5. Speed optimization with the Intel system image

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 20 of 57

Search

QUICK
LINKS


5.6. Installation of the Intel emulator on Windows

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 21 of 57

Search

QUICK
LINKS
5.7. Installation of the Intel emulator on Linux

5.8. Using a real Android device for testing

6. Software components of an Android


application
6.1. Android application

Table 3. Android application components

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 22 of 57

Search

QUICK
LINKS

6.2. Defining the user interface with fragments, views


and layout managers

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 23 of 57

Search

QUICK
LINKS

6.3. Home screen widgets

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 24 of 57

Search

QUICK
LINKS

7. The Android manifest


7.1. Configuration of your Android application

7.2. Android manifest example

XML

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 25 of 57

Search
xmlns:android "http://schemas.android.com/apk/res/android"
package "com.example.android.rssreader"

android:versionName "1.0" >

-
android:minSdkVersion "16"
android:targetSdkVersion "19" /> QUICK
<uses-permission
android:name "android.permission.INTERNET" />
LINKS
<application
android:name "RssApplication"
android:allowBackup "false"
android:icon "@drawable/ic_launcher"
android:label "@string/app_name"
android:theme "@style/AppTheme" >
<activity
android:name "RssfeedActivity"
android:label "@string/title_activity_main" >
<intent-filter>
<action
android:name "android.intent.action.MAIN" />

<category
android:name "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name ".DetailActivity"
android:label "Details" >
</activity>
<activity android:name "MyPreferenceActivity" >
</activity>

<service android:name "RssDownloadService" >


</service>
</application>

</manifest>

7.3. Version and package

7.4. Application and components

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 26 of 57

Search

QUICK
LINKS

7.5. Minimum and target SDK

Table 4. Minimum and target version

7.6. Permissions

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 27 of 57

Search

QUICK
7.7. Required device configuration LINKS

XML
<uses-configuration android:reqHardKeyboard "true"/>

XML
<uses-feature android:name "android.hardware.camera" />

7.8. Installation location

7.9. More info

8. Resources
8.1. Resource files

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 28 of 57

Search

Table 5. Resources QUICK


LINKS

8.2. Example: Defining strings, string arrays, colors


and dimensions

XML

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 29 of 57

Search
<resources>

<string name "action_settings"> </string>


" ">

<string-array name "operationsystems">


<item>
<item>
</item>
</item>
QUICK
<item>
</string-array>
</item>
LINKS
<color name "red"> </color>

<dimen name "mymargin"> </dimen>

</resources>

8.3. Resource files and R.java

8.4. Layout files

XML

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 30 of 57

Search
xmlns:android "http://schemas.android.com/apk/res/android"
xmlns:tools "http://schemas.android.com/tools"

android:layout_height "match_parent"
" "
"
android:paddingRight "@dimen/activity_horizontal_margin"
android:paddingTop "@dimen/activity_vertical_margin"
tools:context ".MainActivity" >
QUICK
<TextView
LINKS
android:id "@+id/mytext"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:text "@string/hello_world" />

</RelativeLayout>

JAVA
package

import android.os.Bundle
import android.app.Activity
import android.view.Menu

MainActivity

@Override

super

8.5. Performance considerations with layouts

8.6. Good practices for resources IDs

XML

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 31 of 57

Search
android:id "@+id/button1"
android:layout_width "wrap_content"

android:text "Show Preferences" >

QUICK
LINKS

XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name "button1" type "id"/>
</resources>

XML
<RelativeLayout
xmlns:android "http://schemas.android.com/apk/res/android"
xmlns:tools "http://schemas.android.com/tools"
android:layout_width "match_parent"
android:layout_height "match_parent"
tools:context ".MainActivity" >

<Button
android:id "@id/button1"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:layout_centerHorizontal "true"
android:layout_centerVertical "true"
android:layout_marginRight "27dp"
android:text "Button" />

</RelativeLayout>


8.7. System resources

9. Views and layout manager


9.1. View - The user interface widgets in Android

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 32 of 57

Search

QUICK
9.2. Using a layout manager LINKS

9.3. Layout attributes

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 33 of 57

Search

QUICK
LINKS

9.4. Constraint layout

9.5. FrameLayout

9.6. LinearLayout

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 34 of 57

Search

QUICK
LINKS

9.7. RelativeLayout

XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android "http://schemas.android.com/apk/res/android"
android:layout_width "match_parent"
android:layout_height "match_parent"
android:orientation "vertical" >

<ProgressBar
android:id "@+id/progressBar1"
style "?android:attr/progressBarStyleLarge"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:layout_centerInParent "true"
/>

</RelativeLayout>

9.8. GridLayout

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 35 of 57

Search

QUICK
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
XML
LINKS
xmlns:android "http://schemas.android.com/apk/res/android"
android:id "@+id/GridLayout1"
android:layout_width "match_parent"
android:layout_height "match_parent"
android:columnCount "4"
android:useDefaultMargins "true" >

<TextView
android:layout_column "0"
android:layout_columnSpan "3"
android:layout_gravity "center_horizontal"
android:layout_marginTop "40dp"
android:layout_row "0"
android:text "User Credentials"
android:textSize "32dip" />

<TextView
android:layout_column "0"
android:layout_gravity "right"
android:layout_row "1"
android:text "User Name: " >
</TextView>

<EditText
android:id "@+id/input1"
android:layout_column "1"
android:layout_columnSpan "2"
android:layout_row "1"
android:ems "10" />

<TextView
android:layout_column "0"
android:layout_gravity "right"
android:layout_row "2"
android:text "Password: " >
</TextView>

<EditText
android:id "@+id/input2"
android:layout_column "1"
android:layout_columnSpan "2"
android:layout_row "2"
android:inputType "textPassword"
android:ems "8" />

<Button
android:id "@+id/button1"
android:layout_column "2"
android:layout_row "3"
android:text "Login" />

</GridLayout>

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 36 of 57

Search

QUICK
LINKS

9.9. ScrollView

XML

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 37 of 57

Search
<ScrollView
xmlns:android "http://schemas.android.com/apk/res/android"

android:layout_height "match_parent"
" "

<TextView
android:id "@+id/TextView01"
QUICK
android:layout_width "wrap_content"
android:layout_height "wrap_content"
LINKS
android:paddingLeft "8dip"
android:paddingRight "8dip"
android:paddingTop "8dip"
android:text "This is a header"
android:textAppearance "?
android:attr/textAppearanceLarge" >
</TextView>

</ScrollView>

9.10. Add radio group and radio buttons to your


layout

Table 6. ID Assignment

XML

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 38 of 57

Search
xmlns:android "http://schemas.android.com/apk/res/android"
xmlns:tools "http://schemas.android.com/tools"

android:layout_height "match_parent"
" "
"
android:paddingRight "@dimen/activity_horizontal_margin"
android:paddingTop "@dimen/activity_vertical_margin"
tools:context ".MainActivity" >
QUICK
LINKS
<EditText
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:id "@+id/main_input"
android:layout_alignParentTop "true"
android:layout_alignParentStart "true"
android:layout_alignParentEnd "true" />

<Button
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:text "Start"
android:id "@+id/button"
android:layout_below "@id/main_input"
android:layout_alignParentStart "true"
android:onClick "onClick"/>

<RadioGroup
android:id "@+id/orientation"
android:layout_below "@id/button"
android:layout_width "match_parent"
android:layout_height "wrap_content"
android:layout_marginTop "16dp">

<RadioButton
android:id "@+id/horizontal"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:text "Horizontal" >
</RadioButton>

<RadioButton
android:id "@+id/vertical"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:checked "true"
android:text "Vertical" >
</RadioButton>
</RadioGroup>

</RelativeLayout>

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 39 of 57

Search

QUICK
LINKS

9.11. Change radio group orientation dynamically

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 40 of 57

Search

QUICK
LINKS
JAVA

new

@Override

switch
case

break
case

break

9.12. Validating

10. Using and accessing static resources


10.1. References to resources in code

JAVA
// BitmapFactory requires an instance of the Resource class

10.2. Accessing views from the layout in an activity

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 41 of 57

Search

@Override
JAVA QUICK
super
LINKS

// TODO do something with the TextView

JAVA
// search in the layout of the activity

// afterwards search in linearLayout for another view

// note, you could have directly searched for R.id.mytext, the


above coding
// is just for demonstration purposes

JAVA
package

import android.app.Activity
import android.view.View

UiUtils

return

return

JAVA
this

10.3. Reference to resources in XML files

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 42 of 57

Search

QUICK
LINKS
10.4. Using assets?

JAVA
// get the AssetManager

// read the "logo.png" bitmap from the assets folder


null
try
"logo.png"

// assign the bitmap to an ImageView in this layout

catch

finally
if null
try

catch

11. Exercise: Using resources in XML files and


in code
11.1. Add images to your application

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 43 of 57

Search

JAVA

for
QUICK
for LINKS

"@+id/myicon"

"@drawable/ic_offline"

11.3. Replace images via button click

11.4. Validating

12. Exercise: Using ScrollView

XML

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 44 of 57

Search
<ScrollView
xmlns:android "http://schemas.android.com/apk/res/android"

android:layout_height "match_parent"
" "

<LinearLayout
android:id "@+id/LinearLayout01"
QUICK
android:layout_width "match_parent"
android:layout_height "wrap_content"
LINKS
android:orientation "vertical" >

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

<TextView
android:id "@+id/TextView02"
android:layout_width "wrap_content"
android:layout_height "match_parent"
android:layout_weight "1.0"
android:text "@+id/TextView02" >
</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:layout_weight "1.0"
android:text "Submit" >
</Button>

<Button
android:id "@+id/Button02"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:layout_weight "1.0"
android:text "Cancel" >
</Button>
</LinearLayout>
</LinearLayout>

</ScrollView>

JAVA

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 45 of 57

Search

import android.app.Activity

import android.view.View

ScrollViewActivity
@Override QUICK
super
LINKS
""
for 0 500
"vogella.com "

13. Exercise: Create a temperature converter

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 46 of 57

Search

QUICK
LINKS
13.1. Create Project

Table 7. New Android project

13.2. Create attributes

Table 8. New attributes to add

XML

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 47 of 57

Search
<resources>

<string name "action_settings"> </string>


" ">
name
<string name "celsius"> </string>
<string name "fahrenheit">
<string name "calc"> </string>
</string> QUICK
</resources>
LINKS

13.3. Creating the layout

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 48 of 57

Search

QUICK
LINKS


XML

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 49 of 57

Search
xmlns:android "http://schemas.android.com/apk/res/android"
xmlns:tools "http://schemas.android.com/tools"

android:layout_height "match_parent"
" "
"
android:paddingLeft "@dimen/activity_horizontal_margin"
android:paddingRight "@dimen/activity_horizontal_margin"
android:paddingTop "@dimen/activity_vertical_margin"
QUICK
tools:context ".MainActivity"
android:background "@color/myColor">
LINKS
<EditText
android:layout_width "match_parent"
android:layout_height "wrap_content"
android:id "@+id/editText1" />

<RadioGroup
android:id "@+id/radioGroup1"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:layout_alignStart "@+id/editText1"
android:layout_below "@+id/editText1">

<RadioButton
android:id "@+id/radio0"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:checked "true"
android:text "RadioButton" />

<RadioButton
android:id "@+id/radio1"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:text "RadioButton" />
</RadioGroup>

<Button
android:id "@+id/button1"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:layout_alignStart "@+id/radioGroup1"
android:layout_below "@+id/radioGroup1"
android:layout_marginTop "22dp"
android:text "Button" />

</LinearLayout>


13.4. Edit view properties

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 50 of 57

Search

QUICK
LINKS

JAVA

"@+id/inputValue"
"match_parent"
"wrap_content"
"true"
"@+id/textView"
"10"
"numberSigned|numberDecimal"

JAVA

"http://schemas.android.com/apk/res/android"
"http://schemas.android.com/tools"
"match_parent"
"match_parent"
"vertical"
"@dimen/activity_vertical_margin"
"@dimen/activity_horizontal_margin"
"@dimen/activity_horizontal_margin"
"@dimen/activity_vertical_margin"
".MainActivity"
"@color/myColor"

JAVA

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 51 of 57

Search
"http://schemas.android.com/apk/res/android"
"http://schemas.android.com/tools"

"match_parent"
" "
"
"@dimen/activity_horizontal_margin"
"@dimen/activity_horizontal_margin"
"@dimen/activity_vertical_margin"
QUICK
".MainActivity"
"@color/myColor"
LINKS

"match_parent"
"wrap_content"
"@+id/inputValue"
"numberSigned|numberDecimal"

"@+id/radioGroup1"
"wrap_content"
"wrap_content"
"@+id/editText1"
"@+id/editText1"

"@+id/radio0"
"wrap_content"
"wrap_content"
"true"
"@string/celsius"

"@+id/radio1"
"wrap_content"
"wrap_content"
"@string/fahrenheit"

"@+id/button1"
"wrap_content"
"wrap_content"
"@+id/radioGroup1"
"@+id/radioGroup1"
"22dp"
"@string/calc"
"onClick"

13.5. Create utiliy class

JAVA

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 52 of 57

Search

ConverterUtil

// converts to fahrenheit
QUICK
LINKS
return 9 5 32

13.6. Change the activity code

JAVA

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 53 of 57

Search

import android.app.Activity

import android.view.View

import android.widget.Toast

MainActivity
QUICK
LINKS
@Override

super

// this method is called at button click because we assigned


the name to the
// "OnClick" property of the button

switch
case

if 0
this "Please enter a valid number"

return

if

false
true
else

false
true

break

13.7. Start the application

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 54 of 57

Search

QUICK
LINKS

14. Deployment
14.1. How to deploy

14.2. Defining software and hardware requirements


for the application

14.3. Signing your application for the release

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 55 of 57

Search

14.5. Export your application via the Eclipse IDE QUICK


LINKS

14.6. Via external sources

14.7. Google Play (Market)

15. About this website


Support free content Questions and Tutorial & code Get source code
discussion license

16. Android online resources

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 56 of 57

Search

QUICK
LINKS

16.2. Android learning resources

16.3. Android tools

16.4. vogella GmbH training and consulting support

http://www.vogella.com/tutorials/Android/article.html 4/11/2016
Android development with Android Studio - Tutorial Page 57 of 57

Search

QUICK
LINKS

http://www.vogella.com/tutorials/Android/article.html 4/11/2016