goodbye findViewById! Hello Kotlin!

in #kotlin6 years ago (edited)

kotlin_250x250.png Here comes Kotlin

Hey folks
ever written an android app? I am... and it is annoying to always initialize your UI elements before using them...
you design your layout, here is the XML:

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/SucheFuer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

then you use it:

...
public void onCreate(View view){
    ..
   TextView textView = findViewById(R.id.textView);
   textView.setText("new Text");
...
}

and so on.. BOOOOOORING!
here we go with Kotlin - JBrains JVM Language, that is officially supported by Android right now!
not only can you write cleaner, shorter code, you can also use some convinience methods out of the box

Image having the same XML like above, but in Kotlin you just do this:

fun onCreate(view: View){
    textView.text = "new Text"
}

uh! I like! I really do!
Kotlin comes closer to my heart with every day - what do you think of it?

Sort:  

I ve got a little java background as amateur and I discovered Kotlin a year ago. It's not a revolution, but I think it brings a little freshness compared to java. It's a pity that it is mostly used for android and almost not for the backend.

in the backend you can just use it...
there are maven and gradle plugins, afaik
I just mixed it in android, too - works smooth!

What do you mean by "just use it" ?
I used for backend but the community is much bigger for android (so tuto, ... also).

oh just found this: https://www.meetup.com/de-DE/Kotlin-User-Group-Munich/events/253612919/#
a meetup in Munich, where they talk about Kotlin for the backend - I will go there and tell, what I learned

Cool, I will wait your feedback.

yeah, tutorials... I mean, you can mix java and kotlin
write in whatever you want ... I think, as soon as you have enough experience, you can always use one for the other.
Just copy/paste from stackoverflow won't work that smooth :)