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]