Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android 101 - Hackatron.org

Android 101 - Hackatron.org

Let's start with the basics of Android development.


Alessandro Calzavara

November 11, 2014


  1. Android 101 The basics of Android development

  2. Alessandro
 Calzavara Mobile Developer at 
 Spreaker.com An Hackatron-er @DrAL3X

  3. None
  4. None
  5. What’s inside an Android app The project will have •

    Java files • C files (optional, NDK required) • XML files (a lot of them) • images and other resource files And, at the end, finally • apk file
  6. Project folders Java source code: java C source code: jni

    (only with NDK) Static resources: res • drawable: for images (xml, png, jpg) • layout: views description (xml) • values: raw properties (xml)
  7. Alternative resources • Using a suffix on a folder, its

    content will override the “default” one • No need to override everything • Per screen density (drawable-mdpi, drawable-hdpi, etc) • Per device sizes (layout-sw600dp, layout- h560dp, etc) • Per language (values-it, values-es, etc) • Mixed (drawable-sw600dp-xxhpi)
  8. On the root folder AndroidManifest.xml • Unique ID of the

    app (package) • Name and icon • Main (launching) activity • Required permissions
  9. AndroidManifest.xml <manifest xmlns:android=“http://schem[…]android” package="org.hackatron.app"> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity

    android:name=“org.hackatron.app.MainActivity"> <intent-filter> <action android:name=“a[…].MAIN”/> <category android:name=“a[…].LAUNCHER”/> </intent-filter> </activity> </application> </manifest>
  10. On the root folder Gradle files (if you use Android

    Studio IDE) • build.gradle • gradle/ • gradle.properties • gradlew • gradle.bat • settings.gradle
  11. Gradle: the building system Combines Ant and Maven with a

    declarative language (Groovy based) You can define tasks (with dependencies) and execute them ./gradlew assembleRelease
  12. Gradle: what it’s look like apply plugin: 'com.android.application' android {

    compileSdkVersion 21 buildToolsVersion "21.1.0" defaultConfig { applicationId "org.hackatron.cfpapp" minSdkVersion 15 targetSdkVersion 21 […] } […] } […]
  13. Let’s start with an Activity • An activity is a

    container that handle user interaction for a specific purpose. • Its content will be “prepared” inside the onCreate(Bundle) callback • mostly loading an xml layout • and it’s ready to go
  14. View in xml, logic in java • View components (and

    their relationships) are described inside a layout (.xml) file • use attributes, strings and styles • View with ID can be fetch by the activity findViewById(R.id.main_list); • Reusable UI (also across other layouts)
  15. A layout file <RelativeLayout xmlns:android="http:// schemas.android.com/apk/res/android" android:layout_width=“match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/main_list"

    android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
  16. Styles • Skin UI • Split UI style from UI

    elements • Share common values across styles • style property <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OK" style=“@style/MyButton" />
  17. Inside a values/*.xml file <style name=“EditingButton.Ok" parent=“Button.Yellow"> <item name=“android:drawableLeft"> @drawable/icon_editing_confirm</item>

    <item name=“android:gravity"> center</item> <item name=“android:text"> @android:string/ok</item> </style>
  18. Fragments: lighter Activity • A Fragment is a piece of

    UI that can be placed in an Activity (through FragmentManager). • Fragment is closely tied to the Activity it is in (lifecycle dependency) • and can not be used apart from one. • Specialized piece of a larger Activity • Reusable logic.
  19. Activity lifecycle Activity launched onCreate() onResume() Activity running Activity shut

    down onStop() onPause() onStart() onDestroy()
  20. Fragment lifecycle Fragment is added onAttach() Fragment is active Fragment

    is destroyed onCreate() onCreateView() onActivityCreated() onStart() onResume() onDetach() onDestroy() onDestroyView() onStop() onPause()
  21. Not enough tricky • Views can be destroyed “at any

    time” • When save and restore view state? • onSaveInstanceState(Bundle) • onRestoreInstanceState(Bundle) • Safer option: save @ onStop(), restore @ onStart()
  22. Start another activity • Activity must be declared in the

    AndroidManifest.xml to be launched (!) • Create an intent, with the activity class, then start it Intent create = new Intent(this, HelloActivity.class); startActivity(create); • if the activity returns something startActivityForResult(Intent, int)
  23. Return the result • Create (or reuse) an Intent to

    store the result Intent data = new Intent(); data.putExtra(“res", field.getText()); • Set the result of the activity setResult(RESULT_OK, data); Terminate the activity finish();
  24. Catch it @Override protected void onActivityResult(int requestCode, int resultCode, Intent

    data) { if (requestCode == REQUEST_CREATE && resultCode == RESULT_OK) { // Do something with data extras Log.i(TAG, data.getStringExtra(“res”)); return; } super.onActivityResult(requestCode, resultCode, data); }
  25. Populate a ListView • An Adapter is responsible for storing

    and display the content of a “list of things”. • Reflect data changes to the view TodoAdapter adapter = new TodoAdapter(); ListView listView = (ListView) findViewById(R.id.main_list); listView.setAdapter(adapter);
  26. Adapters • ArrayAdapter (static content only), CursorAdapter, SimpleAdapter, etc •

    Extends your own from BaseAdapter (easy) • For each content item, choose the proper layout and “hydrate it” • If data changes, notifyDataSetChanged()
  27. Demo

  28. Some useful links • http://developer.android.com/ • https://github.com/android/ platform_frameworks_base • https://github.com/dral3x/