Android Tutorial 1 Outline
Assumptions: To facilitate the entire class, we assume your system meets minimum requirements, Java is installed, and you have the book available.
Install Tools and IDE (pg 15-25): This part goes over Android Studio setup and the basics of android. Would be very beneficial if you came to class having read these pages.
Make a New Project (pg 27-36): Read about how to create a project and start a new one for our purposes today. Name it anything you like. Use API level 19 or higher (we will talk about device compatibility later). When you get to the screen shown below, select the “Empty Activity” option.
Emulators (pg 40-41): Read about Android Studio’s built in emulator and set it up if you desire to do so. You may also use a 3rd party emulator such as Genymotion or a physical device.
Screen Support + SDK (pg 89-91): Read this and make sure you have the right minSDK and targetSDK. Screen Support shouldn’t be an issue for this tutorial, but is something you should know and think about while you are building your application.
Basic Widgets: (pg 131-164): Read about basic widgets (Button, TextView, ImageView, EditText): Follow along with the book and add one of each basic widget to your new project. The book gives an outline of how to add each widget to the layout and how to manipulate each widget to appear and act the way you want.
Debugging (pg 165-171): At this point you have probably run into a bug. Read about debugging and reference here if you run into problems. Android Studio has multiple tools that will drastically speed up your development process.
LinearLayouts (pg 173-180): Read about Linear Layouts. Now put all 4 basic widgets into a linear layout so they are displayed one after another in a vertical fashion.
Relative Layouts + Overlap (pg 195-200):Read about Relative Layouts. Now Take your 4 basic widgets and put them in a RelativeLayout. Make the ImageView widget serve as a background for the activity, and have the other elements overlap the image. Display the 3 top images in a similar fashion to the LinearLayout. Play around with different attributes and try to arrange the widgets in a way that promotes “good” UX.
NEW MATERIAL FOR MONDAY
Important: By the end of today’s lab (Monday) you should be comfortable with basic UI elements. You should also be able to create a new Android project, successfully build your project with Gradle, and run it on an emulator or physical device. If you aren’t comfortable with anything it is very important you ask for help.
You are going to build a guest-list application that looks something like this:
You have total artistic control over this project and I encourage you to make it look as interesting as possible. When you start the application the user is brought to a screen that asks for first name, last name, email address, and phone number. Then, below a submit button, there should be a running list of all names entered(during that session).
Toast: Toast is an extremely useful tool that displays small messages across the bottom of the devices screen. Make a toast “‘name’ has been added!” after the submit button is pressed. (hint: start typing Toast.makeText in Android Studio).
Warning about Toasts: If you are using Toasts to display information to the user (not the developers/testers) you should probably use some other method. If the user doesn’t need to acknowledge the information, they probably don’t need it flashed in their face.
Adapters: pg 225-251 Now for the tricky part; displaying the guest list. Read about AdapterViews and Adapters. Pay special attention to pages 225-232, as they give a detailed outline of how to use a ListView. The end of this section has details on customizing adapters; something you will have to do for homework. Add a ListView to your application showing the first and last name of everyone who has signed in.
NEW STUFF FOR FEBRUARY 5TH
Instructions: Read through the entirety of the tutorial before starting. There are detailed steps on completing a Rock Paper Scissors game at the end.
Git:
Git is a source code manager for software development. The most common use of git is version control. Version control software tracks changes to a file or directory and enables you to recall previous versions later. This feature of git enables users to recover code that they may have deleted, rewritten, or broken. Git also makes backing up of changes to a remote location simple. The most used git commands are summarized below:
Intents:
http://developer.android.com/guide/components/intents-filters.html
In this lab we will have multiple activities. The activity containing gameplay will be separate from the MainActivity, and will need to be started from the MainActivity to begin gameplay. Contained within the gameplay activity will be fragments for each player (more on that below). Intents are a type of messaging object used to request/perform an action from another application component (such as an Activity). Intents can also be used to start a Service, which is an application component that performs actions in the background, without user interaction. Another use of Intents is a broadcast. A broadcast is a message that any application can receive and is handled by the system. Broadcasts are mostly used by the system for system events, such as low battery or when the system boots up.
Fragments:
Here are some links to full details about fragments: http://developer.android.com/guide/components/fragments.html
Creating Fragments: http://developer.android.com/training/basics/fragments/creating.html
Interacting with Fragments: http://developer.android.com/training/basics/fragments/fragment-ui.html
Basically, fragments are modular pieces of an activity that have their own lifecycle. They are useful because they can be added and removed to an activity independently and interact with each other. Activities use a FragmentManager which is an interface for fragments in an activity.
To give you a basic introduction to using fragments you need 3 pieces of code: A container, the implemented fragment, and the instantiation of the fragment. Reference the Android Developer Guide for fully detailed explanations.
This is a basic template for a Fragment implementation:
public static class PlayFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.example_fragment, container, false); } }
There are two ways of making a Fragment show up in an activity. Either way, You will need to declare a View of some sort in the fragment_layout.xml . You can statically declare a Fragment view object, which will then be inflated automatically by its parent activity:
<fragment android:name="com.example.android.fragments.ExampleFragment" android:id="@+id/headlines_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" />
or you can use ‘fragment container’ and instantiate the fragment dynamically. This is the way you should do it for this tutorial:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />
Then when you want the fragment to appear, you would call this code:
getFragmentManager() .beginTransaction() .replace(R.id.fragment_container, new PlayFragment()) .addToBackStack(null) .commit();
You can instantiate fragments with Java’s new operator (ex. new ExampleFragment()), however proper convention is to use a newInstance constructor:
PlayFragment.newInstance(null,null)
This is something that the Android Studio Wizard sets up for you automatically when creating a new Fragment (try creating a new fragment with “make fragment factory methods” checked if you're interested) .
Carefully trace through the code we gave you and try to figure out how the arguments work: you put parcelable objects into a bundle and pass that into the fragment where you extract the objects and use them as arguments.
This may seem like a confusing way of doing something simple, and you’re right. There are “simpler” ways of accomplishing the same task. However, using this style of implementation provides another layer of abstraction, making your code modular and more easily changeable and integratable. For this tutorial you MUST use this convention. To make this easier for you, we have set up the argument handling in its entirety (all you need to do is pass in the proper arguments).
Part 1:
Download and unzip the project file from piazza
Share it to your github account (set up a github account if you haven't already)
Part 2:
Start a new branch for this part and name it part1. In this part you have 2 tasks to complete:
Add a play button to MainActivity and implement the button clicks (hint: use the playPressed method) Implement the playPressed method to call the game play activity using intents as explained previously.
Part 3:
Start a new branch for this part. Before doing so, make sure you commit your previous changes and merge them to the main branch. You now have 3 tasks to complete:
You should now be able to get to the Player 1 move screen, but nothing happens when the buttons are pressed. It is your job to handle the game logic and switch to a new instance of PlayFragment when a button is pressed, and get Player 2’s move.
We have declared String resources for “Rock”, “Paper”, and “Scissors” that you should use in both xml and java code at all times; there are plenty of examples how to do this in the existing code as well as online. Using these resources should make the game logic easy.
To display the winner of the game, you should use an AlertDialog and give the user the option to rematch (Move to the Player 1 screen again) or quit (go back to the start screen). We have given you a nearly finished method (displayWinner(String winner)), you just need to implement the button clicks.
Here's a link to all you would ever need to know about Dialogs: http://developer.android.com/guide/topics/ui/dialogs.html
As you will see, we have fully implemented the AlertDialog, compare it to what you see on the Android Developer Page and play around with the different functionalities of dialogs.