Skip to content

Instantly share code, notes, and snippets.

@kristinababikova
Created March 22, 2017 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristinababikova/d2eb70d88da99976127450ff8a470b9b to your computer and use it in GitHub Desktop.
Save kristinababikova/d2eb70d88da99976127450ff8a470b9b to your computer and use it in GitHub Desktop.
counterkeeper
package com.example.android.courtcounter;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
int scoreTeamB = 0;
private TextView timer;
private Button buttonStart, buttonCancel;
private ToggleButton buttonPause;
private boolean isPaused = false;
private boolean isCanceled = false;
private long remainingTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer=(TextView)findViewById(R.id.Timer);
buttonStart=(Button)findViewById(R.id.buttonStart);
buttonCancel=(Button)findViewById(R.id.buttonCancel);
buttonPause=(ToggleButton)findViewById(R.id.buttonPause);
buttonCancel.setEnabled(false);
buttonPause.setEnabled(false);
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonStart.setEnabled(false);
buttonPause.setEnabled(true);
buttonCancel.setEnabled(true);
isPaused = false;
isCanceled = false;
long millisInFuture = 60000; ////10sec because it is in milliseconds
long countDownInterval = 1000; /////1sec
new CountDownTimer(millisInFuture, countDownInterval){
@Override
public void onTick(long millisUntilFinished) {
if (isPaused || isCanceled){
cancel();
}else {
timer.setText(""+millisUntilFinished/1000);
remainingTime = millisUntilFinished;
}
}
@Override
public void onFinish() {
timer.setText("Times up!");
}
}.start();
}
});
buttonPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (buttonPause.isChecked()){
isPaused = true;
}else {
isPaused = false;
long millisInFuture = remainingTime;
long countDownInterval = 1000; /////1sec
new CountDownTimer(millisInFuture, countDownInterval){
@Override
public void onTick(long millisUntilFinished) {
if (isPaused || isCanceled){
cancel();
}else {
timer.setText(""+millisUntilFinished/1000);
remainingTime = millisUntilFinished;
}
}
@Override
public void onFinish() {
timer.setText("Times up!");
}
}.start();
}
}
});
buttonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isCanceled = true;
timer.setText(00);
buttonStart.setEnabled(true);
buttonPause.setEnabled(false);
buttonCancel.setEnabled(false);
}
});
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
/**
* Displays the given score for Team B.
*/
public void displayForTeamB(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_b_score);
scoreView.setText(String.valueOf(score));
}
/**
* Adding three points for Team A.
*/
public void addThreeForTeamA (View v){
scoreTeamA = scoreTeamA + 3;
displayForTeamA(scoreTeamA);
}
/**
* Adding three points for Team B.
*/
public void addThreeForTeamB (View v){
scoreTeamB = scoreTeamB + 3;
displayForTeamB(scoreTeamB);
}
/**
* Adding two points for Team A.
*/
public void addTwoForTeamA (View view){
scoreTeamA = scoreTeamA + 2;
displayForTeamA(scoreTeamA);
}
/**
* Adding two points for Team B.
*/
public void addTwoForTeamB (View view){
scoreTeamB = scoreTeamB + 2;
displayForTeamB(scoreTeamB);
}
/**
* Adding one point for Team A.
*/
public void addOneForTeamA (View view){
scoreTeamA = scoreTeamA + 1;
displayForTeamA(scoreTeamA);
}
/**
* Adding one point for Team B.
*/
public void addOneForTeamB (View view){
scoreTeamB = scoreTeamB + 1;
displayForTeamB(scoreTeamB);
}
public void resetScore (View view){
scoreTeamA = 0;
scoreTeamB = 0;
displayForTeamA(scoreTeamA);
displayForTeamB(scoreTeamB);
}
}
<?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">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/training" />
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="@android:color/white"
android:cursorVisible="false"
android:ems="10"
android:gravity="center_horizontal"
android:hint="RAMBO #1"
android:inputType="textCapCharacters"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white" />
<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_horizontal"
android:padding="4dp"
android:text="0"
android:textColor="@android:color/white"
android:textSize="48sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="4dp"
android:onClick="addThreeForTeamA"
android:text="push-up" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="4dp"
android:onClick="addTwoForTeamA"
android:text="pull-up" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="addOneForTeamA"
android:text="crunch" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="4dp"
android:onClick="addOneForTeamB"
android:text="squat" />
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="@android:color/white" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="@android:color/white"
android:cursorVisible="false"
android:ems="10"
android:gravity="center_horizontal"
android:hint="RAMBO #2"
android:inputType="textCapCharacters"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white" />
<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_horizontal"
android:padding="4dp"
android:text="0"
android:textColor="@android:color/white"
android:textSize="48sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp"
android:onClick="addThreeForTeamB"
android:text="push-up" />/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="4dp"
android:onClick="addTwoForTeamB"
android:text="pull-up" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="addOneForTeamB"
android:text="crunch" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="4dp"
android:onClick="addOneForTeamB"
android:text="squat" />
</LinearLayout>
</LinearLayout>
<ToggleButton
android:id="@+id/buttonPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:layout_alignTop="@+id/buttonStart"
android:checked="false"
android:text="ToggleButton"
android:textColor="#000000"
android:textOff="Pause"
android:textOn="Resume"
android:textColorLink="@android:color/background_light" />
<Button
android:id="@+id/buttonCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonPause"
android:layout_alignBottom="@+id/buttonPause"
android:layout_toEndOf="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="Cancel"
android:textColor="#000000" />
<Button
android:id="@+id/buttonStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"
android:textColor="#000000"
android:layout_above="@+id/button2"
android:layout_toLeftOf="@+id/buttonPause"
android:layout_toStartOf="@+id/buttonPause"
android:layout_marginBottom="14dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="resetScore"
android:text="Reset"
android:textColor="@android:color/black"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="11dp" />
<TextView
android:id="@+id/Timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="60 sec"
android:textSize="30sp"
android:textAllCaps="true"
android:layout_above="@+id/buttonPause"
android:layout_centerHorizontal="true"
android:layout_marginBottom="17dp"
android:textColor="@android:color/black"
android:textStyle="bold" />
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment