I am trying to achieve the same result by id for every button, using onClickListener. This is the link to the original code where onClick is used for all buttons without creating id for reference. The reason why I intended to create id for every button unlike in the original code is I felt the original code is kinda simplified but I thought to challenge myself to see if the same result can be achieved by using reference, though I know its complexity. But I am trying to do things differently and not just blindly copy-paste the code :)
Any help is highly appreciated!
p.s. Please consider code related to button code only as I am getting error in that part and I haven't yet gone any beyond in the original code.
[https://gist.github.com/udacityandroid/4edd7beac8465acc07ca][1]
Attached is my code using ids for buttons.`
**activity_main.xml**
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/Team_Name_A"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Team A"
android:gravity="center_horizontal"
/>
<TextView
android:id="@+id/Scored_Points"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:padding="20dp"
android:gravity="center_horizontal"
/>
<Button
android:id="@+id/Beyond_Three_Point_Line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="+3 Points"
android:onClick="addThreeForTeamA"/>
<Button
android:id="@+id/Within_Three_Point_Line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="+2 Points"
android:onClick="addTwoForTeamA"/>
<Button
android:id="@+id/Foul_Free_Throw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Free Throw!"
android:onClick="addOneForTeamA"/>
</Linear
**MainActivity.java**
package com.example.scorebasket;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addThreeForTeamA (View view){
Button button1 = (Button) findViewById(R.id.Beyond_Three_Point_Line);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
public void addTwoForTeamA (View view){
Button button2 = (Button) findViewById(R.id.Within_Three_Point_Line);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
public void addOneForTeamA (View view){
Button button3 = (Button) findViewById(R.id.Foul_Free_Throw);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
/**
* Displays the given score for Team A
*
*/
public void scoreEarnedByTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.Scored_Points);
scoreView.setText(String.valueOf(score));
}
}