Working with anonymous class in Android - java

I'm an android beginner, I have some java knowledge, but i'm getting trouble with some android code.
In java we can use anonymous class to override methods of an interface.
I came across the following situation.
MyButton.setOnClickListener(New Button.OnClickListener{
#override
public void onClick(View view){
//some code
}
});
I was used to using the anonymous class like so,
Interface myInterface = new Interface(){
}
I can't just figure out what only New followed by the interface name means,inside a method.

It is a anonymous class without a variable name
MyButton.setOnClickListener(new Button.OnClickListener{
#override
public void onClick(View view){
//some code
}
});
is the same as
Button.OnClickListener listener = new Button.OnClickListener{
#override
public void onClick(View view){
//some code
}
});
MyButton.setOnClickListener(listener);

Related

Call a method just before OnClick is called

I have been looking for my question on Google and inside the forum but I cannot an answer so far. I am using android studio to code an app in Java
The thing is : inside a class, I have overrided the OnClick() method because I have to implement a lot of views.
Now I want to call a function just before OnClick() is called.
What I mean is, for instance, if the user taps on a button, before OnClick() is called I want one of my methods to be called.
Does anyone know how to do this ?
Thank you in advance
I want to call a function just before OnClick() is called
You can use a logic inside onClick() like,
if(userhaspermission())//your method to check if the user has permission
{
//your onclick operation code
}
Make your checking permission method with return type as boolean
Implement your custom OnClickListener as follows
public abstract class MyOnClickListener implements OnClickListener {
#Override
public void onClick(View v) {
//Do common action
if(condidtionSatisfied){
performClick(v);
}
}
public abstract void performClick(View v);
}
Set onClickListener to any component as follows:
button.setOnClickListener(new MyOnClickListener() {
#Override
public void performClick(View v) {
//Execute post click action
}
});
This will ensure your common code will be called for all the views and it would be much cleaner approach.
Override the onClick() method and write your first logic which you want.
Write your own Listener like Sagar said
This logic is quite similar to Sagar's Answers.
Write abstract class as given below
public abstract class OnClick implements View.OnClickListener {
public void beforeClick(View v) {
}
#Override
public void onClick(View v) {
beforeClick(v);
performClick(v);
afterClick(v);
}
public void performClick(View v) {
}
public void afterClick(View v) {
}
}
NOTE : See in above code it implements View.OnClickListener, so this logic will works for views which extends View super class. If you want above logic for object's which needs Dialog interface onClick then you need to modify it for DialogInterface.OnClickListener.
So you can use above logic as below
//Let say you need it for button with id button
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClick() {
#Override
public void beforeClick(View v) {
Toast.makeText(MainActivity.this, "Before", Toast.LENGTH_SHORT).show();
}
#Override
public void performClick(View v) {
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
}
#Override
public void afterClick(View v) {
Toast.makeText(MainActivity.this, "After", Toast.LENGTH_SHORT).show();
}
});

ZXING Android Embedded Usage of IntentIntegrator

I am new to Android development and still trying to get the grasp of some of the concepts. I find the best way to learn is to jump right into the deep end with a project. With that said, here is my question:
I have integrated ZXing Android Embedded into my application; however, I am having trouble understanding the way in which you use IntentIntegrator. All I am trying to do at the moment is call the QR scanner to the screen when the user taps a button. I have been trying to follow the instructions on their github link [here][1] but have been unsuccessful.
Here is what my function looks like so far:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
IntentIntegrator integrator = new IntentIntegrator(this);
IntentIntegrator.forFragment(this).initiateScan();
}
});
I keep getting an error saying:
Error:(109, 25) error: constructor IntentIntegrator in class
IntentIntegrator cannot be applied to given types; required: Activity
found: Intent reason: actual argument Intent cannot be converted to
Activity by method invocation conversion
Also, as I put my mouse over '(this)' in Android Studio, it says:
anonymous android.view.View.onClickListener
Any help would be greatly appreciated, thanks! If you need any other information, please let me know.
An even easier solution than ChrisStillwell´s would be to make your activity-/fragment-class implement the OnClickListener, this way you do not need the reference variable:
public class SomeFragment extends Fragment implements View.OnClickListener {
// Rest of your code
#Override
public void onClick(View v) {
if (v.getId == button.getId) {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
IntentIntegrator.forFragment(this).initiateScan();
}
}
}
If you are implementing a fragment-class, note that you have to call getActivity() when creating the IntentIntegrator.
You are referencing your OnClickListener and not your Fragment when you say this inside your onClick. You need to reference your Fragment, the easiest way to do that is setup a global variable and call it myFragment or something to your liking, then assign it this. For example:
Fragment myFragment = this;
public void myFunction(){
// Code and stuff //
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
IntentIntegrator integrator = new IntentIntegrator(this);
IntentIntegrator.forFragment(myFragment).initiateScan();
}
});
}
following can work:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new IntentIntegrator(getActivity()).initiateScan();
popupWindow.dismiss();
}
});

Call method from another object-java

I'm doing some Android development and I have an object, which doing a specific task. When that task is done I need to inform my main method (Main Activity), which is constantly running, that the process has been finished and pass some information about it.
This may sound a bit unclear, so I'll give you an example:
Take a look at the setOnClickListener() method in Android:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
//This method is called on click
#Override
public void onClick(View v) {
//The View is passed in an anonymous inner class
}
});
It waits for a button to be clicked and calls the onClick(View v) method. I am seeking to achieve the same code structure. How to do this?
You mentioned "process". If you are truly doing something in a different process, then you need to look at interprocess communications (IPC). Otherwise, you can use an interface:
Create a class called MyListener:
public interface MyListener {
void onComplete();
}
In your class that will notify your activity:
MyListener myListener;
public void setMyListener(MyListener myListener){
this.myListener = myListener;
}
Then, when you are ready to notify your main activity, call this line:
myListener.onComplete();
Last, in your MainActivity implement MyListener:
public class MyListener extends Activity implements MyListener {
///other stuff
#Override
public void onComplete(){
// here you are notified when onComplete it called
}
}
Hope this helps. Cheers.
This is exactly Listener pattern that you use with views in android. What you want to do is declare an interface in your class that's doing the job, and pass an instance of this interface. Raw example:
TaskDoer.java:
public class TaskDoer {
public interface OnTaskDoneListener {
void onDone(Data data);
}
public void doTask(OnTaskDoneListener listener) {
// do task...
listener.onDone(data);
}
}
Activity:
public void doTaskAndGetResult() {
new TaskDoer().doTask(new TaskDoer.OnTaskDoneListener() {
public void onDone(Data data) {
// do something
}
}
}

Why use Anonymous Inner classes, and what are the alternatives?

I've recently got into Android and have been looking at examples about Inner classes but don't really understand what the use of them is. They are used often when making listeners and when making a full class is unnecessary right? Maybe someone can explain it to me in laymans terms, also what would the alternative to using an inner anonymous class in this situation be?
This code:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Thanks
One alternative pattern is to make the container class itself a listener.
public class MyClass implements View.OnClickListener {
#Override
public void onClick(View v) {
// Do something when button is clicked
}
public void initOrSomething() {
button.setOnClickListener(this);
}
}
However you may run into trouble if you have more than one button that needs to behave differently.
Another way is to have different listener classes for each button
public class Button1Listener implements View.OnClickListener {
#Override
public void onClick(View v) {
// Do something when button1 is clicked
}
}
public class Button2Listener implements View.OnClickListener {
#Override
public void onClick(View v) {
// Do something when button2 is clicked
}
}
button1.setOnClickListener(new Button1Listener());
button2.setOnClickListener(new Button2Listener());
Anonymous inner classes are just a more compact representation of the second pattern.
EDIT: Variations of both patterns are possible, where contents of the View object are examined to determine which button was clicked or constructor arguments are passed to the listener class to change listener behavior etc.
They are used often when making listeners and when making a full class is unnecessary right?
Listeners in Android, or other interfaces in other situations. But in essence, that's about it.
what would the alternative to using an inner anonymous class in this situation be?
It would be to create a class which implements this interface and submit it as an argument. For instance:
public class MyListener
implements View.OnClickListener
{
// implement onClick(), etc etc
}
// In code:
button.setOnClickListener(new MyListener(...));
That's quite simple: What you are doing is just creating a class. For the JVM (or dalvik in this case), it doesn't matter if the class is it's own compilation unit (a file), an inner class or an anonymous class(*). So you have three equally valid options:
Option 1 Your example:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Option 2 named inner class:
public MyActivity extends Activity {
static class MyListener implements View.OnClickListener {
#Override
public void onClick(View v) {
// do something
}
}
....
button.setOnClickListener(new MyListener());
}
and Option 3 Different Files:
File MyListener.java
public class MyListener implements View.OnClickListener {
#Override
public void onClick(View v) {
// do something
}
}
File MyActivity.java
import MyListener.java
public MyActivity extends Activity {
....
button.setOnClickListener(new MyListener());
}
Which of these options you use is completely subjective - Depending on your needs and usage, one or the other makes more sense.
However, generally in UI listeners, you don't want to have any logic that is disjunct from the logic of the Activity you are programming. Hence you use the anonymous class, because all the code stays in one place and makes it decently readable.

View.OnClickListener() a function or Interface

Is View.OnClickListener() a function or interface? When we try to set a onclicklistener() method in android, we use new View.OnClickListener() and it bugs me there cuz as far as I know,
we don't need to initialize an object of class containing static method inorder to use those methods. Why we do this?
When we use implements inorder to implement an interface, we don't call the static methods of the interface.
So can some one tell me why do we do:
new View.OnClickListener(), for using onclick() method?
Why do we use () with View.OnClickListener if it is an interface?
Thanks for your reply..
I'm not sure I understand what you are writing about static methods. View.OnClickListener is an interface: http://developer.android.com/reference/android/view/View.OnClickListener.html
To set a click listener on a view, you pass an instance implementing the OnClickListerner interface: http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)
The most common way to do this in android is to define an anonymous inner class (http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html) that implements OnClickListener like
myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle view click here
}
});
The code above both defines an anonymous inner class and creates an instance of it. It is equivalent to first defining a class implementing View.OnClickListener (if defined in the same class)
class MyOnClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
// Handle view click here
}
}
And later using this
MyOnClickListener listener = new MyOnClickListener();
myView.setOnClickListener(listener);
Sample Code,
Internally it works something like this,
public class MyView{
public stinterface MyInterface{
public void myOnClick(View view);
}
}
public class MyButton{
View view;
public void setOnClicker(MyInterface onClicker) {
onClicker.myOnClick(view);
}
}
public class MyExample{
public void method(){
MyButton myButton = new MyButton();
myButton.setOnClicker(new MyInterface() {
#Override
public void myOnClick(View view) {
}
});
}
}

Categories

Resources