• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Does onClick() work in 2.1?

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:
package com.test.HelloWorld;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;


public class mainPage extends Activity implements OnClickListener {
    
    TextView dollars;
    TextView euros;
    TextView debug;
    RadioButton dtoe;
    RadioButton etod;
    Button convert;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        dollars = (TextView)this.findViewById(R.id.dollars);
        euros = (TextView)this.findViewById(R.id.euros);
        debug = (TextView)this.findViewById(R.id.Debug);
        dtoe = (RadioButton)this.findViewById(R.id.dtoe);
        etod = (RadioButton)this.findViewById(R.id.etod);
        
        dtoe.setChecked(true);
        
        debug.setText("Program Started..");
        
        convert = (Button)this.findViewById(R.id.convert);
        convert.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
    
        debug.setText("Click handler routine");
        
        if (dtoe.isChecked())
        {
            convertDollarsToEuros();
        }
        
        if (etod.isChecked())
        {
            convertEurosToDollars();
        }
    }
    
    protected void convertDollarsToEuros()
    {
        double val = Double.parseDouble(dollars.getText().toString());
        euros.setText(Double.toString(val*0.67));
        debug.setText(euros.getText());
    
    }
    
    protected void convertEurosToDollars()
    {
        double val = Double.parseDouble(euros.getText().toString());
        dollars.setText(Double.toString(val/0.67));
    }
    
}
 
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:
final TextView debug = (TextView)findViewById(R.id.Debug);

convert.setOnClickListener(new View.onClickListener() {
      @Override
      public void onClick(View v) {
         debug.setText("Hello!");
      }
   });
 
Upvote 0
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.

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:
 debug.setText("Program Started..");
        
        convert = (Button)this.findViewById(R.id.convert);
        convert.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
            
                debug.setText("Click handler routine");
                
                if (dtoe.isChecked())
                {
                    convertDollarsToEuros();
                }
                
                if (etod.isChecked())
                {
                    convertEurosToDollars();
                }
            }
        });
    }
 
Upvote 0
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones