Android setOnClickListener method - How does it work?
It works like this. View.OnClickListenere is defined -
public interface OnClickListener {
void onClick(View v);
}
As far as we know you cannot instantiate an object OnClickListener
, as it doesn't have a method implemented. So there are two ways you can go by - you can implement this interface which will override onClick
method like this:
public class MyListener implements View.OnClickListener {
@Override
public void onClick (View v) {
// your code here;
}
}
But it's tedious to do it each time as you want to set a click listener. So in order to avoid this you can provide the implementation for the method on spot, just like in an example you gave.
setOnClickListener
takes View.OnClickListener
as its parameter.
How does Android's setOnCLickListener() works?
Each View contains ListenerInfo
static class which holds callbacks, OnClickListener
too actually.
How it works?
System always holds all views on screen.
When user tap on screen we have a recursive foreach cycle :
switch(event) {
...
case ON_CLICK:
process(ViewRoot);
}
void process(View view) {
for(View view : view.getChilds()) {
if(view instanceOf ViewGroup && ((ViewGroup)view).getChildCount() > 0) {
process(view);
}
if(view.getListenerInfo().mOnClickListener != null)
view.getListenerInfo().mOnClickListener.onClick(view)
}
}
When you call setOnClickListener
you actually say "hey Android! here it callback. And when user make click, please use it."
View.class also have getListenerInfo
method which returns ListenerInfo
object.
System use this method to dispatch events.
So no Observer
pattern here. It just simple check of existing callback.
Android - setOnClickListener vs OnClickListener vs View.OnClickListener
The logic is simple. setOnClickListener
belongs to step 2.
- You create the button
- You create an instance of
OnClickListener
* like it's done in that example and override theonClick
-method. - You assign that
OnClickListener
to that button usingbtn.setOnClickListener(myOnClickListener);
in your fragments/activitiesonCreate
-method. - When the user clicks the button, the
onClick
function of the assignedOnClickListener
is called.
*If you import android.view.View;
you use View.OnClickListener
. If you import android.view.View.*;
or import android.view.View.OnClickListener;
you use OnClickListener
as far as I get it.
Another way is to let you activity/fragment inherit from OnClickListener
. This way you assign your fragment/activity as the listener for your button and implement onClick
as a member-function.
Android- Logic behind setOnClickListener
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.
setOnClickListener method can't get EditText value
Remove this
final String question = question_EditText.getText().toString();
it only get value 1 time when initial activity.
Move String question = question_EditText.getText().toString();
into onClick method.
Try this to get current string in edittext when you click button.
add_question.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String question = question_EditText.getText().toString();
Log.d("finds","question:"+question);
if (question.length()==0)
{
new AlertDialog.Builder(AddQuestionActivity.this)
.setTitle(R.string.add_error)
.setMessage(R.string.add_blank)
.setPositiveButton(R.string.ok, null)
.setNeutralButton(R.string.cancel, null)
.show();
}
}
});
Related Topics
Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget
Android - How to Pass Hashmap<String,String> Between Activities
Parse JSON Array Without Key in Android
Android Gradle Apache Httpclient Does Not Exist
How to Re-Sign an .Apk with a Different Certificate Than What It Came With
Android Volley - How to Isolate Requests in Another Class
Rotating Image on a Canvas in Android
How to Detect Orientation of Android Device
Simple Example for Intent and Bundle
Error:Execution Failed For Task ':App:Transformclasseswithjarmergingfordebug'
How to Use Setarguments() and Getarguments() Methods in Fragments
Facebook - "Cannot Query Users by Their Username" Solution
Launch Android Application Without Main Activity and Start Service on Launching Application
Proguard Hell - Can't Find Referenced Class
In Android 7 (API Level 24) My App Is Not Allowed to Mute Phone (Set Ringer Mode to Silent)