├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── values │ │ │ │ ├── colors.xml │ │ │ │ ├── styles.xml │ │ │ │ └── strings.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── menu │ │ │ │ └── main.xml │ │ │ ├── layout │ │ │ │ ├── activity_welcome.xml │ │ │ │ └── activity_main.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── oreilly │ │ │ │ └── hellokotlin │ │ │ │ ├── db │ │ │ │ ├── User.kt │ │ │ │ ├── UserRepository.kt │ │ │ │ ├── UserDAO.kt │ │ │ │ └── UserDatabase.kt │ │ │ │ ├── astro │ │ │ │ ├── AstroRequest.kt │ │ │ │ ├── AstroResponseClasses.kt │ │ │ │ └── AstroService.kt │ │ │ │ ├── MainActivity.kt │ │ │ │ ├── ui │ │ │ │ └── WelcomeViewModel.kt │ │ │ │ └── WelcomeActivity.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── oreilly │ │ │ └── hellokotlin │ │ │ ├── ExampleUnitTest.kt │ │ │ └── astro │ │ │ └── AstroRequestTest.kt │ └── androidTest │ │ └── java │ │ └── com │ │ └── oreilly │ │ └── hellokotlin │ │ ├── ExampleInstrumentedTest.kt │ │ ├── EspressoTest.kt │ │ └── db │ │ └── UserRepositoryTest.kt ├── proguard-rules.pro └── build.gradle ├── Kotlin_for_Android.pdf ├── gradle ├── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties └── libs.versions.toml ├── .gitignore ├── settings.gradle ├── README.md ├── gradle.properties ├── gradlew.bat ├── gradlew └── LICENSE /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /Kotlin_for_Android.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/Kotlin_for_Android.pdf -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kousen/HelloKotlinAndroid/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | .DS_Store 9 | /build 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Nov 30 17:33:34 EST 2022 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | gradlePluginPortal() 4 | google() 5 | mavenCentral() 6 | } 7 | } 8 | 9 | dependencyResolutionManagement { 10 | repositories { 11 | google() 12 | mavenCentral() 13 | } 14 | } 15 | 16 | include ':app' 17 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/java/com/oreilly/hellokotlin/db/User.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin.db 2 | 3 | import androidx.room.* 4 | 5 | @Entity(tableName = "users") 6 | data class User( 7 | @ColumnInfo(name = "name") 8 | val name: String, 9 | 10 | @PrimaryKey(autoGenerate = true) // last, so can omit it when instantiating 11 | var _id: Long = 0L 12 | ) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HelloKotlinAndroid 2 | Basic "Hello, World!" Android app written in Kotlin 3 | 4 | Used for seminars on writing Android apps in Kotlin. 5 | 6 | Includes: 7 | 8 | * Uses view binding for layouts 9 | * Room library and coroutines for async access to db and JSON service 10 | * Gson for converting JSON data to Kotlin data classes 11 | 12 | Ken Kousen, ken.kousen@kousenit.com 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/oreilly/hellokotlin/astro/AstroRequest.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin.astro 2 | 3 | import com.google.gson.Gson 4 | import java.net.URL 5 | 6 | class AstroRequest { 7 | fun execute(): AstroResult = 8 | Gson().fromJson( 9 | URL("http://api.open-notify.org/astros.json").readText(), 10 | AstroResult::class.java 11 | ) 12 | } -------------------------------------------------------------------------------- /app/src/main/java/com/oreilly/hellokotlin/astro/AstroResponseClasses.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin.astro 2 | 3 | import kotlinx.serialization.Serializable 4 | 5 | @Serializable 6 | data class AstroResult( 7 | val message: String, 8 | val number: Int, 9 | val people: List 10 | ) 11 | 12 | @Serializable 13 | data class Assignment( 14 | val name: String, 15 | val craft: String, 16 | ) -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/oreilly/hellokotlin/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/oreilly/hellokotlin/db/UserRepository.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin.db 2 | 3 | import androidx.annotation.WorkerThread 4 | import kotlinx.coroutines.flow.Flow 5 | 6 | class UserRepository(private val userDao: UserDAO) { 7 | val allUsers: Flow> = userDao.getAllUsers() 8 | 9 | @WorkerThread 10 | suspend fun insertUser(name: String) { 11 | if (userDao.count(name) == 0) { 12 | userDao.insertUsers(User(name)) 13 | } 14 | } 15 | 16 | suspend fun deleteUsersByName(name: String) = 17 | userDao.findByName(name).forEach { userDao.delete(it) } 18 | 19 | suspend fun deleteAllUsers() = 20 | userDao.deleteAll() 21 | } -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | HelloKotlin 3 | Please enter your name 4 | Say Hello 5 | Hello, %s! 6 | Say Hi 7 | There are %s people in space 8 | Get Astronauts 9 | About Hello, Kotlin 10 | Clear Database 11 | Hello World! 12 | Go to Stack Overflow 13 | Google Headquaters 14 | 15 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | android.useAndroidX=true -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/oreilly/hellokotlin/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin 2 | 3 | import android.content.Context 4 | import androidx.test.core.app.ApplicationProvider 5 | import androidx.test.ext.junit.runners.AndroidJUnit4 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = ApplicationProvider.getApplicationContext() 22 | assertEquals("com.oreilly.hellokotlin", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 9 | 14 | 17 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/oreilly/hellokotlin/db/UserDAO.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin.db 2 | 3 | import androidx.room.* 4 | import kotlinx.coroutines.flow.Flow 5 | 6 | @Dao 7 | interface UserDAO { 8 | @Query("select * from users order by name asc") 9 | fun getAllUsers(): Flow> 10 | 11 | @Query("select * from users where _id = :key") 12 | fun get(key: Long): User? 13 | 14 | @Query("select count(*) from users where name = :name") 15 | suspend fun count(name: String): Int 16 | 17 | @Query("select * from users where name = :name") 18 | fun findByName(name: String): List 19 | 20 | @Insert(onConflict = OnConflictStrategy.IGNORE) 21 | suspend fun insertUsers(vararg users: User) 22 | 23 | @Update 24 | suspend fun update(user: User) 25 | 26 | @Delete 27 | suspend fun delete(user: User) 28 | 29 | @Delete 30 | suspend fun deleteAll(vararg users: User) 31 | } -------------------------------------------------------------------------------- /app/src/main/java/com/oreilly/hellokotlin/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import android.view.View 6 | import androidx.appcompat.app.AppCompatActivity 7 | import com.oreilly.hellokotlin.databinding.ActivityMainBinding 8 | 9 | class MainActivity : AppCompatActivity() { 10 | 11 | private lateinit var binding: ActivityMainBinding 12 | 13 | override fun onCreate(savedInstanceState: Bundle?) { 14 | super.onCreate(savedInstanceState) 15 | 16 | binding = ActivityMainBinding.inflate(layoutInflater) 17 | setContentView(binding.root) 18 | 19 | binding.helloButton.setOnClickListener(this::sayHello) 20 | } 21 | 22 | @Suppress("UNUSED_PARAMETER") 23 | fun sayHello(v: View?) { 24 | Intent(this, WelcomeActivity::class.java).apply { 25 | putExtra(Intent.EXTRA_TEXT, binding.editText.text.toString()) 26 | startActivity(this) 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/oreilly/hellokotlin/astro/AstroService.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin.astro 2 | 3 | import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory 4 | import kotlinx.serialization.ExperimentalSerializationApi 5 | import kotlinx.serialization.json.Json 6 | import okhttp3.MediaType.Companion.toMediaType 7 | import retrofit2.Retrofit 8 | import retrofit2.http.GET 9 | 10 | private const val BASE_URL = "http://api.open-notify.org/" 11 | 12 | @OptIn(ExperimentalSerializationApi::class) 13 | private val retrofit = Retrofit.Builder() 14 | //.addConverterFactory(GsonConverterFactory.create()) 15 | .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) 16 | .baseUrl(BASE_URL) 17 | .build() 18 | 19 | interface AstroService { 20 | @GET("astros.json") 21 | suspend fun getAstroResult(): AstroResult 22 | } 23 | 24 | object AstroApi { 25 | // property delegate called "lazy" 26 | val retrofitService: AstroService by lazy { 27 | retrofit.create(AstroService::class.java) 28 | } 29 | } -------------------------------------------------------------------------------- /app/src/main/java/com/oreilly/hellokotlin/db/UserDatabase.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin.db 2 | 3 | import android.content.Context 4 | import androidx.room.Database 5 | import androidx.room.Room 6 | import androidx.room.RoomDatabase 7 | 8 | @Database(entities = [User::class], version = 1, exportSchema = false) 9 | abstract class UserDatabase : RoomDatabase() { 10 | abstract val userDao: UserDAO 11 | 12 | companion object { 13 | @Volatile 14 | private var instance: UserDatabase? = null 15 | 16 | fun getInstance(context: Context): UserDatabase { 17 | return instance ?: synchronized(this) { 18 | instance ?: buildDatabase(context) 19 | .also { instance = it } 20 | } 21 | } 22 | 23 | private fun buildDatabase(context: Context) = 24 | Room.databaseBuilder( 25 | context.applicationContext, 26 | UserDatabase::class.java, 27 | "user_database") 28 | .fallbackToDestructiveMigration() 29 | .build() 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/test/java/com/oreilly/hellokotlin/astro/AstroRequestTest.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin.astro 2 | 3 | import kotlinx.coroutines.runBlocking 4 | import org.junit.Assert.* 5 | import org.junit.Test 6 | import java.net.URL 7 | 8 | class AstroRequestTest { 9 | 10 | @Test 11 | fun `Access astronaut data`() { 12 | println(URL("http://api.open-notify.org/astros.json").readText()) 13 | } 14 | 15 | @Test 16 | fun execute() { 17 | val result = AstroRequest().execute() 18 | println(result) 19 | 20 | assertEquals("success", result.message) 21 | assertTrue(result.number >= 0) 22 | assertEquals(result.number, result.people.size) 23 | } 24 | 25 | @Test 26 | fun `Use retrofit to get astro data`() = runBlocking { 27 | AstroApi.retrofitService.getAstroResult().run { 28 | println("There are $number people in space") 29 | for ((name,craft) in people) { 30 | println("$name aboard $craft") 31 | } 32 | assertTrue(number >= 0) 33 | assertEquals(number, people.size) 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_welcome.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 21 | 22 | 26 | 27 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/oreilly/hellokotlin/EspressoTest.kt: -------------------------------------------------------------------------------- 1 | package com.oreilly.hellokotlin 2 | 3 | import androidx.test.espresso.Espresso.onView 4 | import androidx.test.espresso.action.ViewActions.click 5 | import androidx.test.espresso.action.ViewActions.typeText 6 | import androidx.test.espresso.assertion.ViewAssertions.matches 7 | import androidx.test.espresso.matcher.ViewMatchers.withId 8 | import androidx.test.espresso.matcher.ViewMatchers.withText 9 | import androidx.test.ext.junit.rules.activityScenarioRule 10 | import androidx.test.ext.junit.runners.AndroidJUnit4 11 | import org.junit.Rule 12 | import org.junit.Test 13 | import org.junit.runner.RunWith 14 | 15 | @RunWith(AndroidJUnit4::class) 16 | class EspressoTest { 17 | 18 | // Alternative is @JvmField and @Rule instead of annotating getter 19 | @get:Rule 20 | val activityRule = activityScenarioRule() 21 | 22 | @Test 23 | fun nameUpdatesTextView() { 24 | onView(withId(R.id.edit_text)) 25 | .perform(typeText("Dolly")) 26 | 27 | onView(withId(R.id.hello_button)) 28 | .perform(click()) 29 | 30 | onView(withId(R.id.welcome_text)) 31 | .check(matches(withText("Hello, Dolly!"))) 32 | } 33 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | 24 | 25 |