Un nouveau langage de programmation écrit par l’éditeur d’Android STUDIO (IntelliJ IDEA)
1er tuto : https://codelabs.developers.google.com/codelabs/build-your-first-android-app-kotlin/#0
et https://kotlinlang.org/docs/tutorials/kotlin-android.html
Et : https://try.kotlinlang.org
Sqlite en Kotlin : https://tutorial.eyehunts.com/android/sqlite-database-in-android-kotlin-example/
Exemple de mise en oeuvre avec des facts aléatoires :
Cet exemple met en oeuvre les JSON Web Services et la OkHttp 3 library.
The facts will be got from the Internet Chuck Norris Database which offers a simple API to get random Chuck Norris Facts :
http://api.icndb.com/jokes/random
L’exemple de mise en oeuvre est ici : https://tinyurl.com/y2hrbdjg
A) Apprentissage :
https://www.programiz.com/kotlin-programming
Il n’est pas nécessaire de créer une classe en Kotlin.
- Variables :
La déclaration de variables se fait avec Var ou Val (constante, valeur non interchangeable) :
var language: String // variable declaration of type String ... .. ... language = "French" // variable initialization val score: Int // variable declaration of type Int ... .. ... score = 95 // variable initialization
Pour connaître le type de variable : placez le curseur sur la variable et appuyez sur Ctrl + Shift + P
Pour les nombres, il y a 6 types :
- Byte : de -128 à 127
- Short : de -32 768 à 32 767
- Int : de
-231
à231-1
- Long : de
-263
à263-1
- Float : comme Int mais avec une virgule
- Double : comme Long mais avec une virgule
Autres types : Char (caracteres) ; Strings ; Boolean ; Arrays
Il est aussi possible de tester dans un programme le type de données :
when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) }
2. Opérateurs :
https://www.programiz.com/kotlin-programming/operators
+ ; – ; / ; %
Concatenation de chaines :
fun main(args: Array<String>) {
val start = "Talk is cheap. "
val middle = "Show me the code. "
val end = "- Linus Torvalds"
val result = start + middle + end
println(result)
}
Les autres opérateurs on le même fonctionnement qu’en Java. L’instruction When s’utilise à la place du Switch Java,
fun main(args: Array<String>) {
val a = 12
val b = 5
println("Enter operator either +, -, * or /")
val operator = readLine()
val result = when (operator) {
"+" -> a + b
"-" -> a - b
"*" -> a * b
"/" -> a / b
else -> "$operator operator is invalid operator."
}
println("result = $result")
}
Autre exemple :
fun main(args: Array<String>) {
val n = -1
when (n) {
1, 2, 3 -> println("n is a positive integer less than 4.")
0 -> println("n is zero")
-1, -2 -> println("n is a negative integer greater than 3.")
}
}
fun main(args: Array<String>) {
val a = 100
when (a) {
in 1..10 -> println("A positive number less than 11.")
in 10..100 -> println("A positive number between 10 and 100 (inclusive)")
}
}
Ou en fonction du contenu d’une variable :
fun main(args: Array<String>) {
val a = 11
val n = "11"
when (n) {
"cat" -> println("Cat? Really?")
12.toString() -> println("Close but not close enough.")
a.toString() -> println("Bingo! It's eleven.")
}
}
3. Fonctions :
https://www.programiz.com/kotlin-programming/functions
Déclaration avec typage :
Appel :
fun square(number: Int) = number * number
4. Programmation orientée objets :
https://www.programiz.com/kotlin-programming/class-objects
5. Utilisation des layouts :
Pour inscrire une valeur dans un textView suite à un click
val textView: TextView = findViewById(R.id.android_text) as TextView
textView.setOnClickListener {
textView.text = getString(R.string.name)
}
Pour lire la valeur d’un textView Textview
:
val str: String = textView.text.toString()
println("the value is $str")
B) Exemples :
https://android.jlelse.eu/create-an-android-sound-recorder-using-kotlin-36902b3bf967
https://www.programiz.com/kotlin-programming/examples
http://gamecodeschool.com/kotlin/coding-a-space-invaders-game-in-kotlin/
https://www.tutorialkart.com/kotlin-android/get-started-with-android-game-development/
https://github.com/janbodnar/Kotlin-Snake-Game
https://kotlin.christmas/2019/11
https://reimerm.de/2016/10/introduction-to-mobile-game-development-using-libgdx-kotlin-1/