Introduction to Android Programming
description
Transcript of Introduction to Android Programming
![Page 1: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/1.jpg)
Introduction to Android Programming
19.3.2013
![Page 2: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/2.jpg)
ContentBasic environmental structureBuilding a simple appDebugging
![Page 3: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/3.jpg)
Basic environmental structure
Create a very simple applicationExamine its structureRun it on a real deviceRun it on the emulator
![Page 4: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/4.jpg)
Create a new Android project (File > New > Android Application)
![Page 5: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/5.jpg)
Name that appears
on device
Directoryname
Class toautomatically
create
Javapackage
Androidversion
![Page 6: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/6.jpg)
Set your project:Project Name:
Hello WorldBuild Target:
Select Android 2.1Application Name:
Hello WorldPackage Name: com.android.testCreate Activity: HelloWorldPress "Finish"
![Page 7: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/7.jpg)
Project Componentssrc – your source codegen – auto-generated code (usually
R.java)Included librariesResources– Drawables (like .png images)– Layouts– Values (like strings)
Manifest file
![Page 8: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/8.jpg)
Sourcecode
Auto-generatedcode
UIlayout
Stringconstants
Configuration
![Page 9: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/9.jpg)
Now you have your project created let's write some code!Your code is located in a file called HelloWorld.java in the src folder.Your screen layout file is main.xml in the layout directory.
![Page 10: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/10.jpg)
Project files
![Page 11: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/11.jpg)
1 public class HelloAndroid extends Activity {2 /** Called when the activity first created. */3 @Override4 public void onCreate(Bundle savedInstanceState) 5 {6 super.onCreate(savedInstanceState);7 setContentView(R.layout.main);8 }9 }
HelloWorld.javaSourcecode
Inherit from the Activity Class
Set the layout of the view as described in the main.xml layout
![Page 12: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/12.jpg)
Hello Worldmodify HelloWorld.java
12
![Page 13: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/13.jpg)
XML fileUsed to define some of the resources– Layouts (UI)– Strings–Manifest file
res/layout: contains layout declarations of the app, UIs are built according to the layout file
![Page 14: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/14.jpg)
Elements and layouts
ImageButtonEditText
CheckBoxButton
RadioButtonToggleButton
RatingBar
![Page 15: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/15.jpg)
DatePickerTimePicker
SpinnerAutoComplete
GalleryMapViewWebView
Elements and layouts
![Page 16: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/16.jpg)
Types of LayoutsLinear Layout– It organizes controls vertical or horizontal
fashionRelative Layout,– It organizes controls relative to one
another.Table Layout– A grid of made up of rows and columns,
where a cell can display a view controlFrame Layout– Frame layouts are the normal layout of
choice when you want to overlap different views stacked on top the other.
![Page 17: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/17.jpg)
Layout
![Page 18: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/18.jpg)
Open main.xml in Layout mode
![Page 19: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/19.jpg)
select main.xml to view/edit the xml markup
![Page 20: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/20.jpg)
Drag a button on to layout
![Page 21: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/21.jpg)
Linear Layout
Layout
21
main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>
TextView, display static text
A reference to String resource ‘hello’
![Page 22: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/22.jpg)
1 <?xml version="1.0" encoding="utf-8"?>2 <resources>3 <string name="hello">Hello World, HelloAndroid!4 </string>5 <string name="app_name">Hello, Android</string>6 </resources>
strings.xml
In res/valuesstrings.xmlPromotes good programming styleStrings are just one kind of ‘Value’
![Page 23: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/23.jpg)
Manifest File
Contains characteristics about your application
NEED to specify it in manifest file– Have more than one Activity in app,– Services and other components too– Important to define permissions and
external libraries, like Google Maps API
![Page 24: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/24.jpg)
Manifest File – Adding an Activity
![Page 25: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/25.jpg)
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 package="edu.upenn.cis542" 5 android:versionCode="1" 6 android:versionName="1.0"> 7 <application android:icon="@drawable/icon" 8 android:label="@string/app_name"> 9 <activity android:name=".HelloAndroid"10 android:label="@string/app_name">11 <intent-filter>12 <action 13 android:name="android.intent.action.MAIN" />14 <category 15 android:name="android.intent.category.LAUNCHER"/>16 </intent-filter>17 </activity>18 </application>19 </manifest>
AndroidManifest.xml
![Page 26: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/26.jpg)
Run Hello WorldSelect HelloWorld Project, Run->Run as->Android Application
ADT will start a proper AVD and run HelloWorld app on it
26
![Page 27: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/27.jpg)
![Page 28: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/28.jpg)
You may receive "Android AVD Error" if you have not setup an android emulator
device.
Select yes to setup a new Android Virtual Device
Select "New"
![Page 29: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/29.jpg)
Create newAndroid Virtual Device• Name:
Android 2.1• Target:
Android 2.1 API Level 7• SD card Size:
4000 MiB• Rest as default settings
![Page 30: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/30.jpg)
Press "Create AVD". Be patient it will take a minute to create your new AVD.
Select your new AVD and run your application.
To get it running…
![Page 31: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/31.jpg)
Emulator
![Page 32: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/32.jpg)
On Phone
Should be enabled on phone to use developer features
In the main apps screen select:→ Settings → Applications → Development → USB debugging (needs to be checked)
![Page 33: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/33.jpg)
How to Debug Log.v(tag, message);
Window > Show View > Other > Android > LogCat
To show a pop-up window:Toast.makeText( getApplicationContext(), message, Toast.LENGTH_LONG).show();
![Page 34: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/34.jpg)
Logcat Information Log.e("MyTag", "Error message with my own tag");Log.w("dalvikvm", "VM Warning message"); Log.d("MyTag", "Debug message");Log.i("MainActivity","Information message"); Log.v("MyTag", "Verbose message"); Log.f(“Fail", "What a Terrible Failure");
![Page 35: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/35.jpg)
![Page 36: Introduction to Android Programming](https://reader035.fdocuments.us/reader035/viewer/2022062521/5681685d550346895dde9aba/html5/thumbnails/36.jpg)
Useful sources
Android Official Site
• http://www.android.com
Android SDK, Tutorial, Concepts and API docs
• http://androidappdocs.appspot.com/index.html
Android Development Community
• http://www.anddev.org/
30 Days Android Apps Development
• http://bakhtiyor.com/category/30-days-of-android-apps/ 36