├── README.md ├── chap04_interactions ├── ex2_toast.txt ├── ex3_list_view_clicks.txt └── ex1_click_handlers.txt ├── chap07_advanced_views ├── ex3_timepicker.txt ├── ex1_toast_inputs.txt ├── ex5_gridview.txt ├── ex2_spinner_toast.txt └── ex4_progressbar.txt ├── chap03_view ├── ex2_basic_views.txt ├── ex1_layout_gravity.txt ├── ex3_view_attributes.txt └── ex4_simple_list_view.txt ├── chap01_app_fundamentals ├── ex2_textview.txt ├── ex5_size_units.txt ├── ex3_strings.txt ├── ex4_lifecycle.txt ├── ex1_manifest.txt └── ex0_new_project.txt ├── chap02_user_interface ├── ex3_layout_params.txt ├── ex2_linear.txt └── ex1_launcher.txt ├── chap06_networking ├── ex3_async_http_client.txt ├── ex2_async_task.txt └── ex1_image_download.txt ├── chap05_user_flows ├── ex1_explicit_intents.txt ├── ex4_action_bar.txt ├── ex2_implicit_intents.txt └── ex3_intent_with_results.txt ├── chap08_preferences └── ex1_persist_settings.txt └── chap09_content_providers └── ex1_contact_list.txt /README.md: -------------------------------------------------------------------------------- 1 | intro_android_exercises 2 | ======================= 3 | 4 | Step by step exercises for learning Android 5 | -------------------------------------------------------------------------------- /chap04_interactions/ex2_toast.txt: -------------------------------------------------------------------------------- 1 | Goal: To display a toast message. 2 | 3 | Description: 4 | A toast is a text view that appears in the bottom half of the screen that automatically fades away after a short delay. It is used to display transient messages. 5 | 6 | Directions: 7 | 1) In the onClick method of a button, display a message using a Toast. 8 | 9 | 10 | ## Snippets 11 | 12 | // Use Toast to show message in methods 13 | Toast.makeText(MyActivityClass.this, "firstButton clicked via XML handler", Toast.LENGTH_SHORT).show(); 14 | -------------------------------------------------------------------------------- /chap07_advanced_views/ex3_timepicker.txt: -------------------------------------------------------------------------------- 1 | Goal: Explore using a TimePicker view. 2 | 3 | Description: 4 | This is a basic exercise using the TimePicker view. 5 | 6 | Directions: 7 | 1) Create an Activity with a TimePicker view. 8 | 2) Add a button. In the click handler, 9 | a) Display a toast of the current selected time. 10 | 11 | 12 | ## Snippets 13 | 14 | // Java Activity 15 | public void displayTime(View v) { 16 | String time = tpTime.getCurrentHour() + ":" + tpTime.getCurrentMinute(); 17 | Toast.makeText(this, time, Toast.LENGTH_SHORT).show(); 18 | } 19 | -------------------------------------------------------------------------------- /chap03_view/ex2_basic_views.txt: -------------------------------------------------------------------------------- 1 | Goal: Explore the basic views of an Android application. 2 | 3 | Description: 4 | While an Android application has many complex views, most complex views are comprised of the basic views, TextView, EditText, Button, ImageView, and ImageButton. 5 | 6 | Directions: 7 | 8 | 1) Open the layout XML. 9 | 2) Ensure that the root layout is a vertical LinearLayout. 10 | 3) Add an EditText input field. 11 | 4) Add a Button. 12 | 5) Add an ImageView. 13 | 6) Add an ImageButton 14 | 7) Set the android:layout_margin property to 20dp to create some space between the elements. 15 | 16 | 17 | ## Snippets 18 | 19 | // Hint: Switch to Graphical Layout and 20 | // drag the inputs from the left-hand palette 21 | -------------------------------------------------------------------------------- /chap01_app_fundamentals/ex2_textview.txt: -------------------------------------------------------------------------------- 1 | Goal: Explore the XML layout of an Activity. 2 | 3 | Description: 4 | Every activity has an associated XML layout file specified in the onCreate method. The XML file is responsible for describing the elements (TextViews, Buttons, etc) that will be on the screen for that activity. 5 | 6 | Every element has XML properties that describes various behavior such as layout, alignment, color, etc. 7 | 8 | Directions: 9 | 10 | 1) Open the XML layout file (e.g., res/layout/activity_hello_world.xml) 11 | 2) Switch to the text mode. 12 | 3) If there is not already a TextView in the layout, add one. 13 | 4) Modify the android:text property, as shown below. 14 | 15 | 16 | ## Snippets 17 | 18 | -------------------------------------------------------------------------------- /chap01_app_fundamentals/ex5_size_units.txt: -------------------------------------------------------------------------------- 1 | Goal: Explore size units. 2 | 3 | Description: 4 | Android devices have a variety of screen resolutions. In other words, any given physical inch of the screen may contain 100 pixels, 200 pixels, or more, depending on the resolution. Therefore, if we specify measurements in pixels, the element (e.g., a TextView or Button) will appear to be different sizes on different devices. 5 | 6 | Never use px for distance or pt for font size. Instead, use dp for distance and sp for font size. 7 | 8 | Directions: 9 | 10 | 1) Open the layout XML. 11 | 2) For the TextView, add the property android:textSize and set it to 20sp. 12 | 13 | 14 | ## Snippets 15 | 16 | -------------------------------------------------------------------------------- /chap01_app_fundamentals/ex3_strings.txt: -------------------------------------------------------------------------------- 1 | Goal: Modify strings in the strings.xml file. 2 | 3 | Description: 4 | The primary purpose of the strings.xml file is for internationalization. Any string that is ultimately shown to the user (e.g., in a text field, button, or navigation) should be added to the strings.xml file. That way, to add support for a new language simply requires adding an additional strings.xml file for the new language. 5 | 6 | In XML, refer to strings described in the strings.xml file using the syntax @string/my_string. 7 | 8 | Directions: 9 | 10 | 1) Open the XML layout file. 11 | 2) If you have any android:text values that do not refer to an @string, hover your mouse over the warning and read what it says. 12 | 3) Add new strings to the strings.xml file. Note: the keys may not include spaces. 13 | 4) Modify all android:text properties to refer to a string 14 | 15 | 16 | ## Snippets 17 | 18 | -------------------------------------------------------------------------------- /chap03_view/ex1_layout_gravity.txt: -------------------------------------------------------------------------------- 1 | Goal: Explore layout gravity. 2 | 3 | Description: 4 | In Chapter 2, we explored the gravity property. Layout gravity specifies the gravity of an element with respect to its parent. The easiest way to understand this is to try a few layout gravity options and see what happens. 5 | 6 | Directions: 7 | 8 | 1) Open the layout XML. 9 | 2) Add at least three TextViews in a vertical LinearLayout. 10 | 3) For each TextView, add the android:padding property and set it to 30dp. This will create some space between the TextViews. 11 | 4) Set the android:layout_gravity property of the first TextView to "left". 12 | 5) Set the android:layout_gravity property of the second TextView to "center". 13 | 6) Set the android:layout_gravity property of the third TextView to "right". 14 | 15 | 16 | ## Snippets 17 | 18 | // In activity, have three TextViews similar to below 19 | 25 | 26 | -------------------------------------------------------------------------------- /chap07_advanced_views/ex1_toast_inputs.txt: -------------------------------------------------------------------------------- 1 | Goal: Experiment with three basic input views: EditText, CheckBox, and RadioGroup. 2 | 3 | Description: 4 | This is a basic exercise in the usage of a few common input views. 5 | 6 | Directions: 7 | 1) Create a new Activity with an EditText, a CheckBox, and a RadioGroup. 8 | 2) In the onCreate method, find and store each view into an instance variable. 9 | 3) Add a button to the activity and implement a click handler. 10 | a) In the click handler, display a Toast of the current values of the EditText, CheckBox, and RadioGroup. 11 | 12 | 13 | ## Snippets 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | // ... 18 | etVal = (EditText) findViewById(R.id.etVal); 19 | chkVal = (CheckBox) findViewById(R.id.chkVal); 20 | rdgVal = (RadioGroup) findViewById(R.id.rdgVal); 21 | } 22 | 23 | public void toastInputs(View v) { 24 | int selected = rdgVal.getCheckedRadioButtonId(); 25 | RadioButton b = (RadioButton) findViewById(selected); 26 | 27 | String text = etVal.getText() + " | " + chkVal.isChecked() + " | " + b.getText(); 28 | Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); 29 | } 30 | -------------------------------------------------------------------------------- /chap02_user_interface/ex3_layout_params.txt: -------------------------------------------------------------------------------- 1 | Goal: Explore layout widths. 2 | 3 | Description: 4 | There are 3 options for layout width: match_parent, wrap_content, and a dp value. match_parent sets the width to be the same width as the container. wrap_content will size the container to be just large enough to fit its content. A dp value will specify the exact width. 5 | 6 | Directions: 7 | 8 | 1) Open the layout XML. 9 | 2) Add at least 3 TextViews to the vertical LinearLayout. 10 | 3) Set the android:background property to #00FF00, as below. 11 | 4) Set the first TextView width to be wrap_content. 12 | 5) Set the second TextView width to be match_parent. 13 | 6) Set the third TextView width to be 50dp. 14 | 15 | 16 | ## Snippets 17 | 18 | 23 | 24 | 29 | 30 | 35 | -------------------------------------------------------------------------------- /chap02_user_interface/ex2_linear.txt: -------------------------------------------------------------------------------- 1 | Goal: Explore LinearLayout. 2 | 3 | Description: 4 | Layouts (ViewGroups) are containers for Views (e.g., TextViews, Buttons, etc) and may also contain other Layouts. Different layout types have different options for configuring how their contained Views are laid out. 5 | 6 | The easiest to use and the most common Layout type is LinearLayout. LinearLayout either lays out its contained Views in a row or a column, depending on the orientation property. 7 | 8 | Note: the gravity property specifies the alignment of the contained views (e.g., left, center, right, etc). This is different from layout_gravity, which specifies the alignment of the entire container with respect to its parent. 9 | 10 | Directions: 11 | 12 | 1) Open the XML layout. 13 | 2) Change the root layout to be a LinearLayout. 14 | 3) Add the android:orientation property and set it to "vertical". 15 | 4) Add the android:gravity property and set it to "center". 16 | 17 | 18 | ## Snippets 19 | 20 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /chap02_user_interface/ex1_launcher.txt: -------------------------------------------------------------------------------- 1 | Goal: Create a new activity and set it to be the default activity. 2 | 3 | Description: 4 | Every activity in the project must be listed in AndroidManifest.xml, in the application node. Only one of them should contain an intent filter with an action set to MAIN and a category of LAUNCHER. That activity will be the default activity. 5 | 6 | Directions: 7 | 1) Create a new activity. 8 | a) Click on File->New->Activity->Blank Activity 9 | b) The wizard will automatically create the activity, add it to AndroidManifest.xml, create a layout file, and create a menu file. If you want to delete an activity, you should remove the corresponding layout and menu file manually. 10 | 2) Modify the new activity's layout file so that you can recognize it as the second activity. 11 | 3) Modify AndroidManifest.xml to change the default activity. 12 | 4) Run the application and confirm that it starts with the second activity. 13 | 14 | 15 | ## Snippets 16 | 17 | // Android Manifest.xml 18 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /chap06_networking/ex3_async_http_client.txt: -------------------------------------------------------------------------------- 1 | Goal: Download an image asynchronously. 2 | 3 | Description: 4 | Using the built-in URLConnection class and AsyncTasks, it's possible to download images from the internet asynchronously. However, third party libraries exist to provide this functionality in a more convenient way. A popular third party library is AsyncHttpClient, which makes all its network requests asynchronously. 5 | 6 | This library uses anonymous classes to receive the desired method from the developer. 7 | 8 | Directions: 9 | 1) Create an Activity with an ImageView. 10 | 2) Find the URL of an image to test. 11 | 3) In the onCreate method, call downloadImageFromUrl(String address) 12 | 4) Implement downloadImageFromUrl(String address) like the snippet below 13 | 14 | Reference: 15 | 16 | Download the library from: http://loopj.com/android-async-http/ 17 | 18 | 19 | ## Snippets 20 | 21 | // In Java 22 | private void downloadImageFromUrl(String address) { 23 | AsyncHttpClient client = new AsyncHttpClient(); 24 | client.get(address, new 25 | BinaryHttpResponseHandler() { 26 | @Override 27 | public void onSuccess(byte[] image) { 28 | Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length); 29 | ImageView img = (ImageView) findViewById(R.id.ivSmartImage); 30 | img.setImageBitmap(bitmap); 31 | } 32 | } 33 | ); 34 | } 35 | -------------------------------------------------------------------------------- /chap01_app_fundamentals/ex4_lifecycle.txt: -------------------------------------------------------------------------------- 1 | Goal: Explore the Activity lifecycle. 2 | 3 | Description: 4 | The Activity lifecycle is automatically managed by the Android framework. If a user taps on something that switches them into a new activity, the previous activity is paused. The activity is also paused if the user receives a phone call. 5 | 6 | The 3 most commonly implemented activity lifecycle methods are onCreate, onPause, and onResume. Note: if you implement an activity lifecycle method, you MUST call the super implementation. 7 | 8 | Directions: 9 | 1) Open the Activity java file (e.g., src/codepath/HelloWorldActivity.java) 10 | 2) Implement the onCreate, onPause, and onResume methods. 11 | 3) In each method, call the super method. 12 | 4) Add a Log.d message to each method, as shown below. 13 | 5) Run the application and observe the LogCat. 14 | a) In the emulator, click the Home button. Did you see the activity pause in the LogCat? 15 | 16 | 17 | ## Snippets 18 | 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_basic_text_view); 22 | Log.d("DEBUG", "onCreate was just called!"); 23 | } 24 | 25 | protected void onResume() { 26 | super.onResume(); 27 | Log.d("DEBUG", "onResume was just called!"); 28 | } 29 | 30 | protected void onPause() { 31 | super.onPause(); 32 | Log.d("DEBUG", "onPause was just called!"); 33 | } -------------------------------------------------------------------------------- /chap04_interactions/ex3_list_view_clicks.txt: -------------------------------------------------------------------------------- 1 | Goal: Implement handling a click on a row in a ListView. 2 | 3 | Description: 4 | ListViews display a collection of data, which it accesses via an Adapter. Each item in the collection can be rendered in a custom view. In this exercise, we will create a simple ListView with a default view and handle clicks on a row. 5 | 6 | Directions: 7 | 1) In a new Activity, create a ListView that fills the screen. 8 | 2) In the onCreate method of the Activity 9 | a) Create a string Array of countries. 10 | b) Instantiate an array adapter with the string array and a default XML layout, android.R.layout.simple_list_item_1. 11 | c) Set the adapter of the ListView to the array adapter. 12 | d) Set the onItemClickListener of the ListView to display a toast upon clicking on the row. The toast should indicate which country was selected. 13 | 14 | 15 | ## Snippets 16 | 17 | String[] myCountries = { "United States", "Canada", "Mexico", "Japan" }; 18 | adapter = new ArrayAdapter(this, 19 | android.R.layout.simple_list_item_1, myCountries); 20 | 21 | ListView listView = (ListView) findViewById(R.id.lvDemo); 22 | listView.setAdapter(adapter); 23 | 24 | listView.setOnItemClickListener(new OnItemClickListener() { 25 | @Override 26 | public void onItemClick(AdapterView parent, View view, int position, long id) { 27 | String country = adapter.getItem(position); 28 | Toast.makeText(ListViewClicksActivity.this, country, Toast.LENGTH_SHORT).show(); 29 | } 30 | 31 | }); 32 | -------------------------------------------------------------------------------- /chap01_app_fundamentals/ex1_manifest.txt: -------------------------------------------------------------------------------- 1 | Goal: Explore the AndroidManifest file. 2 | 3 | Description: 4 | Every Android application has a file called AndroidManifest.xml that controls application-wide settings such as the display name, icon, theme, and default Activity. 5 | 6 | Directions: 7 | 8 | 1) Open AndroidManifest.xml 9 | a) Change the version code and name to 2 and 2.0. Reminder: version code is an integer value that you can programatically inspect in your application. Version name is displayed to the user. 10 | b) Change the application name 11 | c) Change the application icon 12 | i) Find and download an image, e.g., from Google Images. 13 | ii) Create various resolution sizes for your image. Tip: Use a tool to automatically create the resolution sizes: http://android-ui-utils.googlecode.com/hg/asset-studio/dist/icons-launcher.html 14 | iii) Place the various resolutions into the respective drawable folders. 15 | iv) In AndroidManifest.xml, point to the new drawable. 16 | 17 | 18 | ## Snippets 19 | 20 | // AndroidManifest.xml 21 | 25 | 26 | 27 | 28 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /chap05_user_flows/ex1_explicit_intents.txt: -------------------------------------------------------------------------------- 1 | Goal: Launch other activities in the application via explicit intents. 2 | 3 | Description: 4 | An explicit intent is how to launch one Activity in an application from another Activity in the same application. The intent's primary role is to specify which Activity class is to be launched. However, it can also pass along data which the new Activity can read. 5 | 6 | All activities are associated with an originating intent. Retrieve the originating intent at any time using the method getIntent(). 7 | 8 | Directions: 9 | 1) Add a button to the default activity and label it "Launch". 10 | 2) Register a click handler via XML or Java 11 | 3) In the click handler method, 12 | a) Create an intent, initializing it with the Activity to be launched. 13 | b) Put a string "extra" in the intent. 14 | c) Start the second activity. 15 | 4) In the second Activity, 16 | a) Get the originating intent using getIntent() 17 | b) Get the string "extra" from the intent. 18 | c) Set the text of a TextView to the string "extra". 19 | 20 | 21 | ## Snippets 22 | 23 | // In Activity 24 | Button btnLaunchSecond = (Button) findViewById(R.id.btnLaunchSecond); 25 | btnLaunchSecond.setOnClickListener(new OnClickListener() { 26 | @Override 27 | public void onClick(View v) { 28 | Intent i = new Intent(getBaseContext(), SimpleBundleDemoActivity.class); 29 | i.putExtra("text", "Passed String Extra!"); 30 | startActivity(i); 31 | } 32 | }); 33 | 34 | // In Launched Activity 35 | String initialText = getIntent().getStringExtra("text"); 36 | TextView tvDisplayText = (TextView) findViewById(R.id.tvDisplayText); 37 | tvDisplayText.setText(initialText); 38 | -------------------------------------------------------------------------------- /chap04_interactions/ex1_click_handlers.txt: -------------------------------------------------------------------------------- 1 | Goal: Add a click handler to a button. 2 | 3 | Description: 4 | Click handlers are methods that will be called when a button is called. Click handlers can be registered either in the XML or in Java. Whenever possible, add the click handler in XML. However, in some situations, such as dynamically created buttons, the click handler must be registered in Java using anonymous classes. 5 | 6 | Directions: 7 | 1) Create a view that has two buttons. 8 | 2) For the first button, 9 | a) Add the android:onClick property as in the snippet below, and set it to "firstButtonClicked" 10 | b) Add the method "void firstButtonClicked(View view)" 11 | c) Log the button click. 12 | 3) For the second button, 13 | a) In the onCreate method of the activity, get a handle to the second button using findViewById, and set the OnClickListener. 14 | b) Log the button click. 15 | 16 | 17 | ## Snippets 18 | 19 | // First button with xml onClick 20 |