0

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));
}

}
  • Take a look at https://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene/4153842#4153842 – Gunaseelan Jun 06 '20 at 05:20

3 Answers3

0

You are already using onClick attribute on your buttons in the XML file. No need to set onClickListener on buttons. You can add the code that will be executed on the click of these buttons.

Like when the user presses Beyond_Three_Point_Line button addThreeForTeamA will be called and you can add the required code to addThreeForTeamA function, no need to set onClickListener.

You can improve your code by using on one function like onButtonClickHandler and call it through each button:

<?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="onButtonClickHandler"/>
<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="onButtonClickHandler"/>
<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="onButtonClickHandler"/>
</LinearLayout>

Now in your main activity define onButtonClickHandler like:

 public void onButtonClickHandler (View view){
  switch(view.getId()){
    case R.id.Beyond_Three_Point_Line: // statement
                                      break;
    case R.id.Within_Three_Point_Line: //statement 
                                      break;
    case R.id.Foul_Free_Throw: //statements
                                      break;
    }
  }
Alpha 1
  • 4,118
  • 2
  • 17
  • 23
0
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 implements View.OnClickListener {

  @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(this);
  }

  public void addTwoForTeamA (View view){
    Button button2 = (Button) findViewById(R.id.Within_Three_Point_Line);
    button2.setOnClickListener(this);
  }

  public void addOneForTeamA (View view){
    Button button3 = (Button) findViewById(R.id.Foul_Free_Throw);
    button3.setOnClickListener(this);

  }

@Override
public void onClick(View view){
    if(view.getId() == R.id.Beyond_Three_Point_Line){
    // handle on click of R.id.Beyond_Three_Point_Line
    }else if(view.getId() == R.id.Within_Three_Point_Line){
    // handle on click of R.id.Within_Three_Point_Line
    }else if(view.getId() == R.id.Foul_Free_Throw){

    }
}

/**
 * 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));
  }

}

Is that what you mean? Like handle onClick in one method depending on id?

Goodie123
  • 467
  • 4
  • 16
0

Create a View.OnClickListener object and assign it to buttons or other views using setOnClickListener(listener) and then use it like this:

 View.OnClickListener onClickListener1 = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        case R.id.buttonId1 :
            // do something here
            break;
        case R.id.buttonId2 :
            // do something here
            break;
        case R.id.textViewId1 :
            // do something here
            break;
    }
};
// in onCreate() method define these buttons and assign them
    Button button1= findViewById(R.id.buttonId1);
    button1.setOnClickListener(onClickListener);

    Button button2 = findViewById(R.id.buttonId1);
    button2 .setOnClickListener(onClickListener);

    TextView textView1 = findViewById(R.id.textViewId1);
    textView1 .setOnClickListener(onClickListener);

This way you can use one Listener for every view i.e. on any button or textView or other views.

gagangamer
  • 14
  • 1
  • 6