# findViewByID genericized return in API 26: how are YOU coping with it?

**URL:** <https://discuss.kotlinlang.org/t/findviewbyid-genericized-return-in-api-26-how-are-you-coping-with-it/4342>\
**Category:** Android\
**Created:** [August 25, 2017, 9:49pm UTC](https://discuss.kotlinlang.org/t/findviewbyid-genericized-return-in-api-26-how-are-you-coping-with-it/4342 "2017-08-25T21:49:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![harrypotter](https://avatars.discourse-cdn.com/v4/letter/h/ee7513/32.png) [@harrypotter](https://discuss.kotlinlang.org/u/harrypotter)\
**Post date:** [August 25, 2017, 9:49pm UTC](https://discuss.kotlinlang.org/t/findviewbyid-genericized-return-in-api-26-how-are-you-coping-with-it/4342/1 "2017-08-25T21:49:38Z")

</div>

100+ errors when trying to use compileSdk 26:

` val parent = activity.findViewById(android.R.id.content) as ViewGroup`

Of course, if I had typed the declarations, they would all work. But as a good Kotlin developer, I did not.

` val parent: ViewGroup = activity.findViewById(android.R.id.content)`

I can only image the chagrin a team with a very large Kotlin Android codebase would face 😑 Has anyone come up with a regular expression, or shell script, anything that can automate this? I am a little rusty with that!

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [August 26, 2017, 8:54am UTC](https://discuss.kotlinlang.org/t/findviewbyid-genericized-return-in-api-26-how-are-you-coping-with-it/4342/2 "2017-08-26T08:54:33Z")

</div>

This should also work.

```
val parent = activity.findViewById<ViewGroup>(android.R.id.content)

```

IntelliJ’s structural search and replace would be helpful for converting

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [August 28, 2017, 9:42am UTC](https://discuss.kotlinlang.org/t/findviewbyid-genericized-return-in-api-26-how-are-you-coping-with-it/4342/3 "2017-08-28T09:42:09Z")

</div>

Structural search & replace is not supported for Kotlin, so no, it would not help.

---

<div class="post-metadata">

**Author:** ![sealwu](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@sealwu](https://discuss.kotlinlang.org/u/sealwu)\
**Post date:** [August 28, 2017, 10:28am UTC](https://discuss.kotlinlang.org/t/findviewbyid-genericized-return-in-api-26-how-are-you-coping-with-it/4342/4 "2017-08-28T10:28:25Z")

</div>

I’am try do this ,And replace all findViewById

```auto
    fun View.findViewByIdOld(id: Int) :View{

        return findViewById<View>(id)
    }

```

---

<div class="post-metadata">

**Author:** ![roschlau](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@roschlau](https://discuss.kotlinlang.org/u/roschlau)\
**Post date:** [August 29, 2017, 10:30am UTC](https://discuss.kotlinlang.org/t/findviewbyid-genericized-return-in-api-26-how-are-you-coping-with-it/4342/5 "2017-08-29T10:30:12Z")

</div>

You should be able to “search and replace in path” this Regex to catch most occurrences:

```
((?:\w+\.)*findViewById)\(((?:\w+\.)*\w+)\)\s+as\s+(\w+)

```

Using the replace string:

```
$1<$3>($2)

```

---

<div class="post-metadata">

**Author:** ![harrypotter](https://avatars.discourse-cdn.com/v4/letter/h/ee7513/32.png) [@harrypotter](https://discuss.kotlinlang.org/u/harrypotter)\
**Post date:** [August 29, 2017, 10:33pm UTC](https://discuss.kotlinlang.org/t/findviewbyid-genericized-return-in-api-26-how-are-you-coping-with-it/4342/6 "2017-08-29T22:33:56Z")

</div>

Good to know, thanks!

---

<div class="post-metadata">

**Author:** ![harrypotter](https://avatars.discourse-cdn.com/v4/letter/h/ee7513/32.png) [@harrypotter](https://discuss.kotlinlang.org/u/harrypotter)\
**Post date:** [September 16, 2017, 11:28pm UTC](https://discuss.kotlinlang.org/t/findviewbyid-genericized-return-in-api-26-how-are-you-coping-with-it/4342/7 "2017-09-16T23:28:16Z")

</div>

An additional follw-up, I also had lines like

`val view = findViewById(R.id.something)`

I also ran this pattern

```
((?:\w+\.)*findViewById)\(((?:\w+\.)*\w+)\)
$1<View>($2)

```

THanks
