This is for Udacity's Android for Beginners Course submission for the Score Keeper App project. This app keeps the score for a single volleyball game and will announce the winner of the game once the winning score is reached.
Created
February 5, 2017 16:05
-
-
Save novembergave/c5100a9fbfb049c810badaa71f368a25 to your computer and use it in GitHub Desktop.
Android for Beginners: Score Keeper App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/title_text_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:padding="10dp" | |
android:text="@string/app_name" | |
android:textSize="30sp" /> | |
<LinearLayout | |
android:id="@+id/score_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_below="@id/title_text_view" | |
android:orientation="horizontal" | |
android:paddingBottom="20dp"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:fontFamily="sans-serif-medium" | |
android:gravity="center" | |
android:padding="8dp" | |
android:text="Team A" | |
android:textColor="@android:color/holo_red_dark" | |
android:textSize="14dp" /> | |
<TextView | |
android:id="@+id/team_a_score" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:fontFamily="sans-serif-light" | |
android:gravity="center" | |
android:padding="24dp" | |
android:text="0" | |
android:textColor="@android:color/black" | |
android:textSize="56dp" /> | |
<Button | |
android:id="@+id/team_a_miss" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="24dp" | |
android:layout_marginRight="24dp" | |
android:backgroundTint="@android:color/holo_blue_light" | |
android:onClick="aAddScore" | |
android:text="@string/miss" /> | |
<Button | |
android:id="@+id/team_a_oob" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="24dp" | |
android:layout_marginRight="24dp" | |
android:backgroundTint="@android:color/holo_blue_light" | |
android:onClick="aAddScore" | |
android:text="@string/out_of_bounds" /> | |
</LinearLayout> | |
<View | |
android:layout_width="1dp" | |
android:layout_height="match_parent" | |
android:background="@android:color/holo_orange_dark"> | |
</View> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:fontFamily="sans-serif-medium" | |
android:gravity="center" | |
android:padding="8dp" | |
android:text="Team B" | |
android:textColor="@android:color/holo_blue_dark" | |
android:textSize="14dp" /> | |
<TextView | |
android:id="@+id/team_b_score" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:fontFamily="sans-serif-light" | |
android:gravity="center" | |
android:padding="24dp" | |
android:text="0" | |
android:textColor="@android:color/black" | |
android:textSize="56dp" /> | |
<Button | |
android:id="@+id/team_b_miss" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="24dp" | |
android:layout_marginRight="24dp" | |
android:backgroundTint="@android:color/holo_blue_light" | |
android:onClick="bAddScore" | |
android:text="@string/miss" /> | |
<Button | |
android:id="@+id/team_b_oob" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="24dp" | |
android:layout_marginRight="24dp" | |
android:backgroundTint="@android:color/holo_blue_light" | |
android:onClick="bAddScore" | |
android:text="@string/out_of_bounds" /> | |
</LinearLayout> | |
</LinearLayout> | |
<TextView | |
android:id="@+id/winner_box" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_below="@id/score_view" | |
android:gravity="center" | |
android:padding="10dp" | |
android:textSize="30sp" | |
android:visibility="gone" /> | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@id/winner_box" | |
android:layout_centerHorizontal="true" | |
android:padding="50dp" | |
android:src="@drawable/volleyball" /> | |
<Button | |
android:id="@+id/reset_button" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:backgroundTint="@android:color/holo_blue_light" | |
android:onClick="reset" | |
android:text="Reset" | |
android:textSize="20sp" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.novembergave.apps.volleyballscorecounter; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
Button mAMissButton; | |
Button mAOOBButton; | |
Button mBMissButton; | |
Button mBOOBButton; | |
TextView mWinnerTV; | |
TextView mAScoreTV; | |
TextView mBScoreTV; | |
int TeamAScore; | |
int TeamBScore; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mAScoreTV = (TextView) findViewById(R.id.team_a_score); | |
mBScoreTV = (TextView) findViewById(R.id.team_b_score); | |
mWinnerTV = (TextView) findViewById(R.id.winner_box); | |
mAMissButton = (Button) findViewById(R.id.team_a_miss); | |
mAOOBButton = (Button) findViewById(R.id.team_a_oob); | |
mBMissButton = (Button) findViewById(R.id.team_b_miss); | |
mBOOBButton = (Button) findViewById(R.id.team_b_oob); | |
TeamAScore = 0; | |
TeamBScore = 0; | |
} | |
public void aAddScore(View v) { | |
TeamAScore++; | |
displayForTeamA(TeamAScore); | |
// Checks if the team has won and if so, disable the buttons | |
if ((TeamAScore >= 25) && ((TeamAScore - TeamBScore) >= 2)) { | |
String winnerText = getString(R.string.team) + " " + | |
getString(R.string.a) + " " + getString(R.string.wins); | |
mWinnerTV.setText(winnerText); | |
mWinnerTV.setVisibility(View.VISIBLE); | |
mAMissButton.setEnabled(false); | |
mAOOBButton.setEnabled(false); | |
mBMissButton.setEnabled(false); | |
mBOOBButton.setEnabled(false); | |
} | |
} | |
public void bAddScore(View v) { | |
TeamBScore++; | |
displayForTeamB(TeamBScore); | |
// Checks if the team has won and if so, disable the buttons | |
if ((TeamBScore >= 25) && ((TeamBScore - TeamAScore) >= 2)) { | |
String winnerText = getString(R.string.team) + " " + | |
getString(R.string.b) + " " + getString(R.string.wins); | |
mWinnerTV.setText(winnerText); | |
mWinnerTV.setVisibility(View.VISIBLE); | |
mAMissButton.setEnabled(false); | |
mAOOBButton.setEnabled(false); | |
mBMissButton.setEnabled(false); | |
mBOOBButton.setEnabled(false); | |
} | |
} | |
public void reset(View v) { | |
TeamAScore = 0; | |
TeamBScore = 0; | |
displayForTeamA(TeamAScore); | |
displayForTeamB(TeamBScore); | |
mWinnerTV.setVisibility(View.GONE); | |
// Re-enable the buttons | |
mAMissButton.setEnabled(true); | |
mAOOBButton.setEnabled(true); | |
mBMissButton.setEnabled(true); | |
mBOOBButton.setEnabled(true); | |
} | |
/** | |
* Displays the given score for Team A. | |
*/ | |
public void displayForTeamA(int score) { | |
mAScoreTV.setText(String.valueOf(score)); | |
} | |
/** | |
* Displays the given score for Team B. | |
*/ | |
public void displayForTeamB(int score) { | |
mBScoreTV.setText(String.valueOf(score)); | |
} | |
} |
Author
novembergave
commented
Feb 5, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment