# Kotlin-android-extensions dont work with custom view with generic

**URL:** <https://discuss.kotlinlang.org/t/kotlin-android-extensions-dont-work-with-custom-view-with-generic/14394>\
**Category:** Support\
**Created:** [September 24, 2019, 3:51am UTC](https://discuss.kotlinlang.org/t/kotlin-android-extensions-dont-work-with-custom-view-with-generic/14394 "2019-09-24T03:51:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kotlin-Awesome](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@Kotlin-Awesome](https://discuss.kotlinlang.org/u/Kotlin-Awesome)\
**Post date:** [September 24, 2019, 3:51am UTC](https://discuss.kotlinlang.org/t/kotlin-android-extensions-dont-work-with-custom-view-with-generic/14394/1 "2019-09-24T03:51:24Z")

</div>

I have a custom view like this

```
public class MyTextView<T> extends TextView{
    private List<T> mDatas;

    public MyTextView<T> setPages(List<T> datas){
        mDatas = datas;
        return this;
    }
}

```

when i use kotlin-android-extensions there is an error

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/d/d1a8bb8afb0af20274f4f4415e1392b256530eda.jpeg)

how could i solve the problem???

---

<div class="post-metadata">

**Author:** ![dominicJ](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dominicJ](https://discuss.kotlinlang.org/u/dominicJ)\
**Post date:** [September 24, 2019, 9:52am UTC](https://discuss.kotlinlang.org/t/kotlin-android-extensions-dont-work-with-custom-view-with-generic/14394/2 "2019-09-24T09:52:07Z")

</div>

How do you specify generic type? Please show xml file.

---

<div class="post-metadata">

**Author:** ![Kotlin-Awesome](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@Kotlin-Awesome](https://discuss.kotlinlang.org/u/Kotlin-Awesome)\
**Post date:** [September 24, 2019, 10:04am UTC](https://discuss.kotlinlang.org/t/kotlin-android-extensions-dont-work-with-custom-view-with-generic/14394/3 "2019-09-24T10:04:42Z")

</div>

i dont know how to specify generic type. so i cant solve the problem.  
i use it just like the common Textview.  
i think that the extensions plugin should support it  
like this  
import kotlinx.android.synthetic.main.\<layout\>.textview as TextView\<Int\>

---

<div class="post-metadata">

**Author:** ![dominicJ](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dominicJ](https://discuss.kotlinlang.org/u/dominicJ)\
**Post date:** [September 24, 2019, 12:36pm UTC](https://discuss.kotlinlang.org/t/kotlin-android-extensions-dont-work-with-custom-view-with-generic/14394/4 "2019-09-24T12:36:22Z")

</div>

If you just use `MyTextView` in xml layout, then it can’t deduce generic type argument `<T>`. It is the same in Java. The only solution is to extend `MyTextView<T>`, for example `IntTextView` which extends `MyTextView<Int>`, and use it in layout.

> <https://stackoverflow.com/questions/5008426/adding-a-custom-view-to-xml-but-with-a-generic-type/11315578#11315578>

I’m afraid it’s not Kotlin related in any way.

---

<div class="post-metadata">

**Author:** ![Kotlin-Awesome](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@Kotlin-Awesome](https://discuss.kotlinlang.org/u/Kotlin-Awesome)\
**Post date:** [September 27, 2019, 7:21am UTC](https://discuss.kotlinlang.org/t/kotlin-android-extensions-dont-work-with-custom-view-with-generic/14394/5 "2019-09-27T07:21:13Z")

</div>

if i dont use Kotlin-android-extensions,  
i use MyTextView just like this :  
var textview : MyTextView = findViewById(R.id.textView)  
i dont need to extend MyTextView . so i think Kotlin-android-extensions should support some feature to solve the problem.

---

<div class="post-metadata">

**Author:** ![dominicJ](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dominicJ](https://discuss.kotlinlang.org/u/dominicJ)\
**Post date:** [September 27, 2019, 8:03am UTC](https://discuss.kotlinlang.org/t/kotlin-android-extensions-dont-work-with-custom-view-with-generic/14394/6 "2019-09-27T08:03:10Z")

</div>

Again, you can’t use MyTextView directly in layout because it is generic class. Android will make it `MyTextView<Nothing>` because you can’t specify generic type in xml layout. And that’s why you can’t call your `setPages` with `Int` list, because T is deduced as `Nothing`, as you didn’t provide that information in layout.  
It’s not related to Kotlin Android Extensions at all. It’s just how Android handles generics in xml layouts.

---

<div class="post-metadata">

**Author:** ![Kotlin-Awesome](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@Kotlin-Awesome](https://discuss.kotlinlang.org/u/Kotlin-Awesome)\
**Post date:** [September 27, 2019, 8:15am UTC](https://discuss.kotlinlang.org/t/kotlin-android-extensions-dont-work-with-custom-view-with-generic/14394/7 "2019-09-27T08:15:51Z")

</div>

if i use Kotlin-android-extensions, i can’t use MyTextView directly in layout .  
if i dont use Kotlin-android-extensions, i can use MyTextView directly in layout .

> <https://stackoverflow.com/questions/5008426/adding-a-custom-view-to-xml-but-with-a-generic-type/11315578#11315578>

You will find other solutions about this question in this page.

```auto
CustomViewFlipper<TextView> customFlipper = 
            (CustomViewFlipper<TextView>) findViewById(R.id.customFlipper);

```

i can cast it to the correct generic type.  
i my code, i use this solution to solve the problem.

now i just want to use Kotlin-android-extensions and don’t want to use extends to solve thie problem.
