Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi there!
Im net to android programming so sorry if this is noobish, but cant find a good example anywhere. I have 2 image button on the same screen using a on click listener. I keep getting bracket errors and Syntax error on tokens, AnnotationName expected instead errors.

Java
    startButton1.setOnClickListener(new OnClickListener() {
        startButton2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {



            if(currentRun == 0){

                startButton1.setText(hitButtonText);
                startButton1Parent.setBackgroundResource(R.drawable.pump_red);
                startButton2.setText(hitButtonText);
                startButton2Parent.setBackgroundResource(R.drawable.pump_red);
                new TimerClass().execute();
            }else{
                if(allowHit){
                    isHit = true;
                }
            }
        }
    });
}


any ideas?

thank you
Posted
Comments
[no name] 7-Jan-13 23:30pm    
Hi Justin Jones,
Why dont you seperate event for each button ?

You can define event for each button.

Another way, you can do like this:

Java
private OnClickListener mListener = new OnClickListener() {
	@Override
	public void onClick(View v) {
		Button btn = (Button)v;
                // if btn is startButton1
		if (btn.getId() == R.id.startButton1) {
                // do something
                } else (btn.getId() == R.id.startButton2) {
                // do something
                }
	}
};

startButton1.setOnClickListener(mListener);
startButton2.setOnClickListener(mListener);
 
Share this answer
 
Comments
TorstenH. 9-Jan-13 1:23am    
That's sweet! +5.
Java
new OnClickListener() {
  startButton2.setOnClickListener(new OnClickListener() {
    // some code
 }
}


THAT will never work.

When you declare such a OnClickListener, you declare an anonymous class. That one is defined. If there is no directly accessable method onClick(View v), the action will not happen.

Try this:
Java
startButton1.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    // fancy code for button1
  }
});
startButton2.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    // fancy code for button2
  }
});
 
Share this answer
 
Comments
[no name] 8-Jan-13 5:04am    
Exactly.
Or, declare and define an OnClickListener instance.
Then pass it as argument in setOnClickListener method of button as my solution.

Cheer.
TorstenH. 8-Jan-13 6:33am    
... working with an instance would need identification of the button as both buttons would probably get the same instance from the onClickListener. I do so when using a lot of controls.
[no name] 8-Jan-13 22:20pm    
Yes, agree with you.
Justin Jones 8-Jan-13 22:27pm    
Thank You Torsten that helped me understand a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900