0

I'm trying to create buttons that will execute certain different instructions based on a simple click and a long click, but I'm struck with low understanding on how to put everything together. Performing a defined method for every button is ok, but I think it would be better to use onClickListeners for this, isn't it?

So my code is as follows. As you can see, I'm trying to catch both types of event for each button, but when I press the button 1A I get the toast of the 2A, and when I click the button 2A I get an error and the app crashes. The second thing to fix is to bind together the onClick and the onLongClick.

activity_scout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.android.scout.ScoutActivity">

<LinearLayout
    android:id="@+id/activity_scout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="8dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="8dp"
    android:orientation="vertical" >

    <Button
        android:id="@+id/but1A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1a"
        android:onClick="click1a"
        />

    <Button
        android:id="@+id/but2A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2a"
        android:onClick="click2a" />


   </LinearLayout>
</ScrollView>

ScoutActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import static com.example.android.basketscout.R.id.butPlayer1A;
import static com.example.android.basketscout.R.id.butPlayer2A;
import static com.example.android.basketscout.R.id.butPlayer3A;
import static com.example.android.basketscout.R.id.butPlayer4A;
import static com.example.android.basketscout.R.id.butPlayer5A;
import static com.example.android.basketscout.R.id.textView;

public class ScoutActivity extends AppCompatActivity {

    Button but1A;
    Button but2A;
    Button but3A;
    Button but4A;
    Button but5A;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scout);

        but1A = (Button) findViewById(R.id.but1A);
        but2A = (Button) findViewById(R.id.but1A);

        but1A.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                Toast.makeText(getApplicationContext(), "Button 1A clicked", Toast.LENGTH_SHORT).show();
            }
        });

        but2A.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                Toast.makeText(getApplicationContext(), "Button 2A clicked", Toast.LENGTH_SHORT).show();
            }
        });

        but1A.setOnLongClickListener(new View.OnLongClickListener(){
            public void onLongClick (View view) {
                Toast.makeText(getApplicationContext(),"Button 1A long clicked", Toast.LENGTH_SHORT).show();
            }
        });

    but2A.setOnLongClickListener(new View.OnLongClickListener(){
        public void onLongClick (View view) {
            Toast.makeText(getApplicationContext(),"Button 2A long clicked", Toast.LENGTH_SHORT).show();
        }
    });
    }

 }

[If you see any error like unclosed parentheris or not completely correct variable names, it's because of some edit from the copy/paste I did]

GondraKkal
  • 87
  • 2
  • 16

5 Answers5

1

You are finding the same view twice, you have to change this part of your code:

but1A = (Button) findViewById(R.id.but1A);
but2A = (Button) findViewById(R.id.but1A);

To this:

but1A = (Button) findViewById(R.id.but1A);
but2A = (Button) findViewById(R.id.but2A);

Also, remove the android:onClick attribute from layout, it's redundant and causes conflicts.

  <Button
        android:id="@+id/but1A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1a"/>

    <Button
        android:id="@+id/but2A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2a"/>
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
0

Remove your onClick input in the xml file, If you declared an onClick method from xml you don't need to call setOnClickListener on the Buttons object instead just create that method with the parameters of yourOnClickMethod(View view)

GGWP
  • 1,111
  • 2
  • 12
  • 24
0

There are errors in your code. First is already pointed out by @Luiz which is the binding of view to the button object:

but2A = (Button) findViewById(R.id.but2A);

Also, here's a thread that might help you because I see you have declared onClick attribute in your xml for the Button tags: How exactly does the android:onClick XML attribute differ from setOnClickListener?

Basically there are these two ways of implementing click listeners and if your are using setOnClickListener() then onClick attribute in XML is not required and vice versa.

Pulak
  • 768
  • 7
  • 16
0

problem is you finding the same id twice:

    but1A = (Button) findViewById(R.id.but1A);
    but2A = (Button) findViewById(R.id.but1A);

To

  but1A = (Button) findViewById(R.id.but1A);
    but2A = (Button) findViewById(R.id.but2A);

Then,

For long click listener for views see this how to implement a long click listener on a listview

0

Remove the onClick attribute from Button layout

And correct this code

but1A = (Button)findViewById(R.id.but1A);
but2A = (Button)findViewById(R.id.but1A);

To

but1A = (Button)findViewById(R.id.but1A);
but2A = (Button)findViewById(R.id.but2A);