1. 程式人生 > >Dialogs and alerts on Android using Anko and Kotlin

Dialogs and alerts on Android using Anko and Kotlin

Creating alerts and dialogs on Android is a relatively simple task thanks to the builder, but due to the verbosity of the language it usually becomes something quite illegible.

In Kotlin, by the mere fact of the lambdas, it simplifies us a little to understand what is happening in that code.

But thanks to

Anko, we can make all this even easier, and in this article we’ll see how.

Alerts in Kotlin with Anko

Writing alerts with Anko is pretty easy. Just create an alert block:

123 alert("Testing alerts"
){...}.show()

Within the block, you can specify some things like the title of the alert, or the buttons that you want to appear

12345 alert("Testing alerts"){title="Alert"yesButton{toast("Yess!!!")}noButton{}}.show()

This results in an alert like this:

Want to learn Kotlin?

Check my free guide to create your first project in 15 minutes!

alert-anko-kotlin

You can customize the actions by using the positiveButton, negativeButton and neutralButton methods:

123456 alert("Testing alerts"){title="Alert"positiveButton("Cool"){toast("Yess!!!")}negativeButton("Never Ever"){}neutralButton("I'll think about it")}.show()

alert-anko-kotlin-2

And even add a custom view that, of course, you can create also with Anko:

1234567891011 alert{title="Alert"positiveButton("Cool"){toast("Yess!!!")}customView{linearLayout{textView("I'm a text")button("I'm a button")padding=dip(16)}}}.show()

alert-anko-kotlin-3

Progress Dialogs

Another interesting feature that includes Anko is to create progress dialogs, and indeterminate progress.

To give an example of the second ones, you can create a progress dialog in this simple way:

1 indeterminateProgressDialog("This a progress dialog").show()

The result of the previous line will be this:

lert-anko-kotlin-4

Conclusion

You see that thanks to Anko and the power of Kotlin, you can create small DSLs to simplify tasks. Ideally you could create DSLs on any part of the framework so that it makes interaction with it much easier.

In a later article we’ll see how to deal with SQLite databases.

Meantime, why not take a look at the free guide to learn how to build your first project? or in the book where you can learn how to create a complete App from scratch.

I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.

Shares

Like this:

Like Loading...

相關推薦

Dialogs and alerts on Android using Anko and Kotlin

Creating alerts and dialogs on Android is a relatively simple task thanks to the builder, but due to the verbosity of the language it usually becomes

Databases on Android with Anko and Kotlin: SQLite never was so easy

Truth to be told: writing databases in Android is quite boring. All the boilerplate required to work with SQLite is far from being the most delightfu

disable NetworkManager and boot on static ip configuration using /etc/network/interfaces

sta ifdown isa ip link emctl lin mas 8.0 down 1.stop and disable NetworkManager sudo systemctl stop NetworkManager sudo systemctl disable

Android Developers Blog: Improving app security and performance on Google Play for years to come

Posted by Edward Cunningham, Product Manager, Android [Edit: Updated post on Dec 21 to clarify that when the 64-bit requirement is introduced in August 2

Some Thoughts on Android's new ConstraintLayout and Android Studio's new Design Editor

At this year's IO Google introduced a new layout - the ConstraintLayout - and also presented it's totally revamped la

In-app purchases on Android: new and upcoming

In-app purchases on Android: new and upcomingA closer look at the I/O 2018 announcementsGoogle I/O 2018 is just behind us, but it brought a lot of news whe

[PWA] Add Push Notifications to a PWA with React in Chrome and on Android

On Android and in Chrome (but not on iOS), it's possible to send push notifications with a PWA. We'll start by asking the user for permission to send them

Data Blocks: Hybrid OLTP and OLAP on Compressed Storage using both Vectorization and Compilation報告

This work aims at reducing the main-memory footprint in high performance hybrid OLTP & OLAP databases, whileretaining high query perfo

Microservices on AWS Compute Using Containers and Serverless

Deploying microservices-based applications can be complex. First, it requires setting up your basic compute, storage, and networking capa

Using Docker images and containers on your machine

Doug Tidwell is a Senior Software Engineer at IBM. His job as a content strategist for the developerWorks team is to figure out how to deliver as much usef

Using Android MediaCodec H264 and Display

Android MediaCode request API level 16 public class MyTextureView extends TextureView implements TextureView.SurfaceTextureListe

ims Registered and Volte enable on Android N

文前 程式碼基於mkm-mr1 android 7.1.2 本篇記錄了調查ims Registered狀態和VoLTE enable狀態的一些取值和更新的關鍵方法。 初衷是調查ims registered狀態和 VoLTE Available有沒

android adb shell and monkey 學習記錄

型號 orm -a reboot 觸摸事件 serial roc ear cpu Monkey環境: android SDK and JDK SDK目錄下的platform-tools和tools目錄要配置環境變量 查看版本: ADB 的安裝這裏就

Exceptions and Errors on iOS

har published sage tran pil span pointer enc content https://blog.jayway.com/2010/10/13/exceptions-and-errors-on-ios/ October 13, 2010 b

轉!!SQL左右連接中的on andon where的區別

生成 where條件 data- 一個 highlight article 根據 返回 地址 原博文地址:http://blog.csdn.net/xingzhemoluo/article/details/39677891 原先一直對SQL左右連接中的on and和on

Why does Delphi XE7 IDE hangs and fails on out of memory exception?

problem cycle soft addition des microsoft same hour bsp 引自: https://stackoverflow.com/questions/27701294/why-does-delphi-xe7-ide-ha

報異常:AnnotationAwareAspectJAutoProxyCreator is only available on Java 1.5 and higher

ssi cycle cati stp hierarchy worker nal a star sca 項目啟動時候報異常 1,異常信息 信息: Initializing Spring root WebApplicationContext2017-09-07 17:20:44

[TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5

tro wrong check this sta clas sudo ons assertion Typescript 2.5 adds JSDoc type assertion support for javascript file via ts-check servic

MYSQL 表左連接 ON ANDON WHERE 的區別

from col 臨時 根據 sel 生成 clas incr ima 首先是針對左右連接,這裏與inner join區分 在使用left join時,on and 和on where會有區別 1. on的條件是在連接生成臨時表時使用的條件,以左表為基準 ,不管on中的條

VVDI MB questions and answers on its capabilities

There are several question on Xhorse VVDI MB that asked by a beginner, and followed with answers and reviews. 1. Renew by IR only possible with BE