1 / 36

Introduction to Android Programming

Introduction to Android Programming. 19.3.2013. Content. Basic environmental structure Building a simple app Debugging. Basic environmental structure. Create a very simple application Examine its structure Run it on a real device Run it on the emulator. Create a new Android project

sheng
Download Presentation

Introduction to Android Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Android Programming 19.3.2013

  2. Content Basic environmental structure Building a simple app Debugging

  3. Basic environmental structure Create a very simple application Examine its structure Run it on a real device Run it on the emulator

  4. Create a new Android project (File > New > Android Application)

  5. Directory name Android version Javapackage Name that appears on device Class to automatically create

  6. Set your project: Project Name: Hello World Build Target:  Select Android 2.1 Application Name: Hello World Package Name: com.android.test Create Activity:  HelloWorld Press "Finish"

  7. Project Components src – your source code gen – auto-generated code (usually R.java) Included libraries Resources • Drawables (like .png images) • Layouts • Values (like strings) Manifest file

  8. Source code Auto-generated code String constants UI layout Configuration

  9. 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.

  10. Project files

  11. Inherit from the Activity Class HelloWorld.java Sourcecode 1 public class HelloAndroid extends Activity { 2  /** Called when the activity first created. */ 3    @Override 4    public void onCreate(Bundle savedInstanceState) 5 { 6      super.onCreate(savedInstanceState); 7        setContentView(R.layout.main); 8    } 9 } Set the layout of the view as described in the main.xml layout

  12. Hello World modify HelloWorld.java

  13. XML file Used 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

  14. Elements and layouts ImageButton EditText CheckBox Button RadioButton ToggleButton RatingBar

  15. Elements and layouts DatePicker TimePicker Spinner AutoComplete Gallery MapView WebView

  16. Types of Layouts Linear Layout • It organizes controls vertical or horizontal fashion Relative 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 control Frame Layout • Frame layouts are the normal layout of choice when you want to overlap different views stacked on top the other.

  17. Layout

  18. Open main.xml in Layout mode

  19. select main.xml to view/edit the xml markup

  20. Drag a button on to layout

  21. Layout Linear Layout 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’

  22. strings.xml • In res/values • strings.xml • Promotes good programming style • Strings are just one kind of ‘Value’ 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>

  23. 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

  24. Manifest File – Adding an Activity

  25. AndroidManifest.xml 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>

  26. Run Hello World Select HelloWorld Project, Run->Run as->Android Application ADT will start a proper AVD and run HelloWorld app on it

  27. 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"

  28. Create newAndroid Virtual Device • Name:Android 2.1 • Target: Android 2.1 API Level 7 • SD card Size: 4000 MiB • Rest as default settings

  29. To get it running… Press "Create AVD". Be patient it will take a minute to create your new AVD. Select your new AVD and run your application.

  30. Emulator

  31. 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)

  32. 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();

  33. 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");

  34. Useful sources

More Related