When viewing this video, I saw an interesting thing that the UI is not created in XML, but in the code. Googling, I realized that this is done using the Anko library and decided to try to do something. Result:

 class UI(launch: Activity) { init { launch.setContentView( launch.UI { verticalLayout { var cc = 0 button("Click $cc") { onClick { text = "Click ${++cc}" } }.lparams{ width = dip(200) height = dip(75) } } }.view) } } 

Looking at my creation, I thought that it was possible to somehow improve the code, but I don’t understand how to do it now, so I ask the experts on this issue.

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants of D-side , aleksandr barakin , Streletz , Grundy , sercxjo May 31 '16 at 12:40 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What exactly are you going to improve? In general, Anko is the same layout in xml, only in a different format - there you write in declarative language, here in the form of code - the result is the same and the possibilities are equal. - pavlofff
  • I just tried to write UI for the first time on Kotline and I wonder how correctly I did it? - Bleser

1 answer 1

Anko is the same layout in xml, only in a different format - there you write in a declarative language, here in the form of a code - the result is the same and the possibilities are equal.

You can continue to use the classic Android xml-layout and when writing programs on Kotlin, Anko is only an alternative tool, and not a prerequisite for developing a UI on Kotlin.

Personally, I think that the UI should be in a declarative language (XML). First, it divides business logic and design into separate entities. Then the xml-layout can be pre-evaluated in the visual editor and all the attributes of the widgets in the form of a table are available there and in general it is much more comfortable to work with it than with the code.

As for how correctly - do the same thing as in the xml-layout, just write in a different format, that's all correct.

As for binding, there is such official support for binding in XML: Android Data Binding , the tool is perhaps more powerful than Anko, although I, to be honest, didn’t understand Anko very much, I don’t like such concepts (UI in code)