# Property getters & setters in custom view

**URL:** <https://discuss.kotlinlang.org/t/property-getters-setters-in-custom-view/2300>\
**Category:** Android\
**Created:** [February 21, 2017, 2:57pm UTC](https://discuss.kotlinlang.org/t/property-getters-setters-in-custom-view/2300 "2017-02-21T14:57:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayo](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@mayo](https://discuss.kotlinlang.org/u/mayo)\
**Post date:** [February 21, 2017, 2:57pm UTC](https://discuss.kotlinlang.org/t/property-getters-setters-in-custom-view/2300/1 "2017-02-21T14:57:25Z")

</div>

Hello Kotlin community,

recently i switched to kotlin from java in android development.  
Everything works great so far, i have just one issue.

When i’m creating custom views and i define custom attributes, as android sdk says, i have to define getters and setters for that attributes.

For example if i have custom attribute with type String (app:anotherString=“string”)

```
public String getAnotherString() { 
     return mAnotherString;
}
 public void setAnotherString(String anotherString) { 
     this.mAnotherString = anotherString;
     ...
}

```

So i thought, that this can be replaced with property getters and setters like this :

```
var mAnotherString: String ? = null
      set(value) {
      ...
      }

```

But it just doesn’t work. Can someone explain me why?

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [February 21, 2017, 4:19pm UTC](https://discuss.kotlinlang.org/t/property-getters-setters-in-custom-view/2300/2 "2017-02-21T16:19:52Z")

</div>

What is the error?

If you wants define property “anotherString”:

```
var anotherString: String = "string"

```

---

<div class="post-metadata">

**Author:** ![mayo](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@mayo](https://discuss.kotlinlang.org/u/mayo)\
**Post date:** [February 22, 2017, 8:38am UTC](https://discuss.kotlinlang.org/t/property-getters-setters-in-custom-view/2300/3 "2017-02-22T08:38:28Z")

</div>

There is no error, but as i said, i’m trying to implement my own **custom attributes** declared in attrs.xml.  
This is a first step :

```
<declare-styleable name="MyCustomView">
    <attr name="anotherString" format="string" />
</declare-styleable>

```

next step is :

> To provide dynamic behavior, expose a property getter and setter pair for each custom attribute.

as it is described here:

> **[Create a view class  |  Android Developers](https://developer.android.com/develop/ui/views/layout/custom-views/create-view)**
>
> A well-designed custom view is much like any other well-designed class. It encapsulates a specific set of functionality with an easy-to-use interface, it uses CPU and memory efficiently, and so forth. In addition to being a well-designed class,...

Problem is that it just does not work with property getters and setters in kotlin style, i need to define it with “java way” as i posted above.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [February 22, 2017, 9:11am UTC](https://discuss.kotlinlang.org/t/property-getters-setters-in-custom-view/2300/4 "2017-02-22T09:11:05Z")

</div>

Copy the example code

```
public class JTest {
    private boolean mShowText;

    public boolean getShowText() {
        return mShowText;
    }

    public void setShowText(boolean showText) {
        mShowText = showText;
        invalidate();
        requestLayout();
    }
}

```

in a Kotlin file, the IDE should translate it to:

```
class JTest {
    var showText: Boolean = false
        set(showText) {
            field = showText
            invalidate()
            requestLayout()
        }
}

```

I hope this helps you.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [February 22, 2017, 10:33am UTC](https://discuss.kotlinlang.org/t/property-getters-setters-in-custom-view/2300/5 "2017-02-22T10:33:59Z")

</div>

I forgot, the Kotlin way is:

```
var showText: Boolean by Delegates.observable(false) {
    prop, old, new ->
        invalidate()
        requestLayout()
    }

```

[https://kotlinlang.org/docs/reference/delegated-properties.html#observable](https://kotlinlang.org/docs/reference/delegated-properties.html#observable)
