android anko,Announcing Anko for Android
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相关推荐
- Android 第三方库--2017年Android开源项目及库汇总
转自:http://blog.csdn.net/jsonnan/article/details/62215287 东西有点多,但是资源绝对nice,自己都全部亲身体验过了,大家可放心使用 github ...
- android 9 pie公司,谷歌Android 9 Pie,真正的安卓派
IT之家8月7日消息 今天谷歌正式宣布了Android 9 Pie正式版系统,此前的Android P终于定名为Android Pie,该更新首先面向谷歌Pixel设备和Essential Phone ...
- android控件触摸缩放,Android控件之ZoomControls缩放使用
先看一下效果 正常 缩小 放大 一.简介 ZoomControls是一组可缩放的控件.它包含俩个按钮(放大按钮.缩小按钮) 二.重要方法 hasFocus():判断焦点 hide():隐藏 onTou ...
- android平台 arcgisr_ArcGIS Runtime For Android 开发 (7)
第七课 图层管理 在前边的课程中,我们学习了如何加载图层,符号化图层,那么如果一个项目里边包含了很多的图层,但又需要进行查看特定的图层,怎么办?是的,我们需要对加载的图层进行管理. 那么如何来实现图层 ...
- android列表实现置顶,Android利用RecyclerView实现全选、置顶和拖拽功能示例
Android利用RecyclerView实现全选.置顶和拖拽功能示例 发布时间:2020-08-23 16:26:42 来源:脚本之家 阅读:159 作者:爱开发 前言 今天给大家分享是如何在Rec ...
- android用户界面设计:基本按钮,Android用戶界面設計:基本按鈕
本文向你展示了在你的android應用程序中創建一個簡單的Button或ImageButton控件的步驟.首先,你會學到如何向你的布局文件中添加按鈕控件.然後你會學習如何用兩種方法處理用戶對按鈕的點擊 ...
- android asynctask源码分析,Android通过Handler与AsyncTask两种方式动态更新ListView(附源码)...
本文实例讲述了Android通过Handler与AsyncTask两种方式动态更新ListView的方法.分享给大家供大家参考,具体如下: 有时候我们需要修改已经生成的列表,添加或者修改数据,noti ...
- android studio 库项目管理,在Android Studio中将现有项目转换为库项目
在模块的applicationId文件中(如果使用模块,则不是根项目!),只需替换: apply plugin: 'com.android.application' // or, if you're ...
- Android:你好,androidX!再见,android.support
190325 补充:莫名问题的解决 181106 补充:修改未迁移成功的三方库 1.AndroidX简介 点击查看Android文档中对androidx的简介 按照官方文档说明 androidx 是对 ...
最新文章
- 扩增子分析神器USEARCH简介
- 分享Kali Linux 2016.2第36周镜像虚拟机
- C#向C++编写的DLL传递字符串参数的办法
- 浅说动态生成Class实现MVC
- java ibm 2035,C# java 连接 IBM MQ时出现 2035 或 2013认证错误的解决方法
- LINQ to SQL
- JSP知识点大致介绍1
- python sphinx_Python Sphinx使用实例及问题解决
- python 简单的接口测试框架
- MYSQL误删数据恢复
- 三相pwm整流器。采用电压电流双闭环,SVPWM调制
- android dialog的格式显示,详解Android Dialog对话框的五种形式
- java+SpringBoot+HTML+Mysq基于微信小程序的大咖读书系统的设计与实现
- pcb小分享——PCB板组成部分有哪些?
- 有备而来! 解密DEVELOP德凡为何进入中国市场?
- 题外-解决mac重启后git无法使用
- 语言中的历史——汉语和突厥语的纠缠
- java中map参数封装到bean_JavaBean和Map转换封装类详解
- 详解modprobe的用法
- 广告营销DSP和DMP概念解释