0

Hey guys i'm new in Android's dev and for a project, I have to implement a listener for a button. But unfortunately, he can't detect the button I think. Here is the java code :

public class Touch extends AppCompatActivity implements OnTouchListener,OnClickListener {


private button boutonCompteur = null;

@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.id.Compteur);
    boutonCompteur = (button) findViewById(R.id.Compteur);
    boutonCompteur.setOnTouchListener(this);
    boutonCompteur.setOnClickListener(this);

}

@Override
public boolean onTouch(View v, MotionEvent event)
{
    return true;
} }

and here is the XML :

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="appuyez ici pour compter votre nombre de touch"
    android:id="@+id/Compteur"
    android:height="130dp"
    android:textColor="#1818e3"
    android:textColorHighlight="#cd5555"
    android:textSize="22dp"
    android:textStyle="italic"
    android:layout_marginTop="150dp"
    android:layout_alignParentStart="true" />

Thanks for your answers :)

3

2 Answers 2

2

I am going to use some visual help to answer your question and identify why is not working... take a look at the image below and note:

  1. You need a layout for your activity and this is not the same as the button
  2. You need to find the button in the layout, that is odne by the id you used in the xml file, and is not the same as the Layout!!
  3. you need to set the rigth listener to get the onclick, android has for that the OnClickListener interface
  4. the word button between parenthesis is a casting, (android will try to convert something to a class, therefore the class is the class button, and not the name of your variable)
  5. all the code that you write inside the onClick is what will be executed once the button is pressed.

enter image description here


Conclusion

Take the image as a reference and implement it in your code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, it was really helpful and i learnt things thanks to you :)
0

Remove onTouch , use onClick only.

Remove this line

boutonCompteur.setOnTouchListener(this);

and change public boolean onTouch(View v, MotionEvent event) to

 public void onClick(View v) {
           // Do whatever you want
        }

And also change private button boutonCompteur = null; to private Button boutonCompteur; . (I don't think you need to add =null when doing button declaration).

1 Comment

thanks for your answer. I haven't read the whole android courses so i didn't know that i just needed onClick. Anyway, thanks again for a so fast answer :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.