1. Check out our companion app, Forums for Android! Download from Google Play

Does onClick() work in 2.1?

Discussion in 'Application Development' started by MadBunny, May 4, 2010.

  1. MadBunny

    MadBunny Member
    Thread Starter
    35

    May 4, 2010
    11
    0
    35
    Computer Engineer
    Sydney,Australia
    Hi All,

    So I'm just working through a tutorial Application and I can't seem to get the onClick routine working for a button. I managed to get it working by other means (setting the button's onclick property to button_event and writing up a button_event method) but not by implementing the onclickListener for the activity. I have attached the code and would be so grateful if someone could point out where I went wrong.

    Thanks

    Code (Text):
    1. package com.test.HelloWorld;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.view.View.OnClickListener;
    7. import android.widget.Button;
    8. import android.widget.RadioButton;
    9. import android.widget.TextView;
    10.  
    11.  
    12. public class mainPage extends Activity implements OnClickListener {
    13.    
    14.     TextView dollars;
    15.     TextView euros;
    16.     TextView debug;
    17.     RadioButton dtoe;
    18.     RadioButton etod;
    19.     Button convert;
    20.    
    21.    
    22.     /** Called when the activity is first created. */
    23.     @Override
    24.     public void onCreate(Bundle savedInstanceState) {
    25.         super.onCreate(savedInstanceState);
    26.         setContentView(R.layout.main);
    27.        
    28.        
    29.         dollars = (TextView)this.findViewById(R.id.dollars);
    30.         euros = (TextView)this.findViewById(R.id.euros);
    31.         debug = (TextView)this.findViewById(R.id.Debug);
    32.         dtoe = (RadioButton)this.findViewById(R.id.dtoe);
    33.         etod = (RadioButton)this.findViewById(R.id.etod);
    34.        
    35.         dtoe.setChecked(true);
    36.        
    37.         debug.setText("Program Started..");
    38.        
    39.         convert = (Button)this.findViewById(R.id.convert);
    40.         convert.setOnClickListener(this);
    41.     }
    42.     @Override
    43.     public void onClick(View v) {
    44.    
    45.         debug.setText("Click handler routine");
    46.        
    47.         if (dtoe.isChecked())
    48.         {
    49.             convertDollarsToEuros();
    50.         }
    51.        
    52.         if (etod.isChecked())
    53.         {
    54.             convertEurosToDollars();
    55.         }
    56.     }
    57.    
    58.     protected void convertDollarsToEuros()
    59.     {
    60.         double val = Double.parseDouble(dollars.getText().toString());
    61.         euros.setText(Double.toString(val*0.67));
    62.         debug.setText(euros.getText());
    63.    
    64.     }
    65.    
    66.     protected void convertEurosToDollars()
    67.     {
    68.         double val = Double.parseDouble(euros.getText().toString());
    69.         dollars.setText(Double.toString(val/0.67));
    70.     }
    71.    
    72. }
     

    Advertisement

  2. MadBunny

    MadBunny Member
    Thread Starter
    35

    May 4, 2010
    11
    0
    35
    Computer Engineer
    Sydney,Australia
  3. Boogs

    Boogs Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    MadBunny,

    You might want to give your Button it's own onClickListener, instead of using your Activity class to handle the click. This might come in handy later if you want to give other views the ability to respond to a click.

    My recommendation:

    Code (Text):
    1.  
    2. final TextView debug = (TextView)findViewById(R.id.Debug);
    3.  
    4. convert.setOnClickListener(new View.onClickListener() {
    5.       @Override
    6.       public void onClick(View v) {
    7.          debug.setText("Hello!");
    8.       }
    9.    });
    10.  
     
  4. MadBunny

    MadBunny Member
    Thread Starter
    35

    May 4, 2010
    11
    0
    35
    Computer Engineer
    Sydney,Australia
    Thanks Boogs, I was beginning to think nobody read these. I tried something similar, only thing I was missing was the word "final" before the debug declaration and still couldn't get it to work. The only method that I've got to work is assigning the button an onclick property and then writing that metthod.
     
  5. Boogs

    Boogs Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    Hmm... I got my recommendation working just fine. So, you should be able to use that code with yours and make it work.

    Otherwise, I'm not sure what's going on.
     
    MadBunny likes this.
  6. MadBunny

    MadBunny Member
    Thread Starter
    35

    May 4, 2010
    11
    0
    35
    Computer Engineer
    Sydney,Australia
    This is my new code. The click handler is still not running. I get no errors and the program runs fine on my phone, apart from the fact that nothing happens when i click the button.

    Code (Text):
    1.  debug.setText("Program Started..");
    2.        
    3.         convert = (Button)this.findViewById(R.id.convert);
    4.         convert.setOnClickListener(new View.OnClickListener()
    5.         {
    6.             @Override
    7.             public void onClick(View v) {
    8.            
    9.                 debug.setText("Click handler routine");
    10.                
    11.                 if (dtoe.isChecked())
    12.                 {
    13.                     convertDollarsToEuros();
    14.                 }
    15.                
    16.                 if (etod.isChecked())
    17.                 {
    18.                     convertEurosToDollars();
    19.                 }
    20.             }
    21.         });
    22.     }
     
  7. Boogs

    Boogs Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    I recommend using an emulator - if you're not already - and see if you can put a breakpoint at the first line of onClick. Run it in debug mode, try to click on the button, and see if the onClick is actually entered. The problem could be that onClick is entered, but the text isn't displaying for some reason.
     
  8. MadBunny

    MadBunny Member
    Thread Starter
    35

    May 4, 2010
    11
    0
    35
    Computer Engineer
    Sydney,Australia
    The onclick isn't being triggered. I put a breakpoint there and it never entered.
     
  9. MadBunny

    MadBunny Member
    Thread Starter
    35

    May 4, 2010
    11
    0
    35
    Computer Engineer
    Sydney,Australia
    Fixed it, damn droid draw.
     
  10. adr

    adr New Member
    5

    Sep 15, 2010
    1
    0
    5
    I am having similar problems. What was the fix?
     

Share This Page

Loading...