Use Anko and Kotlin to implement the dialog box and warning prompt (KAD 24) on Android and ankokotlin
By Antonio Leiva
Time: Mar 9 and 2017
Link: https://antonioleiva.com/dialogs-android-anko-kotlin/
Using Builder, creating warning prompts and dialog boxes in Android is relatively simple. However, due to the complicated language, some prompts are usually vague.
In Kotlin, only Lambda expressions can help us understand how code is handled.
However, with Anko, we can make it easier. In this article, we can see what is going on.
Use Anko for warning in Kotlin
It is very easy to write warning prompts through Anko. You only need to create an alert code block:
1 alert("Testing alerts") {2 ...3 }.show()
In this Code block, you can specify some things, such as the title of the prompt or the button you want to display:
1 alert("Testing alerts") {2 title("Alert")3 yesButton { toast("Yess!!!") }4 noButton { }5 }.show()
The result of this Code is as follows:
You can also usepositiveButton
,negativeButton
AndneutralButton
Method customization:
1 alert("Testing alerts") {2 title("Alert")3 positiveButton("Cool") { toast("Yess!!!") }4 negativeButton("Never Ever") { }5 neutralButton("I'll think about it")6 }.show()
Of course, you can even customize the view through Anko plus:
1 alert { 2 title("Alert") 3 positiveButton("Cool") { toast("Yess!!!") } 4 customView { 5 linearLayout { 6 textView("I'm a text") 7 button("I'm a button") 8 padding = dip(16) 9 }10 }11 }.show()
Progress dialog box
Anko has another feature. You can create a progress dialog box and uncertain the progress.
Here is the second example. You can use this simple method to create a progress dialog box:
1 indeterminateProgressDialog("This a progress dialog").show()
The result is as follows:
Conclusion
With Anko and Kotlin, creating a small DSL is simple. Ideally, you can create a DSL in any part of the framework, so it makes interaction very easy.
In a later article, we will explain how to use SQLite to process databases.
Now, should you read the free User Guide to learn how to build your first project? Or read a book and learn how to create a complete application from scratch?