Android Introduction - PowerPoint PPT Presentation

About This Presentation
Title:

Android Introduction

Description:

android:text This sets the text that the TextView should display. In this example, you use a string resource instead of a hard-coded string value. – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 23
Provided by: lecturerE
Category:

less

Transcript and Presenter's Notes

Title: Android Introduction


1
Android Introduction
  • Hello World

2
Tutorial
  • http//developer.android.com/resources/tutorials/h
    ello-world.html
  • http//mobiforge.com/developing/story/getting-star
    ted-with-android-development?dm_switchertrue

3
Create a New Android Project
  • From Eclipse, select File gt New gt Android
    Project.
  • Fill in the project details with the following
    values
  • Project name HelloAndroid
  • Application name Hello, Android
  • Package name com.example.helloandroid (or your
    own private namespace)
  • Create Activity HelloAndroid
  • Click Finish

4
Create a New Android Project
5
Create a New Android Project
  • Project name - the name of the project
  • Package name - the name of the package. This name
    will be used as the package name in your Java
    files. Package name must be fully qualified. The
    convention is to use your company's domain name
    in reverse order
  • Activity name - the name of the activity in your
    Android application. In Android, think of an
    activity as a screen containing some actions,
    hence the name "activity"
  • Application name - the user-friendly name of the
    application that will be displayed in the
    Applications tab of the Android UI

6
Package Content
Java code for our activity
All source code here
Generated Java code Helps link resources to Java
code
All non-code resources
Layout of the activity
Images
Strings used in the program
Android Manifest
7
the various fields when create a new Android
project
  • First, the src folder contains your Java source
    files. The HelloAndroid.java file is the source
    file for the HelloAndroid activity you specified
    when you created the project earlier.
  • The R.java file is a special file generated by
    the ADT to keep track of all the names of views,
    constants, etc, used in your Android project. You
    should not modify the content of this file as its
    content is generated automatically by the ADT.
  • The Android Library contains a file named
    android.jar. This file contains all the classes
    that you would use to program an Android
    application.
  • The res folder contains all the resources used by
    your Android application. For example, the
    drawable folder contains a png image file that is
    used as the icon for your application. The layout
    folder contains an XML file used to represent the
    user interface of your Android application. The
    values folder contains an XML file used to store
    a list of string constants.
  • The AndroidManifest.xml file is an application
    configuration file that contains detailed
    information about your application, such as the
    number of activities you have in your
    application, the types of permissions your
    application needs, the version information of
    your application, and so on.

8
HelloAndroid.java
  • Open the HelloAndroid.java file, located inside
    HelloAndroid gt src gt com.example.helloandroid)
  • package com.example.helloandroid
  • import android.app.Activity
  • import android.os.Bundle
  • public class HelloAndroid extends Activity
  • / Called when the activity is first
    created. /
  • _at_Override
  • public void onCreate(Bundle
    savedInstanceState)
  • super.onCreate(savedInstanceState)
  • setContentView(R.layout.main)

9
HelloAndroid.java
  • The basic unit of an Android application is an
    Activity.
  • An Activity displays the user interface of your
    application, which may contain widgets like
    buttons, labels, text boxes, etc
  • When the activity is loaded, the onCreate() event
    handler is called.
  • The activity loads its UI from the XML file named
    main.xml. This is represented by the constant
    named R.layout.main (generated automatically by
    the Eclipse as you save your project).
  • If you examine the main.xml file located in the
    res/layout folder

10
Run the Application
  • The Eclipse plugin makes it easy to run your
    applications
  • Select Run gt Run.
  • Select "Android Application".

11
/res/layout/main.xml
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltLinearLayout xmlnsandroid"http//schemas.androi
    d.com/apk/res/android"
  • androidorientation"vertical"
  • androidlayout_width"fill_parent"
  • androidlayout_height"fill_parent"
  • gt
  • ltTextView
  • androidlayout_width"fill_parent"
  • androidlayout_height"wrap_content"
  • androidtext"_at_string/hello"
  • /gt
  • lt/LinearLayoutgt

12
XML attributes
  • xmlnsandroid
  • This is an XML namespace declaration that tells
    the Android tools that you are going to refer to
    common attributes defined in the Android
    namespace. The outermost tag in every Android
    layout file must have this attribute.
  • androidid
  • This attribute assigns a unique identifier to
    the TextView element. You can use the assigned ID
    to reference this View from your source code or
    from other XML resource declarations.
  • androidlayout_width
  • This attribute defines how much of the available
    width on the screen this View should consume. In
    this case, it's the only View so you want it to
    take up the entire screen, which is what a value
    of "fill_parent" means.

13
XML attributes
  • androidlayout_height
  • This is just like androidlayout_width, except
    that it refers to available screen height.
  • androidtext
  • This sets the text that the TextView should
    display. In this example, you use a string
    resource instead of a hard-coded string value.
    The hello string is defined in the
    res/values/strings.xml file. This is the
    recommended practice for inserting strings to
    your application, because it makes the
    localization of your application to other
    languages graceful, without need to hard-code
    changes to the layout file.

14
/res/values/strings.xml
  • In Android, the UI of each activity is
    represented using various objects known as Views.
    You can create a view using code, or more simply
    through the use of an XML file.
  • In this case, the UI Is represented using an XML
    file.
  • The ltTextViewgt element represents a text label on
    the screen while the ltLinearLayoutgt element
    specifies how views should be arranged.
  • Notice that the ltTextViewgt element has an
    attribute named androidtext with its value set
    to "_at_string/hello".
  • The _at_string/hello refers to the string named
    hello defined in the strings.xml file in the
    res/values folder.
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltresourcesgt
  • ltstring name"hello"gtHello World,
    HelloAndroid!lt/stringgt
  • ltstring name"app_name"gtHelloAndroidlt/stringgt
  • lt/resourcesgt

15
Modify strings.xml
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltresourcesgt
  • ltstring name"hello"gtHello, Android! I am a
    string resource!lt/stringgt
  • ltstring name"app_name"gtHello,
    Androidlt/stringgt
  • lt/resourcesgt

16
Run it !
17
Modify the main.xml
  • Let's now modify the main.xml file. Add the
    following ltButtongt element
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltLinearLayout xmlnsandroid"http//schemas.androi
    d.com/apk/res/android"
  • androidorientation"vertical"
  • androidlayout_width"fill_parent"
  • androidlayout_height"fill_parent"
  • gt
  • ltTextView
  • androidlayout_width"fill_parent"
  • androidlayout_height"wrap_content"
  • androidtext"_at_string/hello"
  • /gt
  • ltButton
  • androidid"_at_id/btnClickMe"
  • androidlayout_width"fill_parent"
  • androidlayout_height"wrap_content"
  • androidtext"Click Me!"
  • /gt

18
Run it !
19
Construct UI
  • package com.example.helloandroid
  • import android.app.Activity
  • import android.os.Bundle
  • import android.widget.TextView
  • public class HelloAndroid extends Activity
  • / Called when the activity is first created.
    /
  • _at_Override
  • public void onCreate(Bundle savedInstanceState)
  • super.onCreate(savedInstanceState)
  • TextView tv new TextView(this)
  • tv.setText("Hello, Android")
  • setContentView(tv)

20
Run it
21
R class
  • In Eclipse, open the file named R.java (in the
    gen/ Generated Java Files folder).
  • The R.java file is a special file generated by
    the ADT to keep track of all the names of views,
    constants, etc, used in your Android project. You
    should not modify the content of this file as its
    content is generated automatically by the ADT
  • package com.example.helloandroid
  • public final class R
  • public static final class attr
  • public static final class drawable
  • public static final int icon0x7f020000
  • public static final class id
  • public static final int
    textview0x7f050000
  • public static final class layout
  • public static final int main0x7f030000
  • public static final class string
  • public static final int
    app_name0x7f040001

22
AndroidManifest.xml
  • The AndroidManifest.xml file is an application
    configuration file that contains detailed
    information about your application, such as the
    number of activities you have in your
    application, the types of permissions your
    application needs, the version information of
    your application, and so on.
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltmanifest xmlnsandroid"http//schemas.android.co
    m/apk/res/android"
  • package"com.example.helloandroid"
  • androidversionCode"1"
  • androidversionName"1.0"gt
  • ltapplication androidicon"_at_drawable/icon"
    androidlabel"_at_string/app_name"gt
  • ltactivity androidname".HelloAndroid"
  • androidlabel"_at_string/app_name"
    gt
  • ltintent-filtergt
  • ltaction androidname"android.inte
    nt.action.MAIN" /gt
  • ltcategory androidname"android.in
    tent.category.LAUNCHER" /gt
  • lt/intent-filtergt
  • lt/activitygt
  • lt/applicationgt
  • lt/manifestgt
Write a Comment
User Comments (0)
About PowerShow.com