In: Computer Science
Create a Scorekeeper app in android studio by using java language
- Layouts | Widgets
CODE:
MainActivity.java
package com.akshansh.score; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioGroup; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener { private TextView teamScoreRedTextView; private TextView teamScoreBlueTextView; private int incrementBy; private int teamRedScore; private int teamBlueScore; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); teamRedScore = 0; teamBlueScore = 0; //finding all the views by ids, refer to the xml page for the ids, //the names are self explanatory RadioGroup radioGroup = findViewById(R.id.radio_group); teamScoreRedTextView = findViewById(R.id.team_score_red); teamScoreBlueTextView = findViewById(R.id.team_score_blue); teamScoreBlueTextView.setText(Integer.toString(teamBlueScore)); teamScoreRedTextView.setText(Integer.toString(teamRedScore)); Button upButtonRed = findViewById(R.id.buttonUp_red); Button downButtonRed = findViewById(R.id.buttonDown_red); Button upButtonBlue = findViewById(R.id.buttonUp_blue); Button downButtonBlue = findViewById(R.id.buttonDown_blue); //setting onClickListeners for these vie radioGroup.setOnCheckedChangeListener(this); upButtonRed.setOnClickListener(this); downButtonRed.setOnClickListener(this); upButtonBlue.setOnClickListener(this); downButtonBlue.setOnClickListener(this); } @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { //selecting the increment value using the radio button switch (i){ case 1: incrementBy = 1; break; case 2: incrementBy = 2; break; case 3: incrementBy = 3; break; } } @Override public void onClick(View view) { int id = view.getId(); //according to the buttons clicked the respective functions are performed switch (id){ case R.id.buttonUp_blue: teamBlueScore += incrementBy; teamScoreBlueTextView.setText(Integer.toString(teamBlueScore)); break; case R.id.buttonDown_blue: teamBlueScore -= incrementBy; teamScoreBlueTextView.setText(Integer.toString(teamBlueScore)); break; case R.id.buttonUp_red: teamRedScore += incrementBy; teamScoreRedTextView.setText(Integer.toString(teamRedScore)); break; case R.id.buttonDown_red: teamRedScore -= incrementBy; teamScoreRedTextView.setText(Integer.toString(teamRedScore)); break; } } }
_____________________________________________
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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" tools:context=".MainActivity"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Team Red" android:textSize="20sp" android:textColor="#EC0519" android:textStyle="bold" android:layout_marginStart="16dp" android:layout_marginTop="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Team Blue" android:textStyle="bold" android:textSize="20sp" android:textColor="#065FF4" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/team_score_red" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:text="0" android:textColor="#000000" android:textSize="20sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="@+id/textView2" app:layout_constraintStart_toStartOf="@+id/textView2" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <TextView android:id="@+id/team_score_blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:text="0" android:textColor="#000000" android:textSize="20sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="@+id/textView" app:layout_constraintStart_toStartOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView" /> <Button android:id="@+id/buttonUp_red" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_marginTop="64dp" android:layout_marginStart="16dp" android:background="#EC0519" android:text="Up" android:textColor="#fff" android:textSize="20sp" android:textStyle="bold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/team_score_red" /> <Button android:id="@+id/buttonUp_blue" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginBottom="16dp" android:background="#065FF4" android:textColor="#fff" android:text="Up" android:layout_marginTop="64dp" android:textSize="20sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/team_score_blue" /> <Button android:id="@+id/buttonDown_red" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:background="#EC0519" android:text="Down" android:textColor="#fff" android:textSize="20sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="@+id/buttonUp_red" app:layout_constraintStart_toStartOf="@+id/buttonUp_red" app:layout_constraintTop_toBottomOf="@+id/buttonUp_red" /> <Button android:layout_width="120dp" android:layout_height="wrap_content" android:text="Down" android:id="@+id/buttonDown_blue" android:background="#065FF4" android:textColor="#fff" android:textSize="20sp" android:layout_marginTop="16dp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="@+id/buttonUp_blue" app:layout_constraintStart_toStartOf="@+id/buttonUp_blue" app:layout_constraintTop_toBottomOf="@+id/buttonUp_blue" /> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="64dp" android:gravity="center" android:orientation="horizontal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/buttonDown_red"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Points increase by: " android:textColor="#000000" android:textSize="16sp" android:textStyle="bold" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radio_group" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:textColor="#000000" android:textSize="16sp" android:textStyle="bold" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:textColor="#000000" android:textSize="16sp" android:textStyle="bold" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:textColor="#000000" android:textSize="16sp" android:textStyle="bold" /> </RadioGroup> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
_____________________________________________
APP SCREENSHOT:
___________________________________________________
Feel free to ask any questions in the comments section
Thank You!