Skip to content

Instantly share code, notes, and snippets.

@csarq
Last active April 19, 2018 13:31
Show Gist options
  • Save csarq/34a78091d9760b96f1853a67f0ae31dc to your computer and use it in GitHub Desktop.
Save csarq/34a78091d9760b96f1853a67f0ae31dc to your computer and use it in GitHub Desktop.
Basic Table Tennis Scoring App
<!-- This is the main xml file
Place this file in the "layout" folder -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#29000000"
android:orientation="vertical"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<include
layout="@layout/my_video_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"/>
<android.support.constraint.Guideline
android:id="@+id/topMargin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.05" />
<android.support.constraint.Guideline
android:id="@+id/topMargin2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.22" />
<android.support.constraint.Guideline
android:id="@+id/leftMargin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.052083332" />
<android.support.constraint.Guideline
android:id="@+id/rightMargin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.95" />
<android.support.constraint.Guideline
android:id="@+id/centreGuide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<android.support.constraint.Guideline
android:id="@+id/pointMargin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.80" />
<android.support.constraint.Guideline
android:id="@+id/bottomMargin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.95" />
<TextView
android:id="@+id/playersResults"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="@id/topMargin1"
app:layout_constraintLeft_toRightOf="@id/leftMargin"
app:layout_constraintRight_toLeftOf="@id/rightMargin"
android:layout_height="wrap_content"
android:background="@drawable/reset"
android:fontFamily="sans-serif-light"
android:padding="10dp"
android:text="Players A and B"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="25sp"
android:textStyle="bold"
app:fontFamily="@font/codystar" />
<LinearLayout
android:id="@+id/linearLeft"
android:layout_width="0dp"
android:padding="15dp"
app:layout_constraintLeft_toRightOf="@id/leftMargin"
app:layout_constraintRight_toLeftOf="@id/centreGuide"
app:layout_constraintTop_toBottomOf="@id/topMargin2"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:background="@drawable/corners_purple"
android:orientation="vertical">
<TextView
android:id="@+id/player_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginBottom="15dp"
android:fontFamily="sans-serif-light"
android:text="pnt"
android:textColor="#000000"
android:textSize="50sp"
app:fontFamily="@font/monoton" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:text="Sets"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
app:fontFamily="@font/codystar" />
<TextView
android:id="@+id/player_a_sets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:text=""
android:textColor="#000000"
android:textSize="30sp"
app:fontFamily="@font/monoton" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/linearLeft"
app:layout_constraintLeft_toRightOf="@id/leftMargin"
app:layout_constraintRight_toLeftOf="@id/centreGuide"
app:layout_constraintBottom_toTopOf="@id/pointMargin"
android:background="@drawable/circle_purple"
android:onClick="addPointForPlayerA"
android:text="Point"
android:textColor="#000000"
android:textAllCaps="true"
android:textStyle="bold"
app:fontFamily="@font/codystar"/>
<!-- Right-hand section here
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~-->
<LinearLayout
android:layout_width="0dp"
android:padding="15dp"
android:id="@+id/linearRight"
app:layout_constraintLeft_toRightOf="@id/centreGuide"
app:layout_constraintRight_toLeftOf="@id/rightMargin"
app:layout_constraintTop_toBottomOf="@id/topMargin2"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:background="@drawable/corners_green"
android:orientation="vertical">
<TextView
android:id="@+id/player_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginBottom="15dp"
android:fontFamily="sans-serif-light"
android:text="pnt"
android:textColor="#000000"
android:textSize="50sp"
app:fontFamily="@font/monoton" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:text="Sets"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
app:fontFamily="@font/codystar" />
<TextView
android:id="@+id/player_b_sets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:text=""
android:textColor="#000000"
android:textSize="30sp"
app:fontFamily="@font/monoton" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/linearRight"
app:layout_constraintLeft_toRightOf="@id/centreGuide"
app:layout_constraintRight_toLeftOf="@id/rightMargin"
app:layout_constraintBottom_toTopOf="@id/pointMargin"
android:background="@drawable/circle_green"
android:onClick="addPointForPlayerB"
android:text="Point"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
app:fontFamily="@font/codystar"/>
<Button
app:layout_constraintLeft_toRightOf="@id/leftMargin"
app:layout_constraintRight_toLeftOf="@id/rightMargin"
app:layout_constraintBottom_toTopOf="@id/bottomMargin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/reset"
android:onClick="Reset"
android:text="Reset"
android:textAllCaps="true"
android:textColor="#000000"
android:textStyle="bold"
app:fontFamily="@font/codystar" />
</android.support.constraint.ConstraintLayout>
<!-- Rounded green button
Place this xml file in the "drawable" folder -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:endColor="#ded9f9e6"
android:startColor="#de0a5e3a"
android:type="linear" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:radius="40dp" />
</shape>
<!-- Rounded purple button
Place this xml file in the "drawable" folder -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:endColor="#dee3cefc"
android:startColor="#de4a158a"
android:type="linear" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:radius="40dp" />
</shape>
<!-- Two rounded corners - green
Place this xml file in the "drawable" folder -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:endColor="#ded9f9e6"
android:startColor="#de0a5e3a"
android:type="linear" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:radius="40dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="0dp"/>
</shape>
<!-- Two rounded corners - purple
Place this xml file in the "drawable" folder -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:endColor="#dee3cefc"
android:startColor="#de4a158a"
android:type="linear" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:radius="30dp"
android:bottomLeftRadius="0dp"
android:topRightRadius="0dp"/>
</shape>
/** I apologise for this messy code! It's because I'm a complete beginner, but I'd really appreciate any advice in improving it/cleaning it up!
The majority has been gathered from various internet search results, and so I'm making this available for any other learners wanting
to play around with the code/app
- Remember to replace occurrances of "ball" with the name of your video file
- The background video wouldn't run for me on the emulator, so try running directly to your android device
- Place your video in a "raw" folder that sits within the "res" folder.
*/
package com.example.android.tabletennisscore;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
int scorePlayerA = 0;
int scorePlayerB = 0;
int setsPlayerA = 0;
int setsPlayerB = 0;
int gamesPlayerA = 0;
String winner = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView videoview = findViewById(R.id.videoView);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.ball);
videoview.setVideoURI(uri);
videoview.start();
videoview.setOnCompletionListener ( new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoview.start();
}
});
displayForPlayerA(0);
displayForPlayerB(0);
}
/** ~~~~ to add: Once game total reaches 5 compare this to other player and if it's more than 2 points above oponent
* then delcare the winner. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ~~~~~~Once game reaches end then reset everything automatically
*
* declares loser also if player is less than oponent?:::::::::::::::::::::::::::
* if (setsPlayerA < setsPlayerB) {
winner = "Pants";
displayForPlayerAResult(winner);
}~~~~~~~~~~~~~~~~~~~~~~~~
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/**
* Increase the Points score for Player A by 1 point.
*/
public void addPointForPlayerA(View v) {
scorePlayerA = scorePlayerA + 1;
displayForPlayerA(scorePlayerA);
if (scorePlayerA < 11) {
displayForPlayersResults("");
displayForPlayerASets(setsPlayerA);
}
if (scorePlayerA == 11) {
winner = "Player A wins Set!";
setsPlayerA = setsPlayerA + 1;
displayForPlayersResults(winner);
displayForPlayerASets(setsPlayerA);
scorePlayerA = 0;
}
if (setsPlayerA == 5) {
/**gamesPlayerA = gamesPlayerA + 1;
displayForPlayerAGames(gamesPlayerA);*/
winner = "Player A wins match!";
displayForPlayersResults(winner);
/**displayForPlayerA(0);*/
setsPlayerA = 0;
}
}
/**
* Displays the current points for Player A.
*/
public void displayForPlayerA(int score) {
TextView scoreView = (TextView) findViewById(R.id.player_a_score);
scoreView.setText(String.valueOf(score));
}
public void displayForPlayersResults(String i) {
TextView r = (TextView) findViewById(R.id.playersResults);
r.setText("" + i);
}
/**
* Displays the sets for Player A.
*/
public void displayForPlayerASets(int i) {
TextView r = (TextView) findViewById(R.id.player_a_sets);
r.setText("" + i + " of 5");
}
/**
* Displays the games for Player A.
public void displayForPlayerAGames(int i) {
TextView r = (TextView) findViewById(R.id.player_a_games);
r.setText("" + i);
} */
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/**
* Increase the score for Player B by 1 point.
*/
public void addPointForPlayerB(View v) {
scorePlayerB = scorePlayerB + 1;
displayForPlayerB(scorePlayerB);
if (scorePlayerB < 11) {
displayForPlayersResults("");
displayForPlayerBSets(setsPlayerB);
}
if (scorePlayerB == 11) {
winner = "Player B Wins Set!";
setsPlayerB = setsPlayerB + 1;
displayForPlayersResults(winner);
displayForPlayerBSets(setsPlayerB);
scorePlayerB = 0;
}
if (setsPlayerB == 5) {
/**gamesPlayerB = gamesPlayerB + 1;
displayForPlayerBGames(gamesPlayerB);*/
winner = "Player B wins match!";
displayForPlayersResults(winner);
/**displayForPlayerB(0);*/
setsPlayerB = 0;
}
}
/**
* Displays the current points for Player A.
*/
public void displayForPlayerB(int score) {
TextView scoreView = (TextView) findViewById(R.id.player_b_score);
scoreView.setText(String.valueOf(score));
}
/**
* Displays the result for Player A. //wins
*/
/**public void displayForPlayerBResult(String i) {
TextView r = (TextView) findViewById(R.id.player_b_result);
r.setText("" + i);
}
* Displays the sets for Player A.
*/
public void displayForPlayerBSets(int i) {
TextView r = (TextView) findViewById(R.id.player_b_sets);
r.setText("" + i + " of 5");
}
/**
* Displays the games for Player A.
public void displayForPlayerBGames(int i) {
TextView r = (TextView) findViewById(R.id.player_b_games);
r.setText("" + i);
}*/
/**
* Reset the score for Players B and A
*/
public void Reset(View v) {
scorePlayerB = 0;
scorePlayerA = 0;
winner = "";
setsPlayerA = 0;
setsPlayerB = 0;
displayForPlayerB(scorePlayerB);
displayForPlayerA(scorePlayerA);
displayForPlayersResults(winner);
displayForPlayerASets(setsPlayerA);
displayForPlayerBSets(setsPlayerB);
}
}
<!-- Place this xml file in the "layout" folder -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_gravity="center" />
</RelativeLayout>
<!-- Blue rounded reset button and top message display section
Place this xml file in the "drawable" folder -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:endColor="#deced6fc"
android:startColor="#de15278a"
android:type="linear" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:radius="40dp" />
</shape>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment