Android - Kotlin - How to refer to a dynamicly generated button?
I try to interact with a dynamicly generated buttons. I want to update text and background color for those player clicked and for those who is near by horizontal or vertical axis at the moment of a click.
What I've tryed. I've found an id property of a button in xml which led me to idea that I can make a text key to refer to a programmatically genereted button. But when I've tryed to assign an id - IDE was expecting a number (Int), not a string. Since buttons form a square array - I decided to encode each button via 4 digit number where first 2 digits stand for a row number and other two stand for a column number. Though when I tryed to use findViewById IDE told me that it was expecting a special id data type, not a number.
That's how it looks for now:
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
button.id = i*100 + j
constraintLayout.addView(button)
}
}
What idea or method could I look at?
android button kotlin id
add a comment |
I try to interact with a dynamicly generated buttons. I want to update text and background color for those player clicked and for those who is near by horizontal or vertical axis at the moment of a click.
What I've tryed. I've found an id property of a button in xml which led me to idea that I can make a text key to refer to a programmatically genereted button. But when I've tryed to assign an id - IDE was expecting a number (Int), not a string. Since buttons form a square array - I decided to encode each button via 4 digit number where first 2 digits stand for a row number and other two stand for a column number. Though when I tryed to use findViewById IDE told me that it was expecting a special id data type, not a number.
That's how it looks for now:
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
button.id = i*100 + j
constraintLayout.addView(button)
}
}
What idea or method could I look at?
android button kotlin id
That IDE message about the special ID data type is from lint. You should be able to suppress it. Put your cursor on the problematic line, and hit alt-enter. There should be a menu item for it.
– Mike M.
Nov 20 '18 at 5:02
if you have only your custom buttons with id you can get all the childs of coordinator layout and iterate over them.
– Karan Mer
Nov 20 '18 at 5:04
1
Mike, thanks! I've tryed to use val btn = constraintLayout.findViewById<View>(101) as Button and via toast I was able to confirm it's the right button by btn.text.toString()
– Roman Voronov
Nov 20 '18 at 5:18
add a comment |
I try to interact with a dynamicly generated buttons. I want to update text and background color for those player clicked and for those who is near by horizontal or vertical axis at the moment of a click.
What I've tryed. I've found an id property of a button in xml which led me to idea that I can make a text key to refer to a programmatically genereted button. But when I've tryed to assign an id - IDE was expecting a number (Int), not a string. Since buttons form a square array - I decided to encode each button via 4 digit number where first 2 digits stand for a row number and other two stand for a column number. Though when I tryed to use findViewById IDE told me that it was expecting a special id data type, not a number.
That's how it looks for now:
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
button.id = i*100 + j
constraintLayout.addView(button)
}
}
What idea or method could I look at?
android button kotlin id
I try to interact with a dynamicly generated buttons. I want to update text and background color for those player clicked and for those who is near by horizontal or vertical axis at the moment of a click.
What I've tryed. I've found an id property of a button in xml which led me to idea that I can make a text key to refer to a programmatically genereted button. But when I've tryed to assign an id - IDE was expecting a number (Int), not a string. Since buttons form a square array - I decided to encode each button via 4 digit number where first 2 digits stand for a row number and other two stand for a column number. Though when I tryed to use findViewById IDE told me that it was expecting a special id data type, not a number.
That's how it looks for now:
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
button.id = i*100 + j
constraintLayout.addView(button)
}
}
What idea or method could I look at?
android button kotlin id
android button kotlin id
asked Nov 20 '18 at 5:00
Roman VoronovRoman Voronov
54
54
That IDE message about the special ID data type is from lint. You should be able to suppress it. Put your cursor on the problematic line, and hit alt-enter. There should be a menu item for it.
– Mike M.
Nov 20 '18 at 5:02
if you have only your custom buttons with id you can get all the childs of coordinator layout and iterate over them.
– Karan Mer
Nov 20 '18 at 5:04
1
Mike, thanks! I've tryed to use val btn = constraintLayout.findViewById<View>(101) as Button and via toast I was able to confirm it's the right button by btn.text.toString()
– Roman Voronov
Nov 20 '18 at 5:18
add a comment |
That IDE message about the special ID data type is from lint. You should be able to suppress it. Put your cursor on the problematic line, and hit alt-enter. There should be a menu item for it.
– Mike M.
Nov 20 '18 at 5:02
if you have only your custom buttons with id you can get all the childs of coordinator layout and iterate over them.
– Karan Mer
Nov 20 '18 at 5:04
1
Mike, thanks! I've tryed to use val btn = constraintLayout.findViewById<View>(101) as Button and via toast I was able to confirm it's the right button by btn.text.toString()
– Roman Voronov
Nov 20 '18 at 5:18
That IDE message about the special ID data type is from lint. You should be able to suppress it. Put your cursor on the problematic line, and hit alt-enter. There should be a menu item for it.
– Mike M.
Nov 20 '18 at 5:02
That IDE message about the special ID data type is from lint. You should be able to suppress it. Put your cursor on the problematic line, and hit alt-enter. There should be a menu item for it.
– Mike M.
Nov 20 '18 at 5:02
if you have only your custom buttons with id you can get all the childs of coordinator layout and iterate over them.
– Karan Mer
Nov 20 '18 at 5:04
if you have only your custom buttons with id you can get all the childs of coordinator layout and iterate over them.
– Karan Mer
Nov 20 '18 at 5:04
1
1
Mike, thanks! I've tryed to use val btn = constraintLayout.findViewById<View>(101) as Button and via toast I was able to confirm it's the right button by btn.text.toString()
– Roman Voronov
Nov 20 '18 at 5:18
Mike, thanks! I've tryed to use val btn = constraintLayout.findViewById<View>(101) as Button and via toast I was able to confirm it's the right button by btn.text.toString()
– Roman Voronov
Nov 20 '18 at 5:18
add a comment |
1 Answer
1
active
oldest
votes
If you created it dynamically you can save it in a variable (or an array) for later use.
val myButtons = ArrayList<Button>()
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
myButtons.add(button)
constraintLayout.addView(button)
}
}
if you have a layout with dynamically created views and you know their order you can get them with getChildAt(index)
.
Alternatively you can assign ids saved in xml like this.
If you set that buttons iD can you access it via the id like you would with views in XML file?
– Brandon
Nov 20 '18 at 5:21
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%2f53386498%2fandroid-kotlin-how-to-refer-to-a-dynamicly-generated-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you created it dynamically you can save it in a variable (or an array) for later use.
val myButtons = ArrayList<Button>()
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
myButtons.add(button)
constraintLayout.addView(button)
}
}
if you have a layout with dynamically created views and you know their order you can get them with getChildAt(index)
.
Alternatively you can assign ids saved in xml like this.
If you set that buttons iD can you access it via the id like you would with views in XML file?
– Brandon
Nov 20 '18 at 5:21
add a comment |
If you created it dynamically you can save it in a variable (or an array) for later use.
val myButtons = ArrayList<Button>()
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
myButtons.add(button)
constraintLayout.addView(button)
}
}
if you have a layout with dynamically created views and you know their order you can get them with getChildAt(index)
.
Alternatively you can assign ids saved in xml like this.
If you set that buttons iD can you access it via the id like you would with views in XML file?
– Brandon
Nov 20 '18 at 5:21
add a comment |
If you created it dynamically you can save it in a variable (or an array) for later use.
val myButtons = ArrayList<Button>()
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
myButtons.add(button)
constraintLayout.addView(button)
}
}
if you have a layout with dynamically created views and you know their order you can get them with getChildAt(index)
.
Alternatively you can assign ids saved in xml like this.
If you created it dynamically you can save it in a variable (or an array) for later use.
val myButtons = ArrayList<Button>()
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
myButtons.add(button)
constraintLayout.addView(button)
}
}
if you have a layout with dynamically created views and you know their order you can get them with getChildAt(index)
.
Alternatively you can assign ids saved in xml like this.
edited Nov 20 '18 at 5:25
answered Nov 20 '18 at 5:14
Leo PelozoLeo Pelozo
158110
158110
If you set that buttons iD can you access it via the id like you would with views in XML file?
– Brandon
Nov 20 '18 at 5:21
add a comment |
If you set that buttons iD can you access it via the id like you would with views in XML file?
– Brandon
Nov 20 '18 at 5:21
If you set that buttons iD can you access it via the id like you would with views in XML file?
– Brandon
Nov 20 '18 at 5:21
If you set that buttons iD can you access it via the id like you would with views in XML file?
– Brandon
Nov 20 '18 at 5:21
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%2f53386498%2fandroid-kotlin-how-to-refer-to-a-dynamicly-generated-button%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
That IDE message about the special ID data type is from lint. You should be able to suppress it. Put your cursor on the problematic line, and hit alt-enter. There should be a menu item for it.
– Mike M.
Nov 20 '18 at 5:02
if you have only your custom buttons with id you can get all the childs of coordinator layout and iterate over them.
– Karan Mer
Nov 20 '18 at 5:04
1
Mike, thanks! I've tryed to use val btn = constraintLayout.findViewById<View>(101) as Button and via toast I was able to confirm it's the right button by btn.text.toString()
– Roman Voronov
Nov 20 '18 at 5:18