CS193A Notes PDF

Title CS193A Notes
Course Android Programming
Institution Stanford University
Pages 10
File Size 155.8 KB
File Type PDF
Total Downloads 14
Total Views 145

Summary

Notes I took as a student in Marty Stepp's class at Stanford University, CS193A: Android Programming....


Description

Lecture 1 1/8/19: - Activity- single screen of UI in app - View- visible onscreen item - Widget- GUI control such as a button - Layout- invisible container to position/size widgets - Event- action that occurs when user interacts with widget - External stimulus your program can respond to (mouse motion / tapping, keys pressed, etc) - Action bar- top menu of common actions - Notification area- top system menu - Event-driven programming - Overall execution of program is largely driven by user interactions - Must have listener methods to handle events attached to widgets Lecture 2 1/10/19: - To get variable for view/widget, use findViewById(R.id._idname_) - Pop-up message = toast! - Toast.makeText(this, “text”, Toast.LENGTH_SHORT).show() - Layout typically done by positioning things relatively to screen boundaries or other views on the screen - Layout implemented with XML - LinearLayout lays out views in a single line - can set orientation to vertical or horizontal - Gravity property tells views where to go to within the layout - Layout_weight helps to size/stretch things to ration out space - Widget box model - Content in the middle, then padding (widget stretched larger than its content), then border (line around widget), then margin (extra space between widgets - Wrap_content for layout_width/height makes it just take up enough space to fill what’s in it - Another common value is match_parent - matches size of parent element - Can also have grid and table layouts and nested layouts, etc - Default in Android Studio with visual layout editor is the Constraint Layout - Widgets’ corners/edges are connected to other widgets or screen edges - Every view must have at least 2 connections - 1 vertical and 1 horizontal Lecture 3 1/15/19: - Lots of possible widgets, typically add the widget in XML and add handlers in Kotlin - Buttons have attributes including id, clickable, onClick, text - TextView to display text, attributes: id, text - Put a scrollview around something if you want it to scroll - EditText has attributes id, hint, inputType, text, lines, maxLines, textSize - ImageView requires you to have image resource stored in drawable file, attributes id, src, contentDescription, tag, scaleType

-

-

- ImageButton- clickable widget with icon - CheckBox has id, checked, clickable, onClick, text - Switch has id, textOn, textOff, showText, onClick, text - RadioButton goes in RadioGroup Resources go in app/src/main/res//. - Refer to in XML as @/ - Refer to in Kotlin as R.. Can setOnClickListener in onCreate with lambda functions (use underscores if you don’t give a shit about the parameters it wants you to pass some

Lecture 4 1/17/19: - ListView to display a list - Add list item names to strings.xml - For a dynamic list that changes over time you need an adapter - Links an array or a database entry to the list - val name = ArrayAdapter(activity, layout, array) - Activity is usually “this”, layout is android.R.layout.simple_list_item_1 - When you change something, say myAdapter.notifyDataSetChanged() to update - Kotlin immutable list: val nums: List = listOf(1,2,3), mutable list: val words = mutableListOf(“Hello”, “Goodbye”) - Can access with square brackets, check membership with “in” - Drop down list is called a Spinner - For Kotlin if you want to initially declare something as null you declare it with a type and a question mark after the type, then put a question mark after it when using it - Can also say private lateinit var with a type but no initialization if you are going to initialize it later - Android can read/write files from either internal storage (private to one app) or external storage (shared by all apps) - Internal storage files need to be put in res/raw folder - Can use File object or InputStream or BufferedReader or Scanner (last 2 better) - All activities have methods (see slides) to do these things - Use Log class to print log statements to help debug (Log.d(tag, msg)) Lecture 5 1/22/19: - Many apps have multiple activities - Use intents to pass information from one activity to another - Right click in project area on left → new → activity → empty - Have to add new activities to the AndroidManifest.xml file in order for them to work - Intents are used as a bridge between activities for one to invoke another - New activity can be in the same app or a different app - Can pass data/parameters

-

-

-

Can start an activity or service (long running app with no GUI), or to broadcast a message Can be explicit (name specific activity to launch) or implicit (general request that can be accomplished by different apps) Create: val myIntent(this, ::class.java) (without ) Add params: myIntent.putExtra(“name”, value) Start activity: startActivity(myIntent) For other activity to return control to previous intent - Make intent with no params: val myIntent = Intent() - Put in any extras - Set as ok: setResult(RESULT_OK, myIntent) - Finish: finish() To catch intent/activity finishing - Instead of saying startActivity(myIntent), say startActivityForResult(myIntent, REQ_CODE) - Then override fun onActivityResult(reqCode, resCode, intent) { - super.onActivityResult(reqCode, resCode, intent) - If (reqCode == REQ_CODE) { - //do something - } - } In onCreate method you always have access to “intent” object that started the current activity and can get params out with intent.getStringExtra, etc.

Lecture 6 1/24/19: - Activity lifecycle - Activity started → onCreate() → onStart() → onRestoreInstanceState() → onResume() → RUNNING! → onSaveInstanceState() → onPause() (goes back to on resume) → onStop() (goes back to onRestart() → onStart() → etc) → onDestroy() (goes back to onCreate) → activity shut down - Pause is typically it’s still there but in the background - Stop is like the activity is fully off the screen - Activity instance state: current state of activity (which boxes are checked, text typed in boxes, values of private fields, etc) - Can lose the state if you rotate the screen, go from one activity to another, launch another app, etc - Can handle rotation by changing AndroidManifest.xml in the configChanges attribute - android:configChanges=”orientation|screenSize” - Could use onPause/onStop to store state in more permanent storage (file storage, database, etc), then do opposite in onResume/onStart - Always call super function when overriding a lifecycle method - Can use Bundles to remember instance state in onSaveInstanceState

Lecture 7 1/29/19: - Fragments - reusable little chunks of UI that can appear in an activity - Often useful to make more responsive UIs - Can make special drawable/layout files for files that you want to use for different layout sizes (or layout-land for landscape!) - Create with right-click → new → fragment (uncheck some boxes) - Then put XML stuff in the fragment file - Then in activity XML can put a fragment tag to insert it and reuse! - Fragments have similar lifecycle methods to activities - If you’re in a fragment, there’s a global “activity” property that is the activity in which the fragment is located - Global fragmentManager can find fragments by id - val detFrag : DetailsFragment = fragmentManager?.findFragmentById(R.id.details_fragment) Lecture 8 1/31/19: - setContentView call in onCreate is a LayoutInflater that tells it to go to that XML file and make that appear on the screen - You can also write your layout in kotlin if you hate yourself - val name = WidgetType(this) (inside activity class) - name.field = value - To add it to a layout say layout_id.addView(name) - Some properties are harder to set - Val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,...) - Then tell it to use those params - Tedious to do all this for a lot of different widgets - Can instead make a mini XML file with properties for one widget and then tell the kotlin to inflate that layout and add it - Like a fragment but without its own events and lifecycle - Can pop up dialog boxes over other content on the screen to get quick response without going to a whole new activity - Many built in dialogs, or you can make a custom one - Most common will be AlertDialog - Can have icon, title, message, negative/neutral/positive buttons - Play sounds! - val mp = MediaPlayer.create(this, R.raw.filename) - mp.start() Lecture 9 2/5/19: - Many shared libraries stored in a Maven repository that you can easily add to your projects

-

-

-

To add library, edit build.gradle file and in the dependencies section add “implementation ” Picasso is a library by Square for manipulating images - implementation ‘com.squareup.picasso:picasso:2.+’ - Make sure app permissions in AndroidManifest.xml include internet access - - Can say Picasso.get().load(“url”).into(ImageView) - In order to use http links instead of https need to add android:usesCleartextTraffic=”true” to like the application section - Can also use picasso to crop, center, fit, rotate, resize, etc Ion library to download files from the web (similar to OkHttp and Volley) - implementation ‘com.koushikdutta.ion:ion:2.+’ - Ion.with(this).load(“url”).as().setCallback { ex, result -> //code } Animation libraries - see slides Bootstrap libraries help make cool fancy widgets

Lecture 10 2/7/19: - Most apps somehow access data through a web layer - Client (app) queries server (web URL), which sends back data - Data typically stored/sent in JSON, XML, YAML, or other well-defined format - Web service - set of functionality offered over a server using the web other than HTML - Service publishes format for requests and responses - Platform independent so any device can use it - Can google for APIs for just about anything for how to connect to different things - Often need to sign up for an API key to prevent fraud - APIs can get taken down or just not work which can be a problem if you become too dependent on them - Can use Ion or some other library to send requests to an API endpoint - Can say val json = JSONObject(data), json.getString(“key”) to get data out of it - Ion has a asJsonObject method, but it doesn’t work great so better to just get it as a raw string - There are built-in things for http connection in kotlin but they suck - Another common form of API request is with query parameters Lecture 12 2/14/19: - Database is data structured in associated tables (think Excel spreadsheets) - Rows are called records, columns have attributes - Can be located locally on a device, on a remote web server, or in the cloud - Powerful search capabilities - Fast searching/filtering - big/scalable, safe, support multi-user, abstract, common syntax - SQLite included with Android API - SQL - structured query language

-

-

Basic query: SELECT FROM WHERE ; - Can do SELECT * to do all, SELECT DISTINCT to eliminate duplicates, WHERE is optional but can be good, table/column names case-sensitive - WHERE can have =, >, >=,...


Similar Free PDFs
CS193A Notes
  • 10 Pages
Notes
  • 18 Pages
Notes
  • 12 Pages
Notes
  • 61 Pages
Notes
  • 35 Pages
Notes
  • 19 Pages
Notes
  • 70 Pages
Notes
  • 6 Pages
Notes
  • 35 Pages
Notes
  • 29 Pages
Notes
  • 70 Pages
Notes
  • 6 Pages
Notes
  • 19 Pages
Notes
  • 32 Pages
Notes
  • 28 Pages