View.OnClickListener, method or class? - java

sorry if this question is stupid, but I can't wrap my head around Java syntax..I learnt C/C++
I know View is a class which is good..but I don't understand if View.OnClickListener() is a method.
I doubt it unless it returns an object?
I think View is a class which has a static OnClickListener member object..again that doesn't make sense to me..
Can some explain what is happening with this line of code?
button1 = (Button) findByView(R.id.button1) ;
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
}
}
So what is happening with this code?
Button1 is a reference to the button1 object in the xml file.
button1 object has a member object setOnClickListener which I from its name I assume initializes an event to the button or something. But it receives View.OnClicListener() object.
I am confused by that..onClick receives a View object so onClickListener is not an object returns a View object?? I don't get it at all.
Can someone explain what happens in that line View.onClickListener() is it another way of saying new this?

View.OnClickListener is an interface, you don't call it, but creates a new instance of it (new View.OnClickListener() is a call to the constructor)
The instance you create is of anonymous class that implements View.OnClickListener, in the brackets right under new View.OnClickListener()
Any class that implements View.OnClickListener must implement the methods declared in it (e.g. onClick)
setOnClickListener just saves the reference to the View.OnClickListener instance you supplied, and when someone clicks the button, the onClick method of the listener you set is getting called.

OnClickListener is an interface. An interface provides a set of Methods other classes can implement. http://download.oracle.com/javase/tutorial/java/concepts/interface.html
You could have another class (Like and adapter), that extends OnClickListener, then your Adapter class could add the method "OnClick(View v)", and it would also be able to handle Click events. Or you could use the code you posted, where you just create an anonymous class, that implements OnClickListener.
-Woody

Android code is geared for event based responses. The block of code is as follows:
Find a button that you've added to the active layout, and assign it to a local variable:
button1 = (Button) findByView(R.id.button1);
Set the on click listener for the button. This is a class that will be invoked when the button registers an event. The class is constructed here, it is anonymous as you don't assign it to a variable, but android will keep track of the reference.
Button events are always due to being pressed, so when the button registers that it has been pressed, it will inform the onClickListener class that the even occurred, and pass itself in as the view. The onClickListener is constructed as:
new View.OnClickListener()
{
public void onClick(View v)
{
}
}
That onClick method is used by the listener to handle the event (in this case, a button press). So, you would put the code you would like executed in that method.
To answer your question directly, the onClickListere is an anonymous class that defines the onClick method, which will handle button events.

Related

Why do android run methods like "onItemSelected" even if they are not called in the oncreate Method?

Hello everyone i begin to learn Android development and i do not Unterstand how it works.
I understood that there is not a main. Instead it is the android lifecyle. But now i do not Unterstand why methods like "onItemSelected" or "onTouchEvent" executes by android even if tbis methods do not called.
Tahir!
These methods are listeners, so they are listening to the special action you make. They will be called every time you select the item or touch a UI stuff you are listening to. For example, there is a method called setOnClickListener which listens every time you click on something, e.g. any button you have. The syntaxis will be like:
final Button button = (Button) findViewById(R.id.my_cool_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
}
});
It will be called on touch your button no matter where it is.

Android- Logic behind setOnClickListener

I am new to Java/Android programming and unfortunately I don't fully under stand the logic behind this piece of code:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
I have alredy read a whole lot of tutorials about it but unfortunately in no one so far the code gets explained in detail. Maybe because it's so basic that every decent object oriented programmer would understand the reason for the structure right away.
I am wondering why do I need to use new View.OnClickListener()as a parameter? for the setOnClickListener method. In other words why doesn't something like
button.setOnClickListener(
public void onClick(View v) {
// Perform action on click
});
this work?
Besides that I am not quite sure why the onClick method requires the paramterer of View v.
I would be quite thankful for some help since I am currently rather puzzled.
View.OnClickListener is an interface which you need to implement when you want to handle click events. In your code, you are doing that by doing new View.OnClickListener(). Here you are actually creating an anonymous class that implements View.OnClickListener. And any class that implements View.OnClickListener must also implement all the methods declared in it (e.g. the onClick method). Also, in public void onClick(View v) {..},
the View v denotes the view or the button that was clicked so that you can perform whatever you want with it. For example, get it's id, change it's color etc.
Ok so first of all onClick method wants a parameter as a function. The thing is in Java you can't send a function as a parameter as you can do in other languages (like C++). The Java solution is to use a Functional Interface.
In this case OnClickListener is an interface that have one method you must implement : public void onClick(View v). View v it's the view you just clicked so the method it's called.
button.setOnClickListener(OnClickListener) means the set... function needs an argument that implements the OnClickListener.
So that what this code does :
new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
It is creating an anonymous class that implements the OnClickListener interface required.
Imagine the case where it would be like what you defined - you do not pass in a parameter and simply describe the actions to be taken when that button is pressed. It will work perfectly but for that button only. You will have to redo this method for every other button. What in the case where there are quite a few buttons that do the same thing? You will end up redefining it every time. Wouldn't it be easier to simply just do it once and then pass that in? Also wouldn't it be easier to simply just have one onClick() method which identifies the id of the element being clicked and then performs the logic just for the widget with that id rather than having laborious separate lines of code?
Also don't forget that the parameter being passed belongs to the View class which is the superclass of every other widget. So you can pass the onClickListener() method defined for other widgets into this method if you want (though this case is practically rare).
To answer your question, the Android team could have designed it to be the way you described but chose to do it this way due to best practices and programming paradignms.

Java Using Method from Different Class for Android

I'm very new to Java and OOP. Here is what I've got:
public class AmountChanged implements View.OnFocusChangeListener {
#Override
public void notFocused(View edittext1, boolean focused) {
//Do this awesome stuff
}
How do I instantiate and use this on one of my editText boxes in the mainActivity? I have already declared the editText boxes in the onCreate method.
In your class where you are writing the code for the editext,in that activity's onCreate() method, you need to write
yourEditext.setOnFocusChangeListener(new AmountChanged());
Also give an eye to this please as you can use anonymous classes too.
Let's imagine you created an EditBox:
EditText editText = new EditText(this);
To set focus change listener, you should provide OnFocusChangeListener instance to the setOnFocusChangeListener. Since AmountChanged implements OnFocusChangeListener, you can do the following:
editText.setOnFocusChangeListener(new AmountChanged());
If you are going to use the same listener on many EditText items, you can save this listener as a variable somewhere:
View.OnFocusChangeListener myListener = new AmountChanged();
...
editText.setOnFocusChangeListener(myListener);
In the onCreate method where you have the editText box(es) you want to use this with,
View.OnFocusChangeListener ac = new AmountChanged();
editText.setOnFocusChangeListener(ac);
From the View Android Developer Guide,
Set up listeners: Views allow clients to set listeners that will be notified when something interesting happens to the view. For example, all views will let you set a listener to be notified when the view gains or loses focus. You can register such a listener using setOnFocusChangeListener(View.OnFocusChangeListener). Other view subclasses offer more specialized listeners. For example, a Button exposes a listener to notify clients when the button is clicked.
Listener in android means that it is gonna listen to some event(OnTouchListener, OnClickListener, OnFocusChangedListener etc.). As you see OnFocusChangedListener interface is announced inside View class, in scope of Android it usually means that any child of View can produce this event, so you need to "listen" to those events.
In scope of EdiText what you have to do is something like this:
editText.setOnFocusChangedListener(new AmmountChanged());
EdiText is a child of View. So we are start "listening" to all OnFocusChanged events that will happen inside editText by registering our instance implementation OnFocusChangeListener.

Java, Android passing params to method

I'm working through the android developer tutorial and we're now creating a method to match to the method name we gave to android:onClick="sendMessage".
Here's the method:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
The text says this about the method:
In order for the system to match this method to the method name given to android:onClick, the signature must be exactly as shown. Specifically, the method must:
Be public
Have a void return value
Have a View as the only parameter (this will be the View that was clicked)
I understand why it must be public and why the return value is void, but why does the method take (View view) instead of just (view)? I'm coming from a Ruby background so the syntax is confusing me. Why do we pass parameters like this?
why does the method take (View view) instead of just (view)?
View means it is a class and view is just a variable adding those 2 is making the view variable an object of View class that can call all of its method.
This is due to the fact that onClick() method in the OnClickListener Interface which requires a parameter of type View. When you remove the parameter, Android will still attempt to call method sendMessage(View view) but that method does not exist any more, therefore you get error and app will force close.
Parameter view is the actual View (Button in your case) that was clicked. With this, you can assign multiple buttons to invoke the same method and inside the method check which button was clicked.
For more information, see this LINK

why Interface methods will take object as parameter

let me take a interface in Android-
OnClickListener and it's method is setonClickListener(), it takes object of
View.OnClickListener or it takes as this
why it is taking parameters--that object or this
The listener object which implements the event call back method.
You can register a listener on the view using setOnClickListener(). This listener will be called by android when the view is clicked. The listener can be any object implementing the OnClickListener interface.
And the method called by android on the listener will be onClick(). And android will pass the view which has been clicked to the method, so that the listener knows which view has been clicked. This allows using the same listener for several views.

Categories