# findViewById with variable

**URL:** <https://discuss.kotlinlang.org/t/findviewbyid-with-variable/26344>\
**Category:** Support\
**Created:** [February 6, 2023, 9:52am UTC](https://discuss.kotlinlang.org/t/findviewbyid-with-variable/26344 "2023-02-06T09:52:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbgood2023](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@jbgood2023](https://discuss.kotlinlang.org/u/jbgood2023)\
**Post date:** [February 6, 2023, 9:52am UTC](https://discuss.kotlinlang.org/t/findviewbyid-with-variable/26344/1 "2023-02-06T09:52:11Z")

</div>

Hi,  
i am very new at Android/Kotlin development and I have a very simple question.  
I try to use the “findViewById” expression in combination with a variable. x is a number and counted up.  
Here is my snippet:

```auto
var my_element = "dot"+x
round = findViewById(R.id.my_element)

```

But it doesnt work.  
How can I use a dynamic expression in the “findViewById”-function?  
Thank you!

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [February 6, 2023, 11:26am UTC](https://discuss.kotlinlang.org/t/findviewbyid-with-variable/26344/2 "2023-02-06T11:26:24Z")

</div>

> [@jbgood2023](#):
>
> How can I use a dynamic expression in the “findViewById”-function?

you can’t.

---

<div class="post-metadata">

**Author:** ![jbgood2023](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@jbgood2023](https://discuss.kotlinlang.org/u/jbgood2023)\
**Post date:** [February 6, 2023, 11:55am UTC](https://discuss.kotlinlang.org/t/findviewbyid-with-variable/26344/3 "2023-02-06T11:55:34Z")

</div>

Okay, good to know. Thank you.  
Is there any possibilty to use a workaround for my question?

Maybe to count the elements in my layout and choose, for example, the third one?

---

<div class="post-metadata">

**Author:** ![tlin47](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@tlin47](https://discuss.kotlinlang.org/u/tlin47)\
**Post date:** [February 6, 2023, 1:14pm UTC](https://discuss.kotlinlang.org/t/findviewbyid-with-variable/26344/5 "2023-02-06T13:14:24Z")

</div>

The parameter for `findViewById` is an int and R.id is a class that provides lots of int constants.

So, technically, it is possible to use (Java) reflection to reach the goal:

> <https://stackoverflow.com/questions/2685345/getting-value-of-public-static-final-field-property-of-a-class-in-java-via-refle>

There is even a method by Android for it:

> **[Resources  |  Android Developers](https://developer.android.com/reference/android/content/res/Resources#getIdentifier(java.lang.String,%20java.lang.String,%20java.lang.String))**

**However, both ways are discouraged since they are very inefficient.**

Therefore, it makes sense to take a second look at your specific case.  
E.g., in the special case that you have a fixed number of dot-views, you can use a simple custom array (defined once):

```auto
val dotIds = arrayOf(R.id.dot0, R.id.dot1, R.id.dot2)
...
round = findViewById(dotIds[x])

```

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [February 7, 2023, 12:10am UTC](https://discuss.kotlinlang.org/t/findviewbyid-with-variable/26344/6 "2023-02-07T00:10:43Z")

</div>

Check out [ViewGroup.getChildCount()](https://developer.android.com/reference/kotlin/android/view/ViewGroup?hl=en#getchildcount) and [ViewGroup.getChildAt()](https://developer.android.com/reference/kotlin/android/view/ViewGroup?hl=en#getchildat).
