Simple Android Widget written in Kotlin in AndroidStudio for Multi-Device Testers to indicate Android Version and Device Name. This README will show step-by-step implementation following Official Kotlin Tutorials with proper references for anyone interested in Kotlin
-
In AndroidStudio, Go to File > Settings > Plugins > Install JetBrains plugin… and Search Kotlin
-
Install Plugin and Restart AndroidStudio
-
Go File > New > New Project and create new Java Android Project
- Minimum SDK Version 21 for Material Design => Now it's 14
- No AppCompat to keep this project as simple as possible
Open MainActivity.java, and follow steps below:
-
Go to Help > Find Action
⇧⌘A
and select Convert Java File to Kotlin File⌥⇧⌘K
-
This will try to convert MainActivity.java to MainActivity.kt
-
By adding .kt file, AndroidStudio will trigger Configure Kotlin in Project Action as well and will automatically generate code for Kotlin configuration in AndroidStudio
You can change Kotlin Version by editing ext.kotlin_version
in $PROJECT_ROOT/build.gradle
3. Simple Dashboard in Activity using Kotlin Android Extensions 04695ae
Since building an Android Widget directly in Kotlin is a bit overdo as a start, in-Activity implementation will be a good bridge for adaptation to Kotlin. View inflation and binding will be supported through Kotlin Android Extensions.
- Configure Kotlin Android Extensions for View Binding
In build.gradle
, add plugin kotlin-android-extensions in addition to kotlin-android plugin
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
In MainActivity.kt, import
layout widget properties
import kotlinx.android.synthetic.main.<layout>.*
Since <layout>
references xml file under src/main/res/layout, views under activity_main.xml
can be imported with following import statement:
import kotlinx.android.synthetic.main.activity_main.*
- You can easily set strings through value assign instead of old
.setText
function call
<!-- For This TextView... -->
<TextView android:id="@+id/device_sdk_version" ... />
/* Java */
TextView textview = (TextView) this.findViewById(R.id.device_sdk_version);
textview.setText("API " + Build.VERSION.SDK_INT);
/* Kotlin */
device_sdk_version.text = "API ${Build.VERSION.SDK_INT}"
NOTE
AndroidStudio Replaces .
in Android Resource IDs with _
for readability and convenience. However, Kotlin Android Extensions cannot recognize this. You must concatenate words in android:id
using _
to avoid this issue.
4. Implement DeviceIntel Widget 95531bb
Since the purpose of this application is quite text-based, we recycle activity_main.xml for the widget as well. In this step, we setup widget configurations in AndroidManifest.xml
.
NOTE
Kotlin always generates method stub with following body:
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
However, this is quite not straightforward. For the time being, auto-generated method stub is better when creating with java and Convert Java File to Kotlin File.
5. Improve DeviceIntel Widget 0ad09e9
- Separate layout
deviceintel_widget.xml
fromactivity_main.xml
- Widget Preview Image for Widget Selection View
- Unified TextView Style with style defined in
styles.xml
- Use
.capitalize()
instead of.toUpperCase
for device brand string
- Getting Started with Android and Kotlin : Official Kotlin Tutorial using AndroidStudio
- Kotlin Koans : Official Kotlin Exercises with failing UnitTest at first. Code to pass! You can play with Koans Online as well.
- Mac Unicode Keys
⌃
Control⌥
Option / Alt⌘
Command / Cmd⇧
Super / Shift