Click here to Skip to main content
13,150,385 members (31,935 online)
Rate this:
 
Please Sign up or sign in to vote.
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 16:18pm
Comments
Phat (Phillip) H. VU 7-Jan-13 23:30pm
   
Hi Justin Jones,
Why dont you seperate event for each button ?
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. 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
Phat (Phillip) H. VU 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.
Phat (Phillip) H. VU 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2017
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