Tutorial #19 - Events Handling -

0 Yaron X · April 8, 2015
Hi everyone

The video "Android App Development for Beginners - 19 - Event Listener and Callback Method" shows how to add a listener to a button click event as follows:



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test005_main);

        Button btnButton = (Button)findViewById(R.id.btnButton);
        btnButton.setOnClickListener( new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView txt = (TextView)findViewById(R.id.txtTheText);
                txt.setText("Clicked!");
            }
        });
    }




The listener code is inside the onCreate() method. I am used from C++ and VB to create a function separately. So I am trying to do something like this:


    public void onButtonClick(View v) {
        TextView txt = (TextView)findViewById(R.id.txtTheText);
        txt.setText("Hello from onButtonClick");

    }


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test005_main);

        Button btnButton = (Button)findViewById(R.id.btnButton);
        btnButton.setOnClickListener( onButtonClick) ;
    }



But this causes a compilation error:
Error:(26, 39) error: cannot find symbol variable onButtonClick

Why is that? And is there a way I can write the listener code outside the onCreate and refer the setOnClickListener to that function?

Thank you.

Post a Reply

Replies

Oldest  Newest  Rating
+1 steven kitchener · April 8, 2015
Hey Swedish Chef,

The reason you are getting an error is because you missed the trailing parenthesis after onButtonClick, here:
btnButton.setOnClickListener(onButtonClick);

which means that Java is trying to find a variable called onButtonClick, which evidently it cant find because it is supposed to be a method call. 

However, saying all that I dont think your code as it is (even with the corrected parenthesis) will do what you expect it to do. 
Try something like this:

In your on create method use

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test005_main);

defUI();
}


and then create the defUI method:
private void defUI(){
Button btnButton = (Button)findViewById(R.id.btnButton);
btnButton.setOnClickListener( new Button.OnClickListener() {
@Override
public void onClick(View v) {
TextView txt = (TextView)findViewById(R.id.txtTheText);
txt.setText("Clicked!");
}
});
}


Unfortunately i have limited experience with this type of code for Android as i almost exclusively use Libgdx to create my Android Applications. However this code should achieve your desired result.

Hope this helps
0 Yaron X · April 9, 2015
Thank you Steven. 
Adding parenthesis does not help. That causes another compilation error.

In C/C++ (and in C# as well I think), you can pass pointers to functions as parameters to other functions. So one could simply pass the name of the listening function as a parameter to setOnClickListener. I guess I cant't do it in Java.

Thank you for the defUI suggestion as well.
  • 1

Java / Android Development

107,254 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator