Hi im a begginer on Android development. I knew basic core java. So i have a question on this particular code:
button.setOnClickListener(new View.OnClickListener()
{
#Override public void onClick(View v)
{
// do something when the button is clicked
}
});
I know that this is anonymous class and OnClickListener is an interface. But what i dont understand is the onClick(View v) method, v is the button that was clicked but under the hood how was this method AUTOMATICALLY executed? I mean isnt that to be able to call a method you must first create an object then a method beside it? I just need to understand this concept, thank you.
In simple words when you create a Button object it has some listener objects:
Example:
class Button extends View{
private OnClickListener clickListener;
public void setOnClickListener(OnClickListener clickListener){
this.clickListener = clickListener;
}
}
when you call this:
button.setOnClickListener();
basically you assign the value to clickListener in Button class and then each time you click the button it triggers
clickListener.onClick(this)
and perform your defined stuff.
Your listener is provided to the Button object, and by clicking the button, the Android framework will try to invoke the OnClickListener (if any) by calling the onClick method you provide.
So it is not really automatically. Your action triggers the click, and Android framework calls your onClick.
Related
btn_Login.setOnClickListener(this);
In android studio.
I've seen this in countless places. What does this mean? I know how this operates but what is the listener called then?
This example is from: Link
Suppose that you have 16 Buttons and every button has setOnclicklistener this means that you are creating many repetitions of similar code to this in your class. And that makes your code ugly, also this is not the efficient way to write your code. So to make your code efficient you have to implement OnClickListener() on your activity and then for each button use buttonX.setOnClickListener(this). Now use the override onClick method. In this method, you can use either the switch case block or if-else to identify which button is pressed. So in the onClick method you just have to give ids of the button.
Implement OnClickListener in Activity
public class MyActivity extends Activity implements View.OnClickListener {
}
For each button use this:
buttonX.setOnClickListener(this);
After this implement override the onClick method
#Override
public void onClick(View view) {
switch(view.getId){
case R.id.buttonX:
// Do something
break;
}
in that example its defined like this
private Button btn_Login;
Button is a class
onClickListener is a listener, to set the listener he is using setOnClickListener method.
From the next time if u need to those kind of clarifications don't post in a separate thread, add a comment in that question itself.
Thank You #august alsina
It is a listener that helps to specify the events to occur on click of a widget.
When your class implement View.OnClickListener, you can defined your click of each button in method public void onClick(View v). Keyword this refer to the method onclick. It is good to use this way when there are a lot of button in your class file. You can define following code On Create method:
button1.setOnClickListener(this);
button2.setOnClickListener(this);
and define its definition oncreate method. For example:
public void onClick(View v){
int id= v.getId();
switch (id){
case R.id.btn1: {
//do sth
//break;
}
case R.id.btn2: {
//do sth
//break;
}
...
}
}
In layman's terms
By writing btn_Login.setOnClickListener(this);
whenever btn_login will be pressed program will go to onclick method public void onClick(View v)
and then you can write in the method what you want to do when button is pressed
Firstly apologies for the long title, I wasn't to sure on how to word it.
I am creating a onclicklistener for a button and am currently trying to improve my apps performance.
Right now I am using this.
final Button ZeroCounter = (Button)view.findViewById(R.id.btn_DigitalReadout_ZeroCounter);
ZeroCounter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ZeroCounters();
}
});
Is there any reason why i would want to first declare the button such as private Button ZeroCounter at the top of my class when all I need from it is the onclicklistener?
Thank you.
Is there any reason why i would want to first declare the button such as private Button ZeroCounter at the top of my class when all i need from it is the onclicklistener?
NO there is no reason to declare private Button as global if you need it in 1 method. It's better to declare it in onCreate() or where-ever you need it to use.
But Some time you need to that Button in other methods too So, there you need it to be declared as global.
You can set the on click function through the xml file
(without declaring on the button at all)
on your button xml :
android:onClick="myClick"
and implement the onClick function on your class:
public void myClick(View v) {
// your code ...
}
I am learning JAVA for Android. I have read that Interfaces should be implemented. But i have confusion here:
final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (checkbox.isChecked()) {
checkbox.setText("I'm checked");
} else {
checkbox.setText("I'm not checked");
}
}
});
we are passing and implementing OnClickListener interface directly here. What is exact reason for this? Please explain this concept in details.
It's because you want to implement it individually for each individual clickable widget. OnClickListener is an inner interface in the View class and logically it would not make sense to make the activity implement it as then any click on the screen even outside of this would trigger unexpected action.
Interfaces alone do not have an implementation. But here what you're actually doing is creating a new anonymous class (a class without a name) that implements the OnClickListener interface and defining the implementation of it all in one place.
As for why you would do this- for very small simple implementations it can be clean, and it prevents you from having dozens of little classes that implement 1 or 2 functions. If the implementation is long it can become difficult to read though. But its never wrong to do it the long way and use a regular class.
Normally we implement OnClickListener like below,
public class MainActivity extends Activity implement OnClickListerner
{
....
view.setOnClickListener(this); // When we are implemeting OnClickListener
#Override
public void onClick(View v)
{
....
}
}
When we implement OnClickListener in that case we set it using setOnClickListener(this), here this refers to OnClickListener listener.
However we can do same thing by declaring another way known as anonymous block as below,
CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (checkbox.isChecked()) {
checkbox.setText("I'm checked");
} else {
checkbox.setText("I'm not checked");
}
}
});
Say I have two activities: "A" and "B".
Activity A has a button in it's layout. and I want to set it's click listener implemention on Activity B.
So let's say here's Activity A:
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(B.this);
and in Activity B, I'm trying to implement the function:
public void OnClick(View v)
{
//DO SOMETHING
}
I'm getting the following errors:
The method setOnClickListener(View.OnClickListener) in the type View
is not applicable for the arguments (A)
No enclosing instance of the type A is accessible in scope
What am I doing wrong here?
The handling of the GUI components must be accompanied within the same UI thread that instantiated that.
So your desired view is not correct also make sure the you can have the click and other listener been worked only if the view is set with that components and is currently visible ( In foreground) for the user to have interaction.
If you really want that then You can override the default implementation of the click listener within the different activities via following:
1)Static Reference: make the button as public static in activity A and use it in Activity B by Class A's name.
2)Interface:implements OnClickListener on the activity A , but will not be accessible in B
3)Custom MyClickListener for all activites.
public class MyClickListener implements OnClickListener {
#Override
public void onClick(View v) {
mContext = v.getContext();
switch (v.getId()) {
case R.id.button:
// Your click even code for all activities
break;
default:
break; }}
}
Use it the class A and B both as shown below:
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new MyClickListener());
You must pass an instance of an OnClickListener to button.setOnClickListener(..). Class A isn't implementing OnClickListener, so you must implement it in order for it to be an instance of an OnClickListener.
class A extends Activity implements OnClickListener {
// instance variable, constructors, etc
#Override
public void onClick(View v) { // note onClick begins with lowercase
// DO SOMETHING
}
}
I have an android program where I have multiple buttons using the same OnClickListener, and I want to be able to reference the button's dynamically assigned text from inside the listener. Is there some way to reference the button that was pushed to get its text? I don't want to have to make multiple button-specific listeners that do the same thing.
In your onClick(View v) you can cast it to a button:
#Override
public void onClick(View v) {
Button clickedButton = (Button)v;
// do stuff with it here.
}
use the View which comes as the argument to the onClick(View v)
this can be casted to a button & worked with.
The argument to onClick is the View that originated the click, which will be the button to which you attached the listener. Cast it to Button to get the button object.
Yes, there should be a way.
public abstract void onClick (View v)
You'll notice that the View that was clicked is passed into the onClick() method. So if you have a reference to the View (Button) available (for example, as an instance variable in the Activity) then you can do this:
public abstract void onClick (View v) {
if (v == firstButton) {
//Do some stuff
}
else if (v == secondButton) {
//Do some other stuff
}
}