User Tools

Site Tools


android-labs-s16:android_tutorial_2

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:

  • Clone: creates a local copy of a remote repository (not localhost)
  • Add: adds current file changes to the stage, pending commit.
  • Commit: Takes file changes in stage into account and commits them to the git repository (local-only if remote repository is enabled) (must be done before push to update remote repository properly)
  • Push: Takes file changes specified by the previous commit and pushes them to the remote repository. Changes will not be applied if the remote repositories previous commit doesn’t match the local-repositories last commit.
  • Branch: Takes the current loca/remote-repository (master branch if no other branches exist) and creates a copy of it on the repository with a different name. This enables non-linear software development by enabling multiple groups of people to code separate portions of an application simultaneously.
  • Merge: Takes two branches and compares the changes in each branch and attempts to merge the code into a single branch. If there are conflicts, the user chooses which code to keep and which code to reject

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

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:

  1. Implement the Player 1 button clicks (hint: call a new fragment)
  2. Implement the Player 2 button clicks (hint: handle the game logic)
  3. Implement the AlertDialog

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.

android-labs-s16/android_tutorial_2.txt · Last modified: 2016/02/05 14:08 by james