How to connect declarations and If statements?
I want to change multiple textView colors through a couple if statements. All of them have different ID's except for the last part of them that ends with "....Price". I have tried just pasting the if statements on all of them but the build fails.
i have made declarations like this.
textView1Price = ("+10.00")
textView2Price = ("-10.00")
textView3Price = ("0.00")
and the if statements like so.
if (colorID.text.startsWith("-")) {
colorID.setTextColor(Color.RED)
}
if (colorID.text.startsWith("+")) {
colorID.setTextColor(Color.GREEN)
}
if (colorID.text.startsWith("0.00")) {
colorID.text = "_"
colorID.setTextColor(Color.DKGRAY)
I am having trouble figuring out how to connect the declarations to the If statements. I have tried something like this with partial success but can't list more that one declaration.
val colorID = textView1Price
i have also tried to find a way to reference the
text.contains("Price")
but have not been able to. Any help is appreciated. Thanks for your time.
EDIT: Solution Follow up
//Declarations
//metal location A price
Price1 = ("+10.00")
//metal location B price
Price2 = ("-10.00")
//metal location C price
Price3 = ("0.00")
Android Studio suggested when statement over if
(1..912).forEach {
val id = resources.getIdentifier("Price$it", "id", packageName)
val tv = findViewById<TextView>(id)
when {
tv.text.startsWith("-") -> tv.setTextColor(Color.RED)
tv.text.startsWith("+") -> tv.setTextColor(Color.GREEN)
tv.text.startsWith("0.00") -> {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
}

add a comment |
I want to change multiple textView colors through a couple if statements. All of them have different ID's except for the last part of them that ends with "....Price". I have tried just pasting the if statements on all of them but the build fails.
i have made declarations like this.
textView1Price = ("+10.00")
textView2Price = ("-10.00")
textView3Price = ("0.00")
and the if statements like so.
if (colorID.text.startsWith("-")) {
colorID.setTextColor(Color.RED)
}
if (colorID.text.startsWith("+")) {
colorID.setTextColor(Color.GREEN)
}
if (colorID.text.startsWith("0.00")) {
colorID.text = "_"
colorID.setTextColor(Color.DKGRAY)
I am having trouble figuring out how to connect the declarations to the If statements. I have tried something like this with partial success but can't list more that one declaration.
val colorID = textView1Price
i have also tried to find a way to reference the
text.contains("Price")
but have not been able to. Any help is appreciated. Thanks for your time.
EDIT: Solution Follow up
//Declarations
//metal location A price
Price1 = ("+10.00")
//metal location B price
Price2 = ("-10.00")
//metal location C price
Price3 = ("0.00")
Android Studio suggested when statement over if
(1..912).forEach {
val id = resources.getIdentifier("Price$it", "id", packageName)
val tv = findViewById<TextView>(id)
when {
tv.text.startsWith("-") -> tv.setTextColor(Color.RED)
tv.text.startsWith("+") -> tv.setTextColor(Color.GREEN)
tv.text.startsWith("0.00") -> {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
}

add a comment |
I want to change multiple textView colors through a couple if statements. All of them have different ID's except for the last part of them that ends with "....Price". I have tried just pasting the if statements on all of them but the build fails.
i have made declarations like this.
textView1Price = ("+10.00")
textView2Price = ("-10.00")
textView3Price = ("0.00")
and the if statements like so.
if (colorID.text.startsWith("-")) {
colorID.setTextColor(Color.RED)
}
if (colorID.text.startsWith("+")) {
colorID.setTextColor(Color.GREEN)
}
if (colorID.text.startsWith("0.00")) {
colorID.text = "_"
colorID.setTextColor(Color.DKGRAY)
I am having trouble figuring out how to connect the declarations to the If statements. I have tried something like this with partial success but can't list more that one declaration.
val colorID = textView1Price
i have also tried to find a way to reference the
text.contains("Price")
but have not been able to. Any help is appreciated. Thanks for your time.
EDIT: Solution Follow up
//Declarations
//metal location A price
Price1 = ("+10.00")
//metal location B price
Price2 = ("-10.00")
//metal location C price
Price3 = ("0.00")
Android Studio suggested when statement over if
(1..912).forEach {
val id = resources.getIdentifier("Price$it", "id", packageName)
val tv = findViewById<TextView>(id)
when {
tv.text.startsWith("-") -> tv.setTextColor(Color.RED)
tv.text.startsWith("+") -> tv.setTextColor(Color.GREEN)
tv.text.startsWith("0.00") -> {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
}

I want to change multiple textView colors through a couple if statements. All of them have different ID's except for the last part of them that ends with "....Price". I have tried just pasting the if statements on all of them but the build fails.
i have made declarations like this.
textView1Price = ("+10.00")
textView2Price = ("-10.00")
textView3Price = ("0.00")
and the if statements like so.
if (colorID.text.startsWith("-")) {
colorID.setTextColor(Color.RED)
}
if (colorID.text.startsWith("+")) {
colorID.setTextColor(Color.GREEN)
}
if (colorID.text.startsWith("0.00")) {
colorID.text = "_"
colorID.setTextColor(Color.DKGRAY)
I am having trouble figuring out how to connect the declarations to the If statements. I have tried something like this with partial success but can't list more that one declaration.
val colorID = textView1Price
i have also tried to find a way to reference the
text.contains("Price")
but have not been able to. Any help is appreciated. Thanks for your time.
EDIT: Solution Follow up
//Declarations
//metal location A price
Price1 = ("+10.00")
//metal location B price
Price2 = ("-10.00")
//metal location C price
Price3 = ("0.00")
Android Studio suggested when statement over if
(1..912).forEach {
val id = resources.getIdentifier("Price$it", "id", packageName)
val tv = findViewById<TextView>(id)
when {
tv.text.startsWith("-") -> tv.setTextColor(Color.RED)
tv.text.startsWith("+") -> tv.setTextColor(Color.GREEN)
tv.text.startsWith("0.00") -> {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
}


edited Jan 2 at 21:58
randbo
asked Jan 1 at 18:59
randborandbo
165
165
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
For the TextViews that have ids like textView?Price
you can use the getIdentifier()
method to get their integer ids in a loop and then apply the conditions, like this (for 9 TextViews):
(1..9).forEach {
val id = resources.getIdentifier("textView${it}Price", "id", packageName)
val tv = findViewById<TextView>(id)
if (tv.text.startsWith("-")) {
tv.setTextColor(Color.RED)
} else if (tv.text.startsWith("+")) {
tv.setTextColor(Color.GREEN)
} else if (tv.text.startsWith("0.00")) {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
For resources
and packageName
you may need to supply a valid Context
like:
val id = context.resources.getIdentifier("textView${it}Price", "id", context.packageName)
if your code is not inside an activity.
For the other TextViews, if their ids have such a similarity, you can use the same method.
Thanks for commenting. I am fairly new to this. Could you elaborate a little more? For the (1..9).forEach what if i have no determined amount? The textView#Price example was for easy reference. The ID's of each only have Price as the commonfactor. Sorry for the inconvenience.
– randbo
Jan 1 at 21:07
I assume that you have textviews liketextView1Price
,textView2Price
,.. if the last one's id is textView1000Price, you just have to change the range to (1..1000
). If the ids have not such a similarity then this code won't work.
– forpas
Jan 1 at 21:10
i think i can make this work. ill just have to comment over all the entries so i can keep track of the values. example NewYorkPrice =. will be changed to textView1Price with a comment above stating New York. Thanks for your help i apprciate it.
– randbo
Jan 1 at 21:19
As a sidenote, I tend to usewith(tv) { when { text.startsWith() -> ... } }
for Kotlin
– shkschneider
Jan 2 at 13:57
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%2f53998118%2fhow-to-connect-declarations-and-if-statements%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
For the TextViews that have ids like textView?Price
you can use the getIdentifier()
method to get their integer ids in a loop and then apply the conditions, like this (for 9 TextViews):
(1..9).forEach {
val id = resources.getIdentifier("textView${it}Price", "id", packageName)
val tv = findViewById<TextView>(id)
if (tv.text.startsWith("-")) {
tv.setTextColor(Color.RED)
} else if (tv.text.startsWith("+")) {
tv.setTextColor(Color.GREEN)
} else if (tv.text.startsWith("0.00")) {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
For resources
and packageName
you may need to supply a valid Context
like:
val id = context.resources.getIdentifier("textView${it}Price", "id", context.packageName)
if your code is not inside an activity.
For the other TextViews, if their ids have such a similarity, you can use the same method.
Thanks for commenting. I am fairly new to this. Could you elaborate a little more? For the (1..9).forEach what if i have no determined amount? The textView#Price example was for easy reference. The ID's of each only have Price as the commonfactor. Sorry for the inconvenience.
– randbo
Jan 1 at 21:07
I assume that you have textviews liketextView1Price
,textView2Price
,.. if the last one's id is textView1000Price, you just have to change the range to (1..1000
). If the ids have not such a similarity then this code won't work.
– forpas
Jan 1 at 21:10
i think i can make this work. ill just have to comment over all the entries so i can keep track of the values. example NewYorkPrice =. will be changed to textView1Price with a comment above stating New York. Thanks for your help i apprciate it.
– randbo
Jan 1 at 21:19
As a sidenote, I tend to usewith(tv) { when { text.startsWith() -> ... } }
for Kotlin
– shkschneider
Jan 2 at 13:57
add a comment |
For the TextViews that have ids like textView?Price
you can use the getIdentifier()
method to get their integer ids in a loop and then apply the conditions, like this (for 9 TextViews):
(1..9).forEach {
val id = resources.getIdentifier("textView${it}Price", "id", packageName)
val tv = findViewById<TextView>(id)
if (tv.text.startsWith("-")) {
tv.setTextColor(Color.RED)
} else if (tv.text.startsWith("+")) {
tv.setTextColor(Color.GREEN)
} else if (tv.text.startsWith("0.00")) {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
For resources
and packageName
you may need to supply a valid Context
like:
val id = context.resources.getIdentifier("textView${it}Price", "id", context.packageName)
if your code is not inside an activity.
For the other TextViews, if their ids have such a similarity, you can use the same method.
Thanks for commenting. I am fairly new to this. Could you elaborate a little more? For the (1..9).forEach what if i have no determined amount? The textView#Price example was for easy reference. The ID's of each only have Price as the commonfactor. Sorry for the inconvenience.
– randbo
Jan 1 at 21:07
I assume that you have textviews liketextView1Price
,textView2Price
,.. if the last one's id is textView1000Price, you just have to change the range to (1..1000
). If the ids have not such a similarity then this code won't work.
– forpas
Jan 1 at 21:10
i think i can make this work. ill just have to comment over all the entries so i can keep track of the values. example NewYorkPrice =. will be changed to textView1Price with a comment above stating New York. Thanks for your help i apprciate it.
– randbo
Jan 1 at 21:19
As a sidenote, I tend to usewith(tv) { when { text.startsWith() -> ... } }
for Kotlin
– shkschneider
Jan 2 at 13:57
add a comment |
For the TextViews that have ids like textView?Price
you can use the getIdentifier()
method to get their integer ids in a loop and then apply the conditions, like this (for 9 TextViews):
(1..9).forEach {
val id = resources.getIdentifier("textView${it}Price", "id", packageName)
val tv = findViewById<TextView>(id)
if (tv.text.startsWith("-")) {
tv.setTextColor(Color.RED)
} else if (tv.text.startsWith("+")) {
tv.setTextColor(Color.GREEN)
} else if (tv.text.startsWith("0.00")) {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
For resources
and packageName
you may need to supply a valid Context
like:
val id = context.resources.getIdentifier("textView${it}Price", "id", context.packageName)
if your code is not inside an activity.
For the other TextViews, if their ids have such a similarity, you can use the same method.
For the TextViews that have ids like textView?Price
you can use the getIdentifier()
method to get their integer ids in a loop and then apply the conditions, like this (for 9 TextViews):
(1..9).forEach {
val id = resources.getIdentifier("textView${it}Price", "id", packageName)
val tv = findViewById<TextView>(id)
if (tv.text.startsWith("-")) {
tv.setTextColor(Color.RED)
} else if (tv.text.startsWith("+")) {
tv.setTextColor(Color.GREEN)
} else if (tv.text.startsWith("0.00")) {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
For resources
and packageName
you may need to supply a valid Context
like:
val id = context.resources.getIdentifier("textView${it}Price", "id", context.packageName)
if your code is not inside an activity.
For the other TextViews, if their ids have such a similarity, you can use the same method.
answered Jan 1 at 19:11
forpasforpas
16.5k3627
16.5k3627
Thanks for commenting. I am fairly new to this. Could you elaborate a little more? For the (1..9).forEach what if i have no determined amount? The textView#Price example was for easy reference. The ID's of each only have Price as the commonfactor. Sorry for the inconvenience.
– randbo
Jan 1 at 21:07
I assume that you have textviews liketextView1Price
,textView2Price
,.. if the last one's id is textView1000Price, you just have to change the range to (1..1000
). If the ids have not such a similarity then this code won't work.
– forpas
Jan 1 at 21:10
i think i can make this work. ill just have to comment over all the entries so i can keep track of the values. example NewYorkPrice =. will be changed to textView1Price with a comment above stating New York. Thanks for your help i apprciate it.
– randbo
Jan 1 at 21:19
As a sidenote, I tend to usewith(tv) { when { text.startsWith() -> ... } }
for Kotlin
– shkschneider
Jan 2 at 13:57
add a comment |
Thanks for commenting. I am fairly new to this. Could you elaborate a little more? For the (1..9).forEach what if i have no determined amount? The textView#Price example was for easy reference. The ID's of each only have Price as the commonfactor. Sorry for the inconvenience.
– randbo
Jan 1 at 21:07
I assume that you have textviews liketextView1Price
,textView2Price
,.. if the last one's id is textView1000Price, you just have to change the range to (1..1000
). If the ids have not such a similarity then this code won't work.
– forpas
Jan 1 at 21:10
i think i can make this work. ill just have to comment over all the entries so i can keep track of the values. example NewYorkPrice =. will be changed to textView1Price with a comment above stating New York. Thanks for your help i apprciate it.
– randbo
Jan 1 at 21:19
As a sidenote, I tend to usewith(tv) { when { text.startsWith() -> ... } }
for Kotlin
– shkschneider
Jan 2 at 13:57
Thanks for commenting. I am fairly new to this. Could you elaborate a little more? For the (1..9).forEach what if i have no determined amount? The textView#Price example was for easy reference. The ID's of each only have Price as the commonfactor. Sorry for the inconvenience.
– randbo
Jan 1 at 21:07
Thanks for commenting. I am fairly new to this. Could you elaborate a little more? For the (1..9).forEach what if i have no determined amount? The textView#Price example was for easy reference. The ID's of each only have Price as the commonfactor. Sorry for the inconvenience.
– randbo
Jan 1 at 21:07
I assume that you have textviews like
textView1Price
, textView2Price
,.. if the last one's id is textView1000Price, you just have to change the range to (1..1000
). If the ids have not such a similarity then this code won't work.– forpas
Jan 1 at 21:10
I assume that you have textviews like
textView1Price
, textView2Price
,.. if the last one's id is textView1000Price, you just have to change the range to (1..1000
). If the ids have not such a similarity then this code won't work.– forpas
Jan 1 at 21:10
i think i can make this work. ill just have to comment over all the entries so i can keep track of the values. example NewYorkPrice =. will be changed to textView1Price with a comment above stating New York. Thanks for your help i apprciate it.
– randbo
Jan 1 at 21:19
i think i can make this work. ill just have to comment over all the entries so i can keep track of the values. example NewYorkPrice =. will be changed to textView1Price with a comment above stating New York. Thanks for your help i apprciate it.
– randbo
Jan 1 at 21:19
As a sidenote, I tend to use
with(tv) { when { text.startsWith() -> ... } }
for Kotlin– shkschneider
Jan 2 at 13:57
As a sidenote, I tend to use
with(tv) { when { text.startsWith() -> ... } }
for Kotlin– shkschneider
Jan 2 at 13:57
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%2f53998118%2fhow-to-connect-declarations-and-if-statements%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