Skip to content

Instantly share code, notes, and snippets.

@abdullah-alialdin
Last active February 25, 2018 19:13
Show Gist options
  • Save abdullah-alialdin/7882860eb16d437cd884647597673341 to your computer and use it in GitHub Desktop.
Save abdullah-alialdin/7882860eb16d437cd884647597673341 to your computer and use it in GitHub Desktop.
Android Development for Beginners : Court Counter - Setting up the Methods
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.android.courtcounter.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team A"
android:gravity="center_horizontal"
android:padding="8dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:gravity="center_horizontal"
android:padding="8dp"
android:id="@+id/team_a_score"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+3 points"
android:layout_margin="8dp"
android:onClick="threePoints"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+2 points"
android:layout_margin="8dp"
android:onClick="twoPoints"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="free throw"
android:layout_margin="8dp"
android:onClick="freeThrow"/>
</LinearLayout>
package com.example.android.courtcounter;
/**
* IMPORTANT: Make sure you are using the correct package name.
* This example uses the package name:
* package com.example.android.courtcounter
* If you get an error when copying this code into Android studio, update it to match teh package name found
* in the project's AndroidManifest.xml file.
**/
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayForTeamA(8);
}
/**
* Increase the score for Team A by 3 point.
*/
public void threePoints (View view){
displayForTeamA(3);
}
/**
* Increase the score for Team A by 2 point.
*/
public void twoPoints (View view){
displayForTeamA(2);
}
/**
* Increase the score for Team A by 1 point.
*/
public void freeThrow (View view){
displayForTeamA(1);
}
/**
* 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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment