use OnClick from different class android - java

this is probably simple but i am still a beginner
i have a button in class MainActivity and i want to use the onclick method in another class(another activity) i am aware that it could be easily achieved by making it public static and accessing it as instance but in my case i can not make it public static for some good reason
button xml:
<Button
android:id="#+id/googledrivemain"
android:layout_width="78dp"
android:layout_height="77dp"
android:layout_marginTop="149dp"
android:background="#drawable/google"
android:onClick="onClickOpenFile"
android:paddingTop="6dp"
android:textColor="#fff"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView3"
app:layout_constraintHorizontal_bias="0.51" />
MainActivity button:
public void onClickCreateFile(View view) {
fileOperation = true;
// create new contents resource
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(driveContentsCallback);
}
second class button:
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity main = new MainActivity();
use onclick from mainactivity...??
}
});

Do it like this
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity) getActivity()).onClickCreateFile(v);
}
});
i think it will work.

Simply wrap the code you invoke from the MainActivity.onClickCreateFile() into another class and call it from wherever you want
And never, NEVER, do something like new MainActivity(). Activities are managed by the system

You can create a handler class, something like this:
public static class ClickHandler {
private ClickHandler mInstance;
public static ClickHandler getInstance(){
if(mInstance == null){
mInstance = new ClickHandler();
}
return mInstance;
}
public void onClickCreateFile(View view) {
fileOperation = true;
// create new contents resource
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(driveContentsCallback);
}
}
Then wherever you need it:
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ClickHandler.getInstance().onClickCreateFile(view);
}
});

The reason you can't make a click method static is that 1) The android:onClick XML action requires public void and 2) The view that you are clicking belong to the instance of the activity, not the class.
You could make a static method like this.
public static void createDriveContents(GoogleApiClient apiClient, ResultCallback callback) {
Drive.DriveApi.newDriveContents(apiClient)
.setResultCallback(callback);
}
But, then you still need a reference to the GoogleApiClient object.
Since no client is bound to any one Activity, you could move it into a singleton in order to access one client instance from everywhere.
For example,
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.createDriveContents(
GoogleClientWrapper.getInstance().getClient(), // A GoogleApiClient instance
new ResultCallback() {
// TODO: Implement
});
}
});
If you have access to the GoogleClient in the second class, just pass in the API client object directly.
Or, abstracting up one level, you can instead provide a Context, and re-build the client.
public static void createDriveContents(Context context, ResultCallback callback) {
GoogleApiClient client = new GoogleApiClient.Builder(context);
// TODO: add scopes to client
Drive.DriveApi.newDriveContents(apiClient)
.setResultCallback(callback);
}
With this method, we assume the second class is another activity without it's own GoogleClient
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.createDriveContents(
SecondClass.this, // A Context instance
new ResultCallback() {
// TODO: Implement
});
}
});

Related

method call from adapter class to activity

Adapter:
check_list_item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
JPrequirements.prepareSelection(v, getAdapterPosition());
}
});
JPrequirements is the activity. and prepareSelection is non-static method inside activity. I cannot access it from adapter.
ERROR:
non static method cannot be referenced from a static context
Which is right. that's why I tried with:
JPrequirements().prepareSelection(v, getAdapterPosition()); // Creating an instance...
But, the problem is I lost all activity component here. eg. layout components and other supporting variables. I don't want that. What is the best way to deal with this? How can I get updated value from adapter to activity? So, I can display it real-time.
Thanks.
You can achieve this via interface. Firstly, define an interface class as:
public interface ActivityAdapterInterface {
public void prepareSelection(View v, int position);
}
Now, implement the interface in your Activity as:
public class JPrequirements extends AppCompatActivity implements ActivityAdapterInterface {
...
public void prepareSelection(View v, int position) {
// cool stuff here
}
...
}
Make sure you pass this interface reference to your Adapter via its constructor. Then finally call it on click as:
check_list_item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mActivityAdapterInterface.prepareSelection(v, getAdapterPosition());
}
});
[EDIT]
To provide the interface to your Adapter provide it the constructor.
public class YourAdapter ... {
private ActivityAdapterInterface mActivityAdapterInterface;
public YourAdapter(..., ActivityAdapterInterface activityAdapterInterface) {
activityAdapterInterface = mActivityAdapterInterface;
}
}

How to pass onClick function to Intent

I want to modularize the usage of my class but I have problem in passing function. I want to be able to pass an OnClickListener from 1 activity to this CoachmarkActivity.
I tried 2 different method:
1. Passing an OnClickListener to Intent
2. Passing a class, FollowUpClass, implements Serializable, which has method onClick.
You can see the code below. It is not complete code, but you should be able to comprehend this.
public class CoachmarkActivity extends Activity {
public static final String RES_LAYOUT = "RES-LAYOUT";
public static final String LISTENER = "LISTENER";
public static final String FOLLOW_UP = "FOLLOW-UP";
#Override protected void onCreate(Bundle savedInstance) {
setContentView(getIntent.getIntExtra(RES_LAYOUT, R.layout.activity_default))
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
// 1ST ATTEMPT
// I want to modularize this
OnClickListener onClickPassedFromIntent = (OnClickListener) getIntent().getSerializableExtra(LISTENER);
button1.setOnClickListener(onClickPassedFromIntent);
// 2ND ATTEMPT
final FollowUpListener folllowup = (FollowUpListener) getIntent().getSerializableExtra(FOLLOW_UP);
button2.setOnClickListener(new OnClickListener() {
#Override void onClick() {
// !! Here is error, exception thrown
folllowup.onClick();
}
});
}
/**
* Public method to be used in other activity.
* Invocation wanna be:
* CoachmarkActivity.startThisActivity(getActivity(), R.layout.coachmark1, new OnClickListener() {
* #Override void onClick() {
* // Do something
* }
* });
*/
public static void startThisActivity(Context context, int resId, OnClickListener listener) {
Intent intent = new Intent(context, CoachmarkActivity.class);
intent.putExtra(RES_LAYOUT, resId);
// !! Line below is error, onClickListener is not serializable, no method can accomadate below
intent.putExtra(LISTENER, listener);
context.startActivity(intent);
}
/**
* Public method to be used in other activity.
* Invocation wanna be:
* CoachmarkActivity.startThisActivity(getActivity(), R.layout.coachmark1, new FollowUpListener() {
* #Override void onClick() {
* // Do something
* }
* });
*/
public static void startThisActivity(Context context, int resId, FollowUpListener folllowup) {
Intent intent = new Intent(context, CoachmarkActivity.class);
intent.putExtra(RES_LAYOUT, resId);
intent.putExtra(FOLLOW_UP, followup);
context.startActivity(intent);
}
}
The abstract class:
public abstract class FollowUpListener implements Serializable {
public abstract void onClick();
}
The problems are stated in the comment in source code above, with tag "!!" (Just CTRL+F "!!"). What I want to do is like passing a Delegate object (function in form of variable) in C#, but in Android Java.
Any idea? Thanks.
You are trying to add a Serializable extra to your Intent, but OnClickListener does not implement that interface. You can achieve what you want by creating a class that implements both of the interfaces you need.
private class SerializableClickListener implements View.OnClickListener, Serializable {
#Override public void onClick() {
// TODO handle click
}
}
However, just because you can doesn't mean you should. Sending a click listener to another activity is a horrible code smell, and you should really rethink how you could do this via Intents/Broadcasts.
I tried to pass the OnlclickListener and I couldn't. then I tried this solution.
I made a static click listener variable in a GlobalData class
public static View.OnClickListener btn;
Then when I call the startactivity to go to another activity I did this.
GlobalData.btn = new View.OnClickListener() {
#Override
public void onClick(View v) {
//Listern action
}
};
c.startActivity(new Intent(c, DialogActivity.class));
Then in the second activity, I can set the static listener reference which I used to assign a listener object in the first activity.
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(GlobalData.btn!=null){
GlobalData.btn1.onClick(v);
}
finish();
}
});
I didn't use it directly as a parameter so I can do other stuff in the second activity listener. this worked for me.
But you have to think more because you are using a static reference. this is not a 100% solution. but it's worth trying.

Callback inside anonymous functions, not getting the syntax propely

I am trying to implement a void method callback inside an anonymous class and I am a bit lost with the syntax since I working on a large android code-base. I have set a listener to and image button like so:
MyImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OnFollowingClick click;
click = new OnMyClick(param1, param2); // irrelevant for this thread
click.onClick(view); // irrelevant for this thread
// my problem is here!
click.setCallback( what do I pass in here? and where can define my callback?);
}
});
Here is the OnMyClick class and the void callback interface I defined:
public interface CallbackInterface {
public void Callback();
}
public class OnMyClick implements View.OnClickListener {
// I added this
CallbackInterface mCallBack;
public void setCallback(CallbackInterface callback) {
this.mCallBack = callback;
}
// Please call my callback
public void onFollowingChanged() {
if (mCallBack != null) {
mCallBack.Callback();
}
}
// a bunch of code in here that does not matter
// ...
I have to create callback somewhere, so I am guessing I need something like:
public class SomeClass implements CallbackInterface {
#Override
public void Callback() {
System.out.println("Hey, you called.");
}
}
The problem is that listener is an anonymous function, so I don't want to define a separate class somewhere just to serve as a callback.
I looked at this thread but did not give me much info:
How can an anonymous class use "extends" or "implements"?
is there a way to make the anonymous function implement my interface inside the scope, like:
MyImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void Callback() {
// please call me!
}
Not sure if it made sense, buy any help is greatly appreciated.
thx!
Just define the callback anonymously like you are already doing with the View.OnClickListener.
MyImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OnFollowingClick click;
click = new OnMyClick(param1, param2); // irrelevant for this thread
click.onClick(view); // irrelevant for this thread
// your solution is here!
click.setCallback(new CallbackInterface() {
#Override
public void Callback() {
System.out.println("Hey, you called.");
}
});
}
});
If this is not what you meant then please clarify and I will try and help further.

View.OnClickListener, can you explain?

Sorry if this question might be stupid for you, but I'm new to Android programming and I can't wrap my head around Java syntax.
Can you explain what is happening with this line of code step by step?
View.OnClickListener ourOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v){
ourMessage.setText(“The button got tapped”);
}
};
There is an interface declared inside View class, and it's OnClickListener, it looks like this in View.java source:
/**
* Interface definition for a callback to be invoked when a view is clicked.
*/
public interface OnClickListener {
/**
* Called when a view has been clicked.
*
* #param v The view that was clicked.
*/
void onClick(View v);
}
Normally you would create a class, and have it implement this interface:
public void MyClass implements View.OnClickListener {
#Override
public void onClick(View view) {
// do stuff
}
}
But sometimes you don't need this class in a separate file. Instead, you can create anonymous inner class, it's like creating new class, which only methods are the one from the interface specified:
new View.OnClickListener() {
#Override
public void onClick(View v){
ourMessage.setText(“The button got tapped”);
}
}
You can then use instance of this class everywhere the View.OnClickListener interface is needed.
What's also worth mentioning is that anonymous inner class will hold a reference to the class in which you're creating it. So this will be legal and valid:
public class MyClass {
private int clicksCount = 0;
private View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
clicksCount += 1;
}
}
}
Here you can access clicksCount field, which is field of MyClass even from the inner class that implements OnClickListener. Side note - if you want to access a variable, you need to add final modifier to it:
public void testMethod(final int canAccess, int cantAccess) {
final String test = otherView.getText().toString();
myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Cannot access cantAccess, because it's not final
if (test.length == 0) { // can access
// do something
}
}
}

How to call member functions from onClickListeners

Consider the below piece of code (which works fine by the way) from an Activity:
class ParentActivity extends Activity {
...
#Override
public void btnOK_onClick(final View view) {
// do stuff
}
}
class ChildActivity extends ParentActivity {
...
public void superClick(View view) {
super.btnOK_onClick(view);
}
#Override
public void btnOK_onClick(final View view) {
final BarcodePickActivity me = this;
if (confirmation_needed == true) {
new AlertDialog.Builder(this)
.setTitle(R.string.are_you_sure_)
.setMessage(R.string.are_you_sure_you_want_to_continue)
.setNegativeButton(R.string.no, null)
.setPositiveButton(R.string.yes, new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
me.superClick(view);
}
}).create().show();
} else {
super.btnOK_onClick(view);
}
}
}
In the layout that is used, there is a Button with an onClick set:
<Button
android:id="#+id/btnOK"
android:onClick="btnOK_onClick"
android:text="#string/ok" />
as you see, I have to jump through quite some hoops to get this working:
define a final variable to have this available in the OnClickListener of the AlertDialog,
define an extra method to be able to call super.btnOk_onClick.
As I said, it works fine, but it doesn't look pretty. Is there a way to do this nicer?
Use classname.this from the inner class. So if you class is MySuperClass you can reference MySuperClass.this from an instance inner class.
There is no reason you need to define superClick as you could always just call super.btnOK_onClick(view);.
So all told, you should be able to do something like MySuperClass.super.btnOK_onClick()

Categories