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

Apps Method initialization question...I'm new please help

m3atwad

Lurker
I think this is more of a fundamental java concept I am trying to grasp. Anyways, could someone explain to me how the

Code:
    private OnClickListener btnListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            Toast.makeText(getBaseContext(),
                    "Hello, Android Development World",
                    Toast.LENGTH_LONG).show();
        }
    };
part of
Code:
package com.learntodev.HelloDroid;

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


public class HelloDroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn1 = (Button)findViewById(R.id.btnClickMe);
        btn1.setOnClickListener(btnListener);
    }
    
    private OnClickListener btnListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            Toast.makeText(getBaseContext(),
                    "Hello, Android Development World",
                    Toast.LENGTH_LONG).show();
        }
    };
    
}
works? From what i've read it looks like it is a method initializer? I don't really understand whats going on though. Why doesn't it look like anything ever calls the function onClick() in there?

Thanks for the help!
 
It's not a method, it's an Interface. An interface is a class with methods but the methods is not filled out. This way the developer of the interface can guarantee that the methods he put in must be used, but the developer using the interface can choose how the methods are used.

Like a car. A car has to brake and accelerate and so on, but the manufacturer can choose how it is done.

The reason why you don't see that the buttons onClick() method is fired is because this happens behind the scenes. When you push the button with your finger, android behind the scenes triggers the onClick() method.
 
Ok things make a little more sense now i seem to understand at least basic interfaces after some reading.!

A second question though, why isn't the keyword "implement" used since you are implementing an interface? Also, how does the definition:

Code:
private OnClickListener btnListener = new OnClickListener()

work exactly. So you are defining a class by implementing the interface onclick listener? So, how does btnListener = new OnClickListener() work because isn't that defining btnListener as an object of the class OnClickListener? Sorry if these are all dumb questions, but I'm a bit rusty on my OOP and new to java.

Thanks again!
 
You seems aware of existing documentation then to help you I may indicate you to search on anonymous class in java

like :
Class myClass {
...
public DumbClass mInstance = new DumbClass() {

@Override
public void myMethod() { /* do something * / }
...
}

mInstance is a intance of a derived class of DumbClass or a class that implement an DumClass Interface ... same writing style no need to add "implement"

in java you would see a anonymous class that may be called $1 that implement or derive DumClass
in my example the real name of this anonymous class should be like myClass.$1 ... the anonymous class is define as an inner class...

...
Hope that help

have fun
 
Hmmm...Im sure this will be helpful after some more reading. Again, I appreciate the replies this is a great resource. Hopefully I can eventually contribute ;).

Appreciate it guys!
 
Back
Top Bottom