btn.setOnClickListener() how does it calls
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am beginner in programing, and I know how btn.setOnClickListener{} function works (curly brackets).
But there is other tipe of function btn.setOnClickListener() - brackets are not curly. I do not know how and when I should use this tipe of functions. How does such type of function calls? I would like to learn more about it but I do not know how to google it
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
kotlin
add a comment |
I am beginner in programing, and I know how btn.setOnClickListener{} function works (curly brackets).
But there is other tipe of function btn.setOnClickListener() - brackets are not curly. I do not know how and when I should use this tipe of functions. How does such type of function calls? I would like to learn more about it but I do not know how to google it
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
kotlin
According to this article: antonioleiva.com/lambdas-kotlin-androidparanthesis ()
are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
– Feelsbadman
Jan 3 at 22:54
add a comment |
I am beginner in programing, and I know how btn.setOnClickListener{} function works (curly brackets).
But there is other tipe of function btn.setOnClickListener() - brackets are not curly. I do not know how and when I should use this tipe of functions. How does such type of function calls? I would like to learn more about it but I do not know how to google it
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
kotlin
I am beginner in programing, and I know how btn.setOnClickListener{} function works (curly brackets).
But there is other tipe of function btn.setOnClickListener() - brackets are not curly. I do not know how and when I should use this tipe of functions. How does such type of function calls? I would like to learn more about it but I do not know how to google it
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
kotlin
kotlin
edited Feb 5 at 21:27
Павел Малиновский
asked Jan 3 at 22:50
Павел МалиновскийПавел Малиновский
913
913
According to this article: antonioleiva.com/lambdas-kotlin-androidparanthesis ()
are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
– Feelsbadman
Jan 3 at 22:54
add a comment |
According to this article: antonioleiva.com/lambdas-kotlin-androidparanthesis ()
are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
– Feelsbadman
Jan 3 at 22:54
According to this article: antonioleiva.com/lambdas-kotlin-android
paranthesis ()
are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"– Feelsbadman
Jan 3 at 22:54
According to this article: antonioleiva.com/lambdas-kotlin-android
paranthesis ()
are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"– Feelsbadman
Jan 3 at 22:54
add a comment |
4 Answers
4
active
oldest
votes
According to : https://antonioleiva.com/lambdas-kotlin-android/
If a function has only one parameter, and this is a function, the parentheses can be deleted
Instead of having empty parentheses, we can better delete them:
view.setOnClickListener { v -> toast("Hello") }
If the function’s last parameter is a function, it can go outside the parentheses
Therefore, we can extract the listener as follows:
view.setOnClickListener() { v -> toast("Hello") }
If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.
add a comment |
Both types are equivalent:
button.setOnClickListener {
// ......................
}
button.setOnClickListener(View.OnClickListener {
// ......................
})
but the 1st one is the preferred way to go.
Even if you write the 2nd one, if you hover the mouse over View.OnClickListener
,
Android Studio will pop up this message:
Redundant SAM constructor
and if you press Alt-Enter you will be prompted:
Remove redundant SAM constructor
and if you click on it then View.OnClickListener
will be removed.
Again by pressing Alt-Enter you will be prompted:
Move lambda argument out of parentheses
and if you click on it then you will get the 1st type.
So don't worry about it, use the 1st type and you will be fine.
add a comment |
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
add a comment |
setOnClickListener(Interface i)
This is Method of View Class in Android.
1. setOnClickListener is method of that class which except only interface as a parameter.
2. or else you have to implement that interface in your class like given example.
Go through Anonymous Class implement process.
the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.
1. When you want to implement in class use this
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
2. Pass interface object of onClickListener
appView.setOnClickListener(); ;----> pass interface object
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030889%2fbtn-setonclicklistener-how-does-it-calls%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to : https://antonioleiva.com/lambdas-kotlin-android/
If a function has only one parameter, and this is a function, the parentheses can be deleted
Instead of having empty parentheses, we can better delete them:
view.setOnClickListener { v -> toast("Hello") }
If the function’s last parameter is a function, it can go outside the parentheses
Therefore, we can extract the listener as follows:
view.setOnClickListener() { v -> toast("Hello") }
If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.
add a comment |
According to : https://antonioleiva.com/lambdas-kotlin-android/
If a function has only one parameter, and this is a function, the parentheses can be deleted
Instead of having empty parentheses, we can better delete them:
view.setOnClickListener { v -> toast("Hello") }
If the function’s last parameter is a function, it can go outside the parentheses
Therefore, we can extract the listener as follows:
view.setOnClickListener() { v -> toast("Hello") }
If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.
add a comment |
According to : https://antonioleiva.com/lambdas-kotlin-android/
If a function has only one parameter, and this is a function, the parentheses can be deleted
Instead of having empty parentheses, we can better delete them:
view.setOnClickListener { v -> toast("Hello") }
If the function’s last parameter is a function, it can go outside the parentheses
Therefore, we can extract the listener as follows:
view.setOnClickListener() { v -> toast("Hello") }
If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.
According to : https://antonioleiva.com/lambdas-kotlin-android/
If a function has only one parameter, and this is a function, the parentheses can be deleted
Instead of having empty parentheses, we can better delete them:
view.setOnClickListener { v -> toast("Hello") }
If the function’s last parameter is a function, it can go outside the parentheses
Therefore, we can extract the listener as follows:
view.setOnClickListener() { v -> toast("Hello") }
If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.
answered Jan 3 at 23:00
FeelsbadmanFeelsbadman
696724
696724
add a comment |
add a comment |
Both types are equivalent:
button.setOnClickListener {
// ......................
}
button.setOnClickListener(View.OnClickListener {
// ......................
})
but the 1st one is the preferred way to go.
Even if you write the 2nd one, if you hover the mouse over View.OnClickListener
,
Android Studio will pop up this message:
Redundant SAM constructor
and if you press Alt-Enter you will be prompted:
Remove redundant SAM constructor
and if you click on it then View.OnClickListener
will be removed.
Again by pressing Alt-Enter you will be prompted:
Move lambda argument out of parentheses
and if you click on it then you will get the 1st type.
So don't worry about it, use the 1st type and you will be fine.
add a comment |
Both types are equivalent:
button.setOnClickListener {
// ......................
}
button.setOnClickListener(View.OnClickListener {
// ......................
})
but the 1st one is the preferred way to go.
Even if you write the 2nd one, if you hover the mouse over View.OnClickListener
,
Android Studio will pop up this message:
Redundant SAM constructor
and if you press Alt-Enter you will be prompted:
Remove redundant SAM constructor
and if you click on it then View.OnClickListener
will be removed.
Again by pressing Alt-Enter you will be prompted:
Move lambda argument out of parentheses
and if you click on it then you will get the 1st type.
So don't worry about it, use the 1st type and you will be fine.
add a comment |
Both types are equivalent:
button.setOnClickListener {
// ......................
}
button.setOnClickListener(View.OnClickListener {
// ......................
})
but the 1st one is the preferred way to go.
Even if you write the 2nd one, if you hover the mouse over View.OnClickListener
,
Android Studio will pop up this message:
Redundant SAM constructor
and if you press Alt-Enter you will be prompted:
Remove redundant SAM constructor
and if you click on it then View.OnClickListener
will be removed.
Again by pressing Alt-Enter you will be prompted:
Move lambda argument out of parentheses
and if you click on it then you will get the 1st type.
So don't worry about it, use the 1st type and you will be fine.
Both types are equivalent:
button.setOnClickListener {
// ......................
}
button.setOnClickListener(View.OnClickListener {
// ......................
})
but the 1st one is the preferred way to go.
Even if you write the 2nd one, if you hover the mouse over View.OnClickListener
,
Android Studio will pop up this message:
Redundant SAM constructor
and if you press Alt-Enter you will be prompted:
Remove redundant SAM constructor
and if you click on it then View.OnClickListener
will be removed.
Again by pressing Alt-Enter you will be prompted:
Move lambda argument out of parentheses
and if you click on it then you will get the 1st type.
So don't worry about it, use the 1st type and you will be fine.
answered Jan 3 at 23:07
forpasforpas
19.9k4830
19.9k4830
add a comment |
add a comment |
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
add a comment |
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
add a comment |
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
answered Feb 7 at 13:37
Павел МалиновскийПавел Малиновский
913
913
add a comment |
add a comment |
setOnClickListener(Interface i)
This is Method of View Class in Android.
1. setOnClickListener is method of that class which except only interface as a parameter.
2. or else you have to implement that interface in your class like given example.
Go through Anonymous Class implement process.
the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.
1. When you want to implement in class use this
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
2. Pass interface object of onClickListener
appView.setOnClickListener(); ;----> pass interface object
add a comment |
setOnClickListener(Interface i)
This is Method of View Class in Android.
1. setOnClickListener is method of that class which except only interface as a parameter.
2. or else you have to implement that interface in your class like given example.
Go through Anonymous Class implement process.
the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.
1. When you want to implement in class use this
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
2. Pass interface object of onClickListener
appView.setOnClickListener(); ;----> pass interface object
add a comment |
setOnClickListener(Interface i)
This is Method of View Class in Android.
1. setOnClickListener is method of that class which except only interface as a parameter.
2. or else you have to implement that interface in your class like given example.
Go through Anonymous Class implement process.
the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.
1. When you want to implement in class use this
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
2. Pass interface object of onClickListener
appView.setOnClickListener(); ;----> pass interface object
setOnClickListener(Interface i)
This is Method of View Class in Android.
1. setOnClickListener is method of that class which except only interface as a parameter.
2. or else you have to implement that interface in your class like given example.
Go through Anonymous Class implement process.
the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.
1. When you want to implement in class use this
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
2. Pass interface object of onClickListener
appView.setOnClickListener(); ;----> pass interface object
answered Feb 7 at 14:10
ZafarHussainZafarHussain
15727
15727
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030889%2fbtn-setonclicklistener-how-does-it-calls%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
According to this article: antonioleiva.com/lambdas-kotlin-android
paranthesis ()
are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"– Feelsbadman
Jan 3 at 22:54