Skip to content

Instantly share code, notes, and snippets.

View theerasan's full-sized avatar

Theerasan Tonthongkam theerasan

View GitHub Profile
fun main(args: Array<String>) {
var x = 10
var y = 3
println("Before Swapping x = ${x}, y = ${y}")
x += y
y = x - y
x -= y
fun main(args: Array<String>) {
println("IPHONE AND ANDROID".reverseSentence())
}
private fun String.reverseSentence(): String {
val words = this.split(" ")
return words.map { it.reversed() }.joinToString { it }.replace(",", "")
}
fun main(args: Array<String>) {
println(13.isPrimeNumber())
}
private fun Int.isPrimeNumber(): Boolean {
if (this < 1) return false
var count = 0
(1..this).forEach {
if (this%it == 0) count++
}
// ViewExtension
fun View.setOnAnimateClickListener(onClick: (View) -> Unit) {
this.setOnAnimateClickListener(null, onClick)
}
// MainActivity
val applyButton = findViewById(R.id.applyButton)
applyButton.setOnAnimateClickListener({view ->
private fun initCardViewWithKotlinExtensionWay() {
val cardView2 = findViewById(R.id.card_view2) as CardView
cardView2.setOnAnimateClickListener(R.id.imageBackground2, { view ->
Toast.makeText(this, "Clicked " + view.id , Toast.LENGTH_SHORT).show()
})
}
fun View.setOnAnimateClickListener(resId: Int?, onClick: (View) -> Unit) {
var secondaryView: View? = null
if (resId != null) {
secondaryView = this.findViewById(resId)
}
this.setOnClickListener({ view ->
Handler().postDelayed({
@theerasan
theerasan / MainActivity.kt
Last active October 5, 2017 16:59
initCardViewWithNormalWay function
private fun initCardViewWithNormalWay() {
val cardView = findViewById(R.id.card_view) as CardView
cardView.setOnTouchListener { v, event ->
Log.d("ANIMATE", event.toString())
when (event.action) {
MotionEvent.ACTION_DOWN -> {
v.animate().cancel()
v.animate().scaleY(0.96f).scaleX(0.96f).setDuration(200).start()
val img = v.findViewById(R.id.imageBackground)
@theerasan
theerasan / activity_main.xml
Created October 5, 2017 16:27
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
<ta.com.workshop2.ImageRatioView
android:id="@+id/banner"
android:src="@drawable/meet"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:imageOrientation="landscape"
app:imageRatio="W2L1" />
class ImageRatioView : AppCompatImageView {
var imageOrientation: Int? = 0
var imageRatio: Int? = 0
val LANDSCAPE = 0
val PORTRAIT = 1
val SQUARE = 0
val RATIO_2_1 = 1
val RATIO_2_1_DIVISION = 0.5
val PIXEL_PERFECT = 1