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

100+ errors when trying to use compileSdk 26:

val parent = activity.findViewById( 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(

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

This should also work.

val parent = activity.findViewById<ViewGroup>(

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

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

I’am try do this ,And replace all findViewById

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

        return findViewById<View>(id)

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


Using the replace string:

1 Like

Good to know, thanks!

An additional follw-up, I also had lines like

val view = findViewById(

I also ran this pattern