I have an app that counts the score in a basketball match. There are three buttons for each team to increase the score (+3, +2, free throw).
I want to create a button to return back in case the user click a button for mistake. Without creating three separate buttons for each score (+3,+2,+1). But i don't really know how to transform this in Java code.
Something like: Score=score-lastNumberAdded. Sorry for my english. This is the code:
XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="Team A"
android:textSize="14sp"
android:textColor="#616161"
android:fontFamily="sans-serif-medium"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="0"
android:textSize="56sp"
android:textColor="#000000"
android:fontFamily="sans-serif-light"
android:layout_marginBottom="24dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="+3 Points"
android:onClick="addThreeforTeamA" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="+2 Points"
android:onClick="addTwoforTeamA" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="+1 Point"
android:onClick="addOneforTeamA" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:layout_marginTop="16dp"
android:layout_marginBottom="50dp"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="Team B"
android:textSize="14sp"
android:textColor="#616161"
android:fontFamily="sans-serif-medium"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="0"
android:textSize="56sp"
android:textColor="#000000"
android:fontFamily="sans-serif-light"
android:layout_marginBottom="24dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="+3 Points"
android:onClick="addThreeforTeamB" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="+2 Points"
android:onClick="addTwoforTeamB" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="+1 Point"
android:onClick="addOneforTeamB" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Reset"
android:onClick="reset" />
</RelativeLayout>
JAVA
package com.example.android.courtcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int scoreTeamA=0;
int scoreTeamB=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 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));
}
public void addThreeforTeamA(View v) {
scoreTeamA+=3;
displayForTeamA(scoreTeamA);
}
public void addTwoforTeamA(View v) {
scoreTeamA+=2;
displayForTeamA(scoreTeamA);
}
public void addOneforTeamA(View v) {
scoreTeamA+=1;
displayForTeamA(scoreTeamA);
}
public void addThreeforTeamB(View v) {
scoreTeamB+=3;
displayForTeamB(scoreTeamB); }
public void addTwoforTeamB(View v) {
scoreTeamB+=2;
displayForTeamB(scoreTeamB); }
public void addOneforTeamB(View v) {
scoreTeamB+=1;
displayForTeamB(scoreTeamB); }
public void reset(View v) {
scoreTeamA=0;
scoreTeamB=0;
displayForTeamA(scoreTeamA);
displayForTeamB(scoreTeamB); }
}