Skip to content

Simple Android Widget written in Kotlin for Multi-Device Testers to indicate Android Version and Device Name

Notifications You must be signed in to change notification settings

pjhjohn/kotlin-device-intel

Repository files navigation

kotlin-device-intel

playstore minSdkVersion

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

  1. In AndroidStudio, Go to File > Settings > Plugins > Install JetBrains plugin… and Search Kotlin

  2. Install Plugin and Restart AndroidStudio

  3. 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:

  1. Go to Help > Find Action ⇧⌘A and select Convert Java File to Kotlin File ⌥⇧⌘K

  2. This will try to convert MainActivity.java to MainActivity.kt

  3. 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.

  1. 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.*
  1. 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 from activity_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

References

Miscellaneous

  • Mac Unicode Keys
    • Control
    • Option / Alt
    • Command / Cmd
    • Super / Shift

About

Simple Android Widget written in Kotlin for Multi-Device Testers to indicate Android Version and Device Name

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published