Interface Vs anonymous class in android - android

I am little bit confused regarding backend working of anonymous class, like if we have a button and we are setting onclickListener
Button B = (Button)findViewById(R.id.myButton);
B.setOnClickListener(new onClickListener(){
public void onClick(View V){
Log.v("","Hello world");
}
));
What is here actually happening in backend ?Does this will implement interface of View.OnClickListener or something else???

Please look over this
How can an anonymous class use "extends" or "implements"?
http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm

Yes it is an instance of new unnamed class that implements the OnClickListener interface.

Anonymous classes must always extend a class or implement an interface.
b.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
Log.v("", "Hello world");
}
});
In this case, you are creating a new anonymous (unnamed) class that implements the View.OnClickListener interface. This works because the setOnClickListener method takes an argument of type View.OnClickListener.

Related

Class 'MainActivity' must either be declared abstract or implement abstract method 'onClick(View)' in 'OnClickListener

I have been coding for a very long time and I've only started using Android Stud last month, I'm having a problem with implementing an OnClickListener.
here's the code that is giving me the error.
I hope my question is clear, else I'll happy to give my code snippet.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button scanBnt;
private TextView formatText, contentText;
....
}
You have implemented View.OnClickListener interface in your activity. This interface has one abstract method abstract void onClick(View v) which you need to override in your activity.
Add the following code to your activity,
#Override
public void onClick(View v) {
// Your code
}
Check out
Button button = (Button) findViewById(R.id.mybutton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(this, "Button Clicked", Toast.LENGTH_LONG).show();
}
});
for explanation visit here
Highlight on the error (OnClickListener) then press Ctrl + Space then click implement method. else you can add the method manually like what #gprathour written.

I don't fully understand onClickListeners

So when we set an onClickListener to, say, a Button, it looks something like this.
private Button myButton = (Button) findViewById(R.id.my_button);
myButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
//do this
}
});
So we're creating a nameless anonymous class when we state new View.OnClickListener... and implementing the OnClickListener interface / and overriding it's onClick method. What I don't understand, is if we have no reference to this anon class, because it's nameless, how does the onClick() method get called? I've only ever implemented an anonymous class to override certain methods in said class, like this:
public class Foo{
public void bar(){
//do something
}
}
Foo foo = new Foo(){
#Override
public void bar(){
//do something else
}
}
This makes perfect sense to me because now, anytime I use the "foo" reference to call the bar() method, that reference will use the overridden version of bar. In the case of the Button, there's no reference to onClick(). I'm beyond confused about this.
If it helps your understanding, you could rewrite to this:
View.OnClickListener listener = new View.OnClickListener(){
#Override
public void onClick(View view){
//do this
}
};
myButton.setOnClickListener(listener);
The button holds the reference after listener goes out of scope, and can call the onClick callback on the held listener object.
What I don't understand, is if we have no reference to this anon class, because it's nameless, how does the onClick() method get called?
myButton is holding onto the instance of the anonymous inner class that you created. myButton, therefore, can call onClick() on it.
The onclick event is called in the button object and this object delegates to your anonymous class onclick with the reference you set.

syntax issue regarding to setOnClickListener?

May anyone explain to me this bit of code?
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
startActivity(intent);
}
});
In this case, MainActivity and DetailActivity are two classes I have created.
I am kind of confused,
In this case, what are the roles of setOnClickListener and View.OnClickListener?
Within the constructor method of the Intent class, for the context, why can't we just put ".this" but we have to put in MainActivity class in the front? Under what kind of situation can we use ".this"?
setOnClickListener is a method. View.OnClickListener is an interface. The setOnClickListener method takes a View.OnClickListener as an argument. The syntax new Foo() { ... } defines an anonymous inner class instance that implements the interface Foo.
this refers to the instance, which in case of an inner class is the inner class View.OnClickListener instance. You can refer to the outer class instance (an activity which is-a Context) by scoping the reference with the outer class name.
You have to put MainActivity.this because the setOnClickListener is a interface in a View class and it contains a method onClick(View v); , if you will use this instead of MainActivity.this, it will refer to the Context of OnClickListener, when we specify MainActiviy.this it refers to the Context of MainActivity class. This concept is called Shadowing in java.

Android setOnClickListener method - How does it work?

I have trouble understanding this code. I get that findViewById will get the button widget and then it'll cast it. Then, it's going to use the button to call the setOnClickListener method. However, I don't know what is that argument being passed into the setOnClickListener and I have never seen code like that before. How is it that it creates a new object but is able to create a method of its own within another method's argument? Would be great if someone could explain that. Also, what type of object is the setOnClickListener method taking in?
btn = (Button)findViewById(R.id.firstButton);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
tv.setText(months[rand.nextInt(12)]);
tv.setTextColor(Color.rgb(rand.nextInt(255)+1, rand.nextInt(255)+1, rand.nextInt(255)+1));
}
});
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.
This is the best way to implement Onclicklistener for many buttons in a row
implement View.onclicklistener.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
This is a button in the MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_submit = (Button) findViewById(R.id.submit);
bt_submit.setOnClickListener(this);
}
This is an override method
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.submit:
//action
break;
case R.id.secondbutton:
//action
break;
}
}
That what manual says about setOnClickListener method is:
public void setOnClickListener (View.OnClickListener l)
Added in API level 1 Register a callback to be invoked when this view
is clicked. If this view is not clickable, it becomes clickable.
Parameters
l View.OnClickListener: The callback that will run
And normally you have to use it like this
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
...
}
Take a look at this lesson as well Building a Simple Calculator using Android Studio.
its an implementation of anonymouse class object creation to give ease of writing less code and to save time
It works by same principle of anonymous inner class where we can instantiate an interface without actually defining a class :
Ref: https://www.geeksforgeeks.org/anonymous-inner-class-java/

How do you Override onClick in an Android ImageButton derived class

I've seen this asked a thousand times in a thousand different ways, but still can't get it to work...
I created a class which I've derived from ImageButton. I want to define my "on-click" behavior in the class.
I know I can do something inside my Activity's onCreate like:
myButton b;
b = (myButton)findViewById(R.drawable.mybutton);
b.setOnClickListener(new View.OnClickListener() {
...etc...
but I want to define the code where it should be, in the derived class.
I first thought I could define it as:
#Override public void onClick(View v) {
...but I get an error saying that I can't use "#Override" here because "onClick" isn't in the superclass. (When trying to remove "#Override", it just builds and runs, but never gets called). I've also tried:
#Override public void onClickListener(View v) {
...and several variants of "implements onClickListener" and "implements OnClickListener" to no avail.
This should be fairly simple - any ideas??
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
derivedClassFunction(v);
}
});
public void derivedClassFunction(View v) {
/* code...*/
}
Another way:
public class DerivedClass extends ImageButton implements View.OnClickListener {
/*code...*/
b.setOnClickListener(this);
/*code...*/
#Override
public void onClick(View v) {
/*code...*/
}
}
This is because there actually is no method onClick() in views. The work is done in the onUpKey() in the View class.
However, if you want to listen to clicking events in the subclass, this could be done very easily. You can either create an inner class which implements View.OnClickLister and use it to listen to events or even simpler, implement the interface in your class and set it as a listener during construction. The latter will look like this:
class YourClass extends ImageButton implements View.OnClickListener {
public YourClass() {
setOnClickListener(this);
}
#Override
public void onClick(View v) {
//Your code
}
}
LAS_VEGAS has already posted how the first variant with the inner class may look like.

Categories

Resources