Android

Mobile

Announcing Anko for Android

We’re excited to announce a library we’ve been working on for Android development, which, among other things allows the creation of Application Interfaces in a type-safe and dynamic way using a DSL.

A Sample Taste

Here is a small example describing some of Anko’s possibilities. Imagine we need to create a simple sign-up form consisting of an EditText for a username and a “Sign up” Button. The code for this, using Anko would be:

import kotlinx.android.anko.*

class MainActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

verticalLayout {

padding = dip(16)

textView("Username:") {

textSize = 18f

}.layoutParams { verticalMargin = dip(4) }

val login = editText()

button("Sign up") {

textSize = 20f

onClick { login(login.text) }

}.layoutParams { topMargin = dip(8) }

}

}

}

Anko makes extensive use of Kotlin’s extension functions and properties arranged into type-safe builders to describe the user interface. In return, we get conciseness and type-safety at compile time.

Of course, we can also see a preview during design time using the Anko Preview plugin, available for both IntelliJ IDEA and Android Studio:

If we now want to add another text input widget, for instance an email, we could probably create another pair of textView() and editText() function calls. However, a nicer approach would be to extract the corresponding DSL fragment into a new function:

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

verticalLayout {

padding = dip(16)

val login = inputField("Username")

val email = inputField("E-mail")

button("Sign up") {

textSize = 20f

onClick { login(login.text) }

}.layoutParams { topMargin = dip(8) }

}

}

fun _LinearLayout.inputField(name: String): TextView {

textView("$name:") {

textSize = 18f

}.layoutParams { verticalMargin = dip(4) }

return editText()

}

Any additional inputs only require a single function call.

End result of the form would be:

Partially defined listeners

Anko is very helpful when you are using Android listeners with lots of methods. Consider the following code written that does not use Anko:

seekBar.setOnSeekBarChangeListener(object: OnSeekBarChangeListener {

override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {

// Something

}

override fun onStartTrackingTouch(seekBar: SeekBar?) {

// Just an empty method

}

override fun onStopTrackingTouch(seekBar: SeekBar) {

// Another empty method

}

})

Here’s the version using Anko:

seekBar {

onProgressChanged { (seekBar, progress, fromUser) ->

// Something

}

}

Methods that have empty bodies no longer require, well, empty implementations. Also, if setting onProgressChanged() and onStartTrackingTouch() for the same View, these two “partially defined” listeners will be merged.

More than a DSL

Anko is not just a DSL but a library which facilitates Android development in different areas. It has many methods covering dialogs, asynchronous tasks, services, intents and even SQLite database access.

For instance, if you want to start a new Activity:

// Without Anko

val intent = Intent(this, javaClass())

intent.putExtra("id", 5)

startActivity(intent)

// With Anko

startActivity("id" to 5)

Or activate vibrator:

// Without Anko

val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

vibrator.vibrate(500)

// With Anko

vibrator.vibrate(500)

Or even send a toast message:

// Without Anko

Toast.makeText(this, "Download is complete!", Toast.LENGTH_SHORT).show()

// With Anko

toast("Download is complete!")

Existing code support

You can keep your old classes written in Java. Moreover, if you still want (or have) to write a Kotlin activity class and inflate an XML layout for some reason, you can use View properties and listener helpers which would make things easier:

name.hint = "Enter your name"

name.onClick { /* do something */ }

Benefits of Anko

Hopefully you can see that Anko offers a series of benefits, in particular:

Everything is in one place. Instead of splitting layouts into static (XML) and dynamic parts and then trying to tie them together, we can just write everything we want using Kotlin. Compile-time type checking is a sweet bonus.

Anko can make our code more concise and readable.

It allows for easy re-use. We can just extract a part of the DSL into a function and use it multiple times.

Give it a try!

Anko is still in alpha stage but we want to release early to get your feedback, so please give it a try. We’ve made it as simple as possible to do so. It’s all published on Maven Central, and if you’re using Gradle, you can easily add the required dependencies to the build.gradle file:

dependencies {

compile 'org.jetbrains.anko:anko:0.5-15'

}

The Anko Preview plugin is available both for IntelliJ IDEA and Android Studio. You can download it directly from the Plugin Repository.

There are binaries targeting both raw Android (SDK version 15, Ice Cream Sandwich) and Android with a support-v4 package.

Also, last but not least, much like everything related to Kotlin, Anko is fully Open Source. The repository is on GitHub and as always, contributions are welcome!

Share

android anko,Announcing Anko for Android相关推荐

  1. Android 第三方库--2017年Android开源项目及库汇总

    转自:http://blog.csdn.net/jsonnan/article/details/62215287 东西有点多,但是资源绝对nice,自己都全部亲身体验过了,大家可放心使用 github ...

  2. android 9 pie公司,谷歌Android 9 Pie,真正的安卓派

    IT之家8月7日消息 今天谷歌正式宣布了Android 9 Pie正式版系统,此前的Android P终于定名为Android Pie,该更新首先面向谷歌Pixel设备和Essential Phone ...

  3. android控件触摸缩放,Android控件之ZoomControls缩放使用

    先看一下效果 正常 缩小 放大 一.简介 ZoomControls是一组可缩放的控件.它包含俩个按钮(放大按钮.缩小按钮) 二.重要方法 hasFocus():判断焦点 hide():隐藏 onTou ...

  4. android平台 arcgisr_ArcGIS Runtime For Android 开发 (7)

    第七课 图层管理 在前边的课程中,我们学习了如何加载图层,符号化图层,那么如果一个项目里边包含了很多的图层,但又需要进行查看特定的图层,怎么办?是的,我们需要对加载的图层进行管理. 那么如何来实现图层 ...

  5. android列表实现置顶,Android利用RecyclerView实现全选、置顶和拖拽功能示例

    Android利用RecyclerView实现全选.置顶和拖拽功能示例 发布时间:2020-08-23 16:26:42 来源:脚本之家 阅读:159 作者:爱开发 前言 今天给大家分享是如何在Rec ...

  6. android用户界面设计:基本按钮,Android用戶界面設計:基本按鈕

    本文向你展示了在你的android應用程序中創建一個簡單的Button或ImageButton控件的步驟.首先,你會學到如何向你的布局文件中添加按鈕控件.然後你會學習如何用兩種方法處理用戶對按鈕的點擊 ...

  7. android asynctask源码分析,Android通过Handler与AsyncTask两种方式动态更新ListView(附源码)...

    本文实例讲述了Android通过Handler与AsyncTask两种方式动态更新ListView的方法.分享给大家供大家参考,具体如下: 有时候我们需要修改已经生成的列表,添加或者修改数据,noti ...

  8. android studio 库项目管理,在Android Studio中将现有项目转换为库项目

    在模块的applicationId文件中(如果使用模块,则不是根项目!),只需替换: apply plugin: 'com.android.application' // or, if you're ...

  9. Android:你好,androidX!再见,android.support

    190325 补充:莫名问题的解决 181106 补充:修改未迁移成功的三方库 1.AndroidX简介 点击查看Android文档中对androidx的简介 按照官方文档说明 androidx 是对 ...

最新文章

  1. blktrace 工具集使用 及其实现原理
  2. Opencv 数学基础--范数(17)
  3. java ee domain作用_java EE应用概述
  4. Mysql数据库按时间点恢复实战
  5. Java黑皮书课后题第6章:6.10(使用isPrime方法)程序清单6-7提供了测试某个数字是否是素数的方法isPrime(int number)。使用这个方法求小于10000的素数的个数
  6. 使用mvn插件surefire 执行工程单元测试 出现OOM的解决分析
  7. 双胞胎被麻省理工全奖录取,他们成功的秘诀,跟你想的不一样……
  8. 1:1 人脸比对 开源_在开源周宣布青年:1月13日至17日
  9. Java应用基础微专业-设计篇
  10. setlocale 与 mbstowcs 的问题
  11. 关于我玩单片机学习路线(个人总结)
  12. 【自考】数据结构导论—二叉树计算题
  13. windows 下在 码市(coding.net) 上配置远程 git
  14. springboot启动 lombok 找不到符号
  15. 2019 码云 最流行的开源项目 TOP 50
  16. Bugzilla 使用教程
  17. 深入理解设计模式-抽象工厂模式
  18. User’s Guide
  19. 香港服务器跟国内服务器的区别!
  20. SAP ABAP PP常用数据库表

热门文章

  1. tftp服务器传送文件,3.64 tftp(传输文件)
  2. Laravel之Monolog全解析
  3. 系统重装git用户名密码忘了的恢复方法
  4. python怎么用numpy函数_Python numpy.ravel函数方法的使用
  5. SQL SERVER触发器中instead of的坑
  6. cgb2104-day16
  7. Android 8.1实现静默安装、卸载功能
  8. 好公司如何让员工体会公司文化和温度之个人理解
  9. jtl 转换html 乱码,解决 jmeter 非 GUI 模式运行测试,结果 jtl 文件没有响应数据及中文乱码...
  10. Pytorch中的Variable