I am building a little app in Android Studio using Kotlin. In one of my activities I have a button which when pressed I want it to call a function. For this I use the onClick attribute in the activity_main.xml android:onClick="". What I want to be able to do is display some information based on the user inputs and update a textView in the same activity.
What I am doing now is setting the onClick attribute to android:onClick"getResult".
The issue I run into is that it requires to have a parameter 'public void getResult(android.view.View)' and what I don't know how to do is what parameter to pass as android.view.view in my getResult() function when I call it in my code.
Here is the complete signature of getResult()
fun getResult(view: View){
//do stuff
}
In your case you have to use:
/** Called when the user touches the button */
fun getResult(view: View){
//view.method()
//(view as Button).method()
}
The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:
The value for this attribute must be the name of the method you want to call in response to a click event.
The method is hosted in the Activity
Be public
Return void
Define a View as its only parameter. This will be the View that was clicked.
You can also declare the click event handler programmatically settign the View.OnClickListener:
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
//..
}
Related
I'm new to Java and Android Development in General. I found a sample which uses the following statement.
sButton.setOnClickListener(this);
in the onCreate() method of the Activity which implements View.OnClickListener where sButton is a Button variable. As far as I understand this register the on click event handler
Later in the sample
public void onClick(View v) {
if(v.getId() == R.id.button_s)
{
//some work
}
this happens.
My question is if it uses the 'this' keyword from inside the activity shouldn't it pass an object of class Activity? If that happens then the Button ID would never match.
I know there are other methods to implement the Button click.
I have a bit of experience with C# and Windows Phone. The procedure there was that the methods were called for the respective buttons without any need for registering them.
Also what is the difference between event handlers and listeners?
Any help would be appreciated! Thanks
The OnClickListener (in this case the Activity instance) is used to declare the behaviour of your application when it consumes click event. However, your application's Main (UI) Thread registers all (UI) events and it will dispatch the appropriate View object as a parameter to your onClick(View v) function.
The snippet you provided - sButton.setOnClickListener(this) simply instructs your application to use the implemented OnClickListener inside your Activity implementation to respond to user clicks. It doesn't forward the this instance as the parameter to the onClick() function, Android OS does that.
In conclusion: The View v parameter in your onClick(View v) function will correspond to the View that the user clicked, regardless of the OnClickListener that has been attached to that View
EDIT: This is (perhaps outdated but) Android's source-code for the performClick() method of the View class. As you can see, inside that method it calls the mOnClickListener.onClick(this) if there's a listener attached, and that's how the view that has been clicked is forwarded to the onClick() method of the appropriate onClickListener object.
One is below
Button btn_stop=(Button) findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
}
});
Second is through XML android:onClick="doClick"
below is code for activity
public void doClick(View v)
{
}
When you do this
sButton.setOnClickListener(this);
this is indeed an instance of Activity. But it is declared as
MyActivity extends Activity implements View.OnClickListener
The important part is that it implements the interface that setOnClickListener takes as argument. This is why it works, this is treated as an OnClickListener, regardless of being an Activity or not.
As for the difference between event handlers and listeners, see this question.
When I create an android:onClick attribute in the activity's xml, the method defined by onClick needs to have the View parameter, why View?
i.e.
onClick method in my activity----------> public void sayHello(View v){...}
consider the next code, I'm not using the view variable, but I still need to pass it in method, how come?:
public void onClick(View view){
TextView t= new TextView(this);
t=(TextView)this.findViewById(R.id.textView2);
t.setText("new text");
}
So you know which View is calling the method.
It's like implementing the OnClickListener for you activity, the method created is onClick(View v) (or arg0 depending on your Eclipse), defining it from xml is just specifying a sort of listener for the View, and the method from the listener as that argument.
Once you're in the method, you can do a switch for the id of the button, to perform different actions:
public void myOnClickMethod(View v){
switch(v.getId()){
case R.id.button1:
//Do something for button 1
break;
case R.id.button2:
//Do something for button 2
break;
}
}
In short. Android just implements the OnClickListener for you when you define the android:onClick="myOnClickMethod" attribute.
Before answering the question I would like to mention what a view is...
Android app contains activities which are like screens which further contains GUI elements(such as buttons). In simple words, those GUI elements are called views.
Answer for your question...
Just imagine a situation in which your activity has more than one button (Let's consider it to be 5) and on getting clicked those buttons make a call to the same function (namely onClick()).So, how would the program know which button was clicked. The answer is simple just pass a view parameter to the onClick() function which will allow access to the information regarding the button that was clicked.
How does Android's View Objects call a method inside my activity? For example, when you generate a view (like a button) dynamically, you have to add an click listener and then your activity will implement click listener and then the button object will have a reference to your activity through the interface.
My question though is when you create buttons in the XML file, you can specify an attribute "onClick" and then give a custom method name such as public void button1pushed(View v) how does the button object reference my activity if my activity doesn't implement anything?
I have a checkbox in the Android layout file. When the checkbox is clicked i call an empty function in the activity class. This causes the app to stop working. Why is this?
I am assuming that you are using the "onclick" attribute in your XML.
If you are receiving a MethodNotFound exception, it could be one of two things:
You have a typo in either your Activity's method name or your XML's method name, or...
The visibility of your method in your Activity is not public.
When specifying onclick values in XML, the method should look like this in your activity:
public void myOnClickMethod(View v) { ... }
I have an activity which contains QuickContactBadges. I'm looking for a way to either chain event listeners on the QuickContactBadge, or to call the default listener from within an override.
Specifically, what I am looking to do is have the QuickContactBadge, when clicked to show the QuickContact card, and then to setResult and finish, to close my activity.
So either I want to add a second listener to the badge in addition to the default one, or implement something like the following:
bdg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
QuickContactBadge bdg = (QuickContactBadge) view;
bdg.base.onClick(); // PSEUDO-CODE LINE
setResult(RESULT_CANCELED, null);
finish();
}
});
Are either of these methods possible, or is there some other way I should be doing this?
Well, the answer to what I was trying to do was not actually in an event listener at all.
The key to getting my activity to close when the QuickBadge is clicked was to add android:noHistory="true" to the activity definition in the application manifest file.
Though, it would still be interesting to know yes/no if there is a way to chain event listeners.