HUJI Post PC Workshop 1 Introduction to Android Development Ari Sprung arisprung@gmail.com
Workshops Structure Workshop 1 – Introduction Setting up, basic app, views, layouts Workshop 2 – Adapters and Menus Resources, adapter views, menus, unit testing Workshop 3 – Activities Multiple activities, dialogs, intents Workshop 4 – Persistence SQLite, cursors, backend-as-a-service Workshop 5 – Threading Files, threads, web services
Prerequisites and Homework What you need to know: Java programming, collections, files, etc. Database basics, tables, SQL Homework will involve: Application development Activity testing Using source control
Make Sure You’re OK With This: Thread t = new Thread(new Runnable() { public void run() { Connection c = DriverManager.getConnection(...); Statement s = c.createStatement(); ResultSet r = s.executeQuery( “SELECT name, AVERAGE(grade) avg “ + “FROM students GROUP BY grade “ + “ORDER BY avg DESC”); while (r.next()) { ... } } }); t.start();
Before We Begin… You can’t learn from the slides. The slides are a learning aid, and they will not contain all the information discussed in class. You still have to come to class and use additional reading to fill the gaps.
Android Architecture
Versions and API Levels Considerable fragmentation API levels correspond to each version
Setting Up Dev Environment Download the Android Studio for your OS http://developer.android.com/sdk/index.html Contains Eclipse and Android SDK Use SDK Manager to download multiple versions of Android Use AVD Manager to create emulator images for various versions(not recommend). Genny Motion https://www.genymotion.com
Demonstration: Dev Tools Studio SDK Manager Gradle Emulator
Hello World App Components src – your package’s sources gen – code generated by the compiler Do not touch this res – resources: images, strings, layouts res/layout/main.xml – your main UI AndroidManifest.xml – app description bin – binary products, including your apk
Application Components Activity Screen, form – usually full-screen, one at a time Activity subclass View Anything that appears on the screen: button, edit box (EditText), label (TextView) Layout A component that manages a bunch of views and decides how to position and draw them
Code-Layout Separation Your view lives in XML files (layouts) Your code lives in Java files Your view communicates with your code through listeners Your code communicates with your view through object references <Button android:text=“Click me” android:id=“@+id/btn1” /> final Button b = (Button) findViewById(R.id.btn1); b.setOnClickListener( new OnClickListener() { public void onClick(View v) { b.setText(“Thanks!”); } });
Basic Views Explore the standard Android views and how they are declared in XML <Button android:text=“Button” /> <EditText android:hint=“phone” /> <CheckBox android:checked=“true” />
Basic Layouts LinearLayout stacks elements horizontally/vertically (orientation) It’s often very useful to nest layouts <LinearLayout android:orientation=“horizontal”> <ImageView android:layout_weight=“1” android:layout_height=“fill_parent” ... /> <TextView android:layout_weight=“2” <WebView ... /> </LinearLayout>
Basic Layouts RelativeLayout allows relative positioning of child elements <RelativeLayout> <ImageView android:id=“@+id/img” android:layout_width=“fill_parent” ... /> <TextView android:id=“@+id/txt” android:layout_below=“@id/img” android:layout_alignParentLeft=“true” <Button ... /> </RelativeLayout>
Demonstration: UI Events Your activity class’ onCreate method creates the UI from an XML file Then, retrieve views and use listeners public class MyActivity extends Activity { @Override public void onCreate(Bundle unused) { super.onCreate(unused); setContentView(R.layout.main); EditText ed = (EditText)findViewById(R.id.ed); ed.setOnTextChangeListener(...); Button btn = (Button)findViewById(R.id.btn); btn.setTextSize(14.0f); }
Demonstration: Source Control Source control: management of changes to source code Logical branching and versioning Development history, rollback Change tracking You will be using GitHub for your homework assignments and final projects Go to http://github.com, create an account For each homework, associate your username with the GitHub repository
Homework 1 Tip (12%) Calculator Fill submission form 12:38 Tip (12%) Calculator Package: il.ac.huji.tipcalculator Repo tag: v1 Activity: TipCalculatorActivity Fill submission form Pay attention to view ids: edtBillAmount chkRound btnCalculate txtTipResult 42.20 Bill Amount Round to nearest dollar Calculate Tip: $5 When rounding to nearest dollar, do not print decimal separator and any trailing digits. When not rounding to nearest dollar, ALWAYS print decimal separator and two trailing digits, e.g.: $6.00, $6.12
Additional Resources Tools workflow App fundamentals Layouts http://developer.android.com/tools/workflow/index.html App fundamentals http://developer.android.com/guide/components/fundamentals.html Layouts http://developer.android.com/guide/topics/ui/declaring-layout.html Handling input events http://developer.android.com/guide/topics/ui/ui-events.html Stack Overflow http://stackoverflow.com/