Call a method with element as parameter - java

I have this:
final Button myButton = (Button) findViewById(R.id.mybutton);
How can I pass myButton into a method as a parameter? So I can use it inside a method to do something.
Like, I can call my method hideThis() as MyClass.hideThis(myButton) and it hides the given object.
So, how can i declare the method with the element as the parameter?

Well, you'd better refer to documentation and Java tutorials.
But in your case, you can try:
public class VisibilityManager {
public static void hide(View view){
view.setVisibility(View.GONE);
}
public static void show(View view){
view.setVisibility(View.VISIBLE);
}
}
And then call this like:
final Button mybutton = (Button) findViewById(R.id.mybutton);
// you can do it because `Button extends View`.
VisibilityManager.hide(mybutton);
Read more: View#setVisibility(int)

Related

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.

How to call function from one file class to another file with onClick?

This is probably a duplicate, but i can't find any thing on this so here goes.
I want to call a function in a class from file B.java into A.java with the onClick. Problem is, i get an error every time I add the line in. Here is my code and I'll give the error at the bottom.
A.java
import com.example.app.B;
public class MainService extends Service
{
private CallFunc callFunc;
private Button btn;
public void onCreate()
{
callFunc = new CallFunc();
btn = new Button(this);
//Code for setOnClickListener
#Override
public void onClick(View v)
{
callFunc();
}
}
}
B.java
public class CallFunc
{
public CallFunc()
{
//Stuff to do
}
}
Error I get
The method callFunc() is undefined for the type new View.OnClickListener(){}
//you are not calling your function:
import com.example.app.B;
public class MainService extends Service
{
private CallFunc callFunc;
private Button btn;
public void onCreate()
{
callFunc = new CallFunc();
btn = new Button(this);
//Code for setOnClickListener
#Override
public void onClick(View v)
{
callFunc.callFunc();//It is a good idea to use better method names. it looks like you are calling your constructor, not a method.
}
}
}
In your CallFunc class,
public CallFunc()
{
//Stuff to do
}
means it's constructor. It get called when you create it. In here callFunc = new CallFunc();.
You can include it to onClick method.
#Override
public void onClick(View v)
{
callFunc = new CallFunc();
}
Better way is to do this is add a method to CallFunc class because method means you do something. You do your stuff in that method. Class means a object like a car. Car can be drive. so it should have dirve() method. Then car.drive() means you drive the car. :)
#Override
public void onClick(View v)
{
callFunc.someMethod();
}
What you have in your B.java is a constructor not a method. A constructor is a special case method that gets invoked when an instance of that class is created. It is used to correctly initialise and populate new instances of a class.
What you were probably trying to do is:
public class CallFunc
{
public CallFunc()
{
// this is the constructor
// initialise the class instance here
}
public void someMethod()
{
// a method you can call
// perform actions here
}
}
Then in the onCreate() of your MainService you should:
#Override
public void onClick(View v)
{
callFunc.someMethod();
}
However, you are extending Service and using UI components (Button and onClick) when a Service does not have a UI. Are you sure you didn't want to use an Activity?

Method definition passed as argument

I saw this code when looking at an Android example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editNumber;
Button btnCall = (Button) this.findViewById( R.id.btnCall);
editNumber = (EditText) this.findViewById(R.id.editNumber);
btnCall.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
call();
}
});
// ...
}
Here:
new OnClickListener() {
public void onClick(View v) {
call();
}
}
is passed to setOnClickListener() as a parameter. What I don't understand is what code inside {...} does here? if new OnClickListener() calls the constructor, and the constructor returns an object, yes, object can be passed to method as a parameter, but what is:
{
public void onClick(View v) {
call();
}
}
doing here? It looks like a method definition?
Thanks a lot for the help!
As #Perception said, it is an anonymous inner class. btnCall.setOnClickListenter() is expecting an argument that has the type OnClickListener. You could instantiate a concrete reference to an OnClickListener and pass that as an argument but if you are never going to refer to it again, sometimes it is easier to simply make an anonymous inner class.

OnClickListener super

Why can I not pass this as Context in following code?
I am getting error
The constructor DataManager(new View.OnClickListener(){}) is undefined
I am new to android programming, so sorry if question is weird!!
Any help would be great.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("vkx", "clk insert");
String qq="insert into users(id,name,password) values ('6','usrs','passs')";
DataManager dm=new DataManager(this);
dm.SqliteExecutenonquery(qq);
Log.d("vkx", "clk insert done");
}
});
I have my DataManger class
public DataManager(Context cont) {
super(cont, DBname, null, DBver);
}
....
So what you are doing is when instantiating DataManager, You are not passing a context as a paramater but actually your instance of OnClickListener.
DataManager dm=new DataManager(YOUR_CLASS_NAME.this);
YOUR_CLASS_NAME should be the Activity class name. ie
class MyClass extends Activity{
public onCreate(Bundle savedInstanceState){
Button btn = new Button();
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
DataManager dm=new DataManager(MyClass.this);
}
});
}
}
Give this a try and let me know how it goes
Just write MyClassName.this, assuming that 'MyClassName' is the Name of your Activity.
before setOnClickListener add this line:
final Context ThisContext = this;
and in your listener use this
DataManager dm=new DataManager(ThisContext);
You have written anonymous class new OnClickListener() {
There is no constructor for DataManager class with OnClickListener as type parameter.
I think instead of this, you need use getApplicationContext();
"this" is a reference to the current instance of the containing class.
Where you have used "this", it refers to an instance of an anonymous inner class of type onClickListener.
I believe that you might be wanting to pass the context of the Activity defining the onClickListener? If so, use MyActivity.this, where MyActivity is the name of the Activity class.

Why 'this' can be used as the argument here in Java?

public class Activity01 extends Activity implements OnClickListener,
ViewFactory {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout main_view = new LinearLayout(this);
m_Switcher = new ImageSwitcher(this);
main_view.addView(m_Switcher);
m_Switcher.setId(SWITCHER_ID);
m_Switcher.setFactory(this);
m_Switcher.setOnClickListener(this);
setContentView(main_view);
...
}
public void onClick(View v) {
...
}
}
Above code is from an Android project, and below function's argument is set as 'this', why?
m_Switcher.setOnClickListener(this);
According to the javadoc, here should be like below:
public void setOnClickListener (View.OnClickListener l)
That means the argument should be this kind: View.OnClickListener
So why 'this' can be there? Thanks!
Note: According to the answers, I gave a more complete code above.
In the class declaration you will find it either extends or implements OnClickListener. That means that the class can be used as an OnClickListener (because it is one, amongst other things). That is why you can use this here.

Categories