300 likes | 411 Vues
Dive into the world of Android app development with this comprehensive guide to creating your first Android application. Learn how to set up a new project within Eclipse, define the app's name and package, and understand key components like the AndroidManifest.xml file and resource management. This tutorial also covers creating a basic user interface, utilizing resources, and configuring application settings for compatibility with various Android versions. Start building your Hello World app today and lay the foundation for your journey in mobile development!
E N D
Building first android application Dr. Razi Iqbal razi.iqbal@leads.edu.pk
Create a new Android Application • Application Name • Name of the App / Project • Project Name • Used by Eclipse and should be unique within a workspace • Package Name • It is collection of Classes. Should be same for the lifetime of the app • Should be like com.razi.helloWorld • Minimum SDK • Lowest SDK that app will support. • Will support more devices and less features • Target SDK • Highest SDK an app supports • Compile with • Compiles with Highest API level. • Theme • Base theme for the project
Creating New Run Configuration • ADT automatically creates Run Configuration • If that doesn’t suit a developer, new run configurations can be built. • Used for Customized Configurations • Emulator • Project • Activity
Hello World Console • Contains all the tasks performed for the Project • ADB is Android Debug Bridge
Project Components (Package Explorer) • src • It is source folder of the project • Contains the Package • com.razi.helloworld • The package contains .java file • MainActivity.java • Built during the new project wizard • src folder can have more than one package • Sometimes apps use different user packages • For Example • One for Web Access • One for Creating GUI
Project Components (Package Explorer) • assets • It contains all the assets of the project • Normally will contain • Databases • Dictionaries • Read data from the assets • Accessed with • streams of bytes • Converted into Objects • Using proper methods
Project Components (Package Explorer) • bin • Contains files built by ADT during build process • It contains all important .apk file • .apk is Android Package File • It is like exe file for windows • It’s a binary • It contains everything needed by Android app
Project Components (Package Explorer) • res • Contains all the resources for the app • Icons, Images etc • Menus • Layouts • Activity_main.xml • Layout of the Activity • values • Strings.xml
Project Components (Package Explorer) • gen • Its ADT generated folder • Files in this folder should not be changed manually • It contains • BuildConfig.java • R.java • R.java class is used • To reference the resources of the project in res folder • Each member of res has a class in R.java and contains all the members in that class • Don’t worry about Hex values
AndroidManifest.xml 1 2 3
AndroidManifest.xml • Provides the package name which is a unique identifier • Provides the version code • It is an integer value • Next values should be greater than the previous values • It is used for updates • Google Play knows when to inform user about updates based on this code • Code is not visible to the user • Provides version name • It’s a string value • <major>.<minor>.<point> e.g. 2.1.2 • It is used to display it to user • It can be anything like Date e.g. 2010-06-28 1
AndroidManifest.xml • Provides minimum SDK version • Use to run the apps on minimum OS • Normally should be minimum OS to support more devices • Provides Target SDK version • Maximum SDK version • SDK in which the app is built and tested • Should be latest SDK to support latest features 2
AndroidManifest.xml • Provides Launcher Icon • Available in Drawable folder under res. • Provides App Label • Label for the application • The Label is stored in strings.xml file • Provides App Theme • Theme for the app • Available in styles.xml file • Provides Activity • There is one activity • Activity is handled by MainActivity.java in src folder • Label for the Activity is the same as app name under strings.xml 3
strings.xml • Available under res values • Contains strings for the project (app) • All strings should be in this xml file • Strings are in this file so that localization is easy • If in future the app is to be converted into some other language only strings in this file will be converted • It contains • Name (the name of the string e.g. app_name) • Value (the value of the string e.g. Hello World)
activity_main.xml • xmlns: android and xmlns:tools are the namespaces • .MainActivity tells this layout is for MainActivity.java file • @dimen and @string are defined in R.java
colors.xml • Add new XML file for colors in res values • Name it colors.xml • Copy paste colors in the file • Save it • Use it like this • @color/Red android:textColor="@color/Red" To change the Font size android:textSize="40sp"
Adding a Button • Add new Button by dragging and Dropping • Change Text to “Click Me!” • Placement will be relative to Text View • Check for R.java class • It will add a new button1 in id class • This is done after building the project
MainActivity.java 1 2 3 4
MainActivity.java • Package for current App • Developed during New Project Wizard • It contains the App • A Project can have Multiple Packages 1 • Using Android Available Packages • In order to use classes, we have to import Packages • Different packages are imported in this case • import android.os.Bundle; • import android.app.Activity; • import android.view.Menu; 2
MainActivity.java • Inherit MainActivity from Activity Class • Our MainActivity class is subclass of Activity • Activity Class contains many methods • MainActivity can use these methods 3 • onCreate is a methods in Activity class • @Override is used to tell the compiler we are overriding the superclass method • App will compile without it also but better use it 4