Created
December 5, 2019 23:02
-
-
Save caHarkness/7ae42d294be7384811c0fc9146a68324 to your computer and use it in GitHub Desktop.
Single Activity Kotlin Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.caharkness.demo.kotlin | |
import android.graphics.Canvas | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.util.TypedValue | |
import android.view.Gravity | |
import android.view.ViewGroup | |
import android.widget.Button | |
import android.widget.LinearLayout | |
import android.widget.TextView | |
class MainActivity : AppCompatActivity() | |
{ | |
// | |
// Single Activity Kotlin Demo | |
// Building a UI in Kotlin (with ZERO prior experience, thanks Stack Overflow University) | |
// | |
// by Conner Harkness on 12/5/2019 | |
// | |
override fun onCreate(savedInstanceState: Bundle?) | |
{ | |
super.onCreate(savedInstanceState) | |
// Commenting out the layout funcionality, let's build our own in Kotlin! | |
// This was put here by Android Studio and its blank activity template, we won't be using XML | |
//setContentView(R.layout.activity_main) | |
// Similar to LinearLayout OurMainLayout = new LinearLayout(this); | |
val OurMainLayout = LinearLayout(this) | |
OurMainLayout.layoutParams = | |
object : LinearLayout.LayoutParams( | |
LinearLayout.LayoutParams.MATCH_PARENT, | |
LinearLayout.LayoutParams.MATCH_PARENT) | |
{ | |
// | |
// This is the equivalent of new LayoutParams(...) {{ gravity = Gravity.CENTER }} | |
// | |
init | |
{ | |
gravity = Gravity.CENTER | |
} | |
} | |
OurMainLayout.gravity = Gravity.CENTER | |
OurMainLayout.orientation = LinearLayout.VERTICAL | |
// "Kotlin doesn't automatically convert between number types." -Kiskae | |
// https://stackoverflow.com/questions/44604555/declaring-byte-in-kotlin-does-compile-time-error-the-integer-literal-does-not-c | |
// | |
// Let's make the color black and call ".toInt()" on it to let .setBackgroundColor() accept an integer literal | |
OurMainLayout.setBackgroundColor(0xFF000000.toInt()) | |
// Similar to TextView OurCaption = new TextView(this) | |
val OurCaption = TextView(this) | |
// | |
// Here, let's do something Java cannot; define functions on the fly within the Kotlin language, but not as anonymous runnables... | |
// This function allows us to programmatically create pixel measurements using inches | |
// | |
fun inches(i: Float): Int | |
{ | |
return TypedValue.applyDimension( | |
TypedValue.COMPLEX_UNIT_IN, | |
i, | |
getResources() | |
.getDisplayMetrics() | |
).toInt() | |
} | |
// | |
// Regardless of the DPI of the device, our caption will have a consistent, quarter inch padding! | |
// | |
OurCaption.setPadding( | |
inches(0.25f), | |
inches(0.25f), | |
inches(0.25f), | |
inches(0.25f)) | |
OurCaption.text = "Hello, world!" | |
OurCaption.setTextColor(0xFFFFFFFF.toInt()) | |
OurCaption.setTextSize(TypedValue.COMPLEX_UNIT_IN, 0.25f) // This allows our TextView's text size to be a quarter inch as well (using TypedValue) | |
val OurButton = object : Button(this) | |
{ | |
// | |
// Another example of anonymous classes, this time with a Button view | |
// | |
init | |
{ | |
text = "Tap me" | |
setPadding( | |
inches(1/8f), | |
inches(1/8f), | |
inches(1/8f), | |
inches(1/8f)) // Give the button some beefy padding so it doesn't look like it skips "press" day | |
// | |
// Lambda version of an anonymous OnClickListener class? | |
// | |
setOnClickListener { | |
text = "Tapped" | |
OurCaption.text = "Goodbye, world!" // Interact with a previously defined view outside of our anonymous Button view class... | |
} | |
layoutParams = ViewGroup.LayoutParams( | |
ViewGroup.LayoutParams.WRAP_CONTENT, | |
ViewGroup.LayoutParams.WRAP_CONTENT) | |
} | |
// Let's have some fun overriding the Button view's draw method | |
override fun draw(canvas: Canvas?) | |
{ | |
// | |
// Unimportant, but make the button violently vibrate from -2 to +2 degrees around its center | |
// "Violently" because the view is only redrawn when interacted with and does not redraw when nothing is happening... | |
// | |
canvas?.rotate( | |
-2f + (Math.sin(System.currentTimeMillis().toDouble()) * 4).toFloat(), | |
canvas?.width / 2f, | |
canvas?.height / 2f) | |
super.draw(canvas) | |
} | |
} | |
OurMainLayout.addView(OurCaption) | |
OurMainLayout.addView(OurButton) | |
// Literally one semicolon short of being identical to setting the content view in Java, nice! | |
setContentView(OurMainLayout) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment