programming concepts and skills -...

29
Programming Concepts and Skills Creating an Android Project

Upload: others

Post on 25-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

Programming Conceptsand Skills

Creating an Android Project

Page 2: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

Getting Started

• An Android project contains all the files that comprise the source code for your Android app. The Android SDK tools make it easy to start a new Android project with a set of default project directories and files.

• This lesson shows how to create a new project either using Eclipse (with the ADT plugin) or using the SDK tools from a command line.

Page 3: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

Create a Project with Eclipse

• Once you have Eclipse open:

• Click New in the toolbar. 

• In the window that appears, open the Androidfolder, select Android Application Project, and click Next. 

Page 4: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• Fill in the form that appears: – Application Name is the app name that appears to users. For this project, use "My First App." 

– Project Name is the name of your project directory and the name visible in Eclipse. 

– Package Name is the package namespace for your app (following the same rules as packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. For this reason, it's generally best if you use a name that begins with the reverse domain name of your organization or publisher entity. For this project, you can use something like "com.example.myfirstapp." However, you cannot publish your app on Google Play using the "com.example" namespace.

Page 5: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

Minimum Required SDK is the lowest version of Android that your app supports, indicated using the API level. To support as many devices as possible, you should set this to the lowest version available that allows your app to provide its core feature set. If any feature of your app is possible only on newer versions of Android and it's not critical to the app's core feature set, you can enable the feature only when running on the versions that support it (as discussed in Supporting Different Platform Versions). Leave this set to the default value for this project. 

Target SDK indicates the highest version of Android (also using the API level) with which you have tested with your application. As new versions of Android become available, you should test your app on the new version and update this value to match the latest API level in order to take advantage of new platform features.

Compile With is the platform version against which you will compile your app. By default, this is set to the latest version of Android available in your SDK. (It should be Android 4.1 or greater; if you don't have such a version available, you must install one using the SDK Manager). You can still build your app to support older versions, but setting the build target to the latest version allows you to enable new features and optimize your app for a great user experience on the latest devices. 

– Theme specifies the Android UI style to apply for your app. You can leave this alone. 

• Click Next.

Page 6: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• On the next screen to configure the project, leave the default selections and click Next. 

• The next screen can help you create a launcher icon for your app. You can customize an icon in several ways and the tool generates an icon for all screen densities. Before you publish your app, you should be sure your icon meets the specifications defined in the Iconography design guide.

• Click Next.

Page 7: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• Now you can select an activity template from which to begin building your app. For this project, select BlankActivity and click Next.

• Leave all the details for the activity in their default state and click Finish. 

• Your Android project is now set up with some default files and you’re ready to begin building the app.

Page 8: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

Managing Projects

• Projects act as containers for storing things such as code and resource files. The SDK tools expect your projects to follow a specific structure so it can compile and package your application correctly, so it is highly recommended that you create them with Eclipse and ADT.

• There are three types of projects, and they all share the same general structure but differ in function:

Page 9: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• Android Projects An Android project is the container for your application's source code, resource files, and files such as the Ant build and Android Manifest file. An application project is the main type of project and the contents are eventually built into an .apk file that you install on a device. 

• Test Projects These projects contain code to test your application projects and are built into applications that run on a device. 

Page 10: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• Library Projects These projects contain shareable Android source code and resources that you can reference in Android projects. This is useful when you have common code that you want to reuse. Library projects cannot be installed onto a device, however, they are pulled into the .apk file at build time. 

Page 11: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• When you use the Android development tools to create a new project, the essential files and folders will be created for you. There are only a handful of files and folders generated for you, and some of them depend on whether you use the Eclipse plugin or the android tool to generate your project. As your application grows in complexity, you might require new kinds of resources, directories, and files.

Page 12: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

Android Projects

• Android projects are the projects that eventually get built into an .apk file that you install onto a device. They contain things such as application source code and resource files. Some are generated for you by default, while others should be created if required. The following directories and files comprise an Android project:

Page 13: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• src/ Contains your stub Activity file, which is stored at src/your/package/namespace/ActivityName.java. All other source code files (such as .java or .aidl files) go here as well. 

• bin/ Output directory of the build. This is where you can find the final .apk file and other compiled resources. 

• jni/ Contains native code sources developed using the Android NDK. For more information, see the Android NDK documentation. 

Page 14: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• gen/ Contains the Java files generated by ADT, such as your R.java file and interfaces created from AIDL files. 

• assets/ This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as‐is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data. 

Page 15: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• res/ Contains application resources, such as drawable files, layout files, and string values. See Application Resources for more information. 

• anim/ For XML files that are compiled into animation objects. See the Animation resource type. 

• color/ For XML files that describe colors. See the Color Values resource type. 

Page 16: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• drawable/ For bitmap files (PNG, JPEG, or GIF), 9‐Patch image files, and XML files that describe Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or focused). See the Drawable resource type. 

• layout/ XML files that are compiled into screen layouts (or part of a screen). See the Layout resource type. 

• menu/ For XML files that define application menus. See the Menus resource type. 

Page 17: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• raw/ For arbitrary raw asset files. Saving asset files here instead of in the assets/ directory only differs in the way that you access them. These files are processed by aaptand must be referenced from the application using a resource identifier in the R class. For example, this is a good place for media, such as MP3 or Ogg files. 

• values/ For XML files that are compiled into many kinds of resource. Unlike other resources in the res/ directory, resources written to XML files in this folder are not referenced by the file name. Instead, the XML element type controls how the resources is defined within them are placed into the R class. 

Page 18: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• xml/ For miscellaneous XML files that configure application components. For example, an XML file that defines a PreferenceScreen, AppWidgetProviderInfo, or Searchability Metadata. See Application Resources for more information about configuring these application components. 

• libs/ Contains private libraries. 

Page 19: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• AndroidManifest.xml The control file that describes the nature of the application and each of its components. For instance, it describes: certain qualities about the activities, services, intent receivers, and content providers; what permissions are requested; what external libraries are needed; what device features are required, what API Levels are supported or required; and others. See the AndroidManifest.xml documentation for more information 

Page 20: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• project.properties This file contains project settings, such as the build target. This file is integral to the project, so maintain it in a source revision control system. To edit project properties in Eclipse, right‐click the project folder and select Properties. 

Page 21: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• local.properties Customizable computer‐specific properties for the build system. If you use Ant to build the project, this contains the path to the SDK installation. Because the content of the file is specific to the local installation of the SDK, the local.properties should not be maintained in a source revision control system. If you use Eclipse, this file is not used. 

Page 22: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• ant.properties Customizable properties for the build system. You can edit this file to override default build settings used by Ant and also provide the location of your keystore and key alias so that the build tools can sign your application when building in release mode. This file is integral to the project, so maintain it in a source revision control system. If you use Eclipse, this file is not used. 

Page 23: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• build.xml The Ant build file for your project. This is only applicable for projects that you build with Ant. 

• Library Projects and Test Projects will not be used at this time.

Page 24: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

Running Your App

• How you run your app depends on two things: whether you have a real Android‐powered device and whether you're using Eclipse.

• For all the projects and activities for this course, we will use the Android emulator.

Page 25: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• When you build and run the default Android app, the default Activity class starts and loads a layout file that says "Hello World." The result is nothing exciting, but it's important that you understand how to run your app before you start developing.

Page 26: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

Run on the Emulator

• To run your app on the emulator you need to first create an Android Virtual Device (AVD). An AVD is a device configuration for the Android emulator that allows you to model different devices.

Page 27: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• To create an AVD:

• Launch the Android Virtual Device Manager: – In Eclipse, click Android Virtual Device Manager from the toolbar. 

• In the Android Virtual Device Manager panel, click New. 

• Fill in the details for the AVD. Give it a name, a platform target, an SD card size, and a skin (HVGA is default). 

Page 28: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

• Click Create AVD. 

• Select the new AVD from the Android Virtual Device Manager and click Start. 

• After the emulator boots up, unlock the emulator screen. 

Page 29: Programming Concepts and Skills - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/3_1_gettingstarted.pdf · Getting Started • An Android project contains all the files that comprise

To run the app from Eclipse:

• Open one of your project's files and click Runfrom the toolbar. 

• In the Run as window that appears, select Android Application and click OK. 

• Eclipse installs the app on your AVD and starts it.

• On the emulator, locate MyFirstActivity and open it. 

• That's how you build and run your Android app on the emulator!