Click here to Skip to main content
11,415,074 members (74,718 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Android Eclipse
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.

		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 7-Jan-13 17:18pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can define event for each button.

Another way, you can do like this:

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);
  Permalink  
Comments
TorstenH. at 9-Jan-13 1:23am
   
That's sweet! +5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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
  }
});
  Permalink  
Comments
TorstenH. at 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.
Justin Jones at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,200
1 OriginalGriff 7,512
2 Maciej Los 3,710
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web04 | 2.8.150427.4 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100