Created
November 7, 2015 18:29
-
-
Save cnevinc/eb1befa394d5cdb394b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.example.nevin.myapplication | |
import android.app.Activity | |
import android.app.Application | |
import android.os.Bundle | |
import com.jakewharton.threetenabp.AndroidThreeTen | |
import org.jetbrains.anko.button | |
import org.jetbrains.anko.centerInParent | |
import org.jetbrains.anko.onClick | |
import org.jetbrains.anko.relativeLayout | |
import org.threeten.bp.Instant | |
public class MyApp : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
AndroidThreeTen.init(this) | |
} | |
} | |
public class KActivity : Activity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
relativeLayout { | |
button("Add a Custom View") { | |
val info = Instant.now().toString() | |
onClick { ViewController.showLogin(this@KActivity, info) } | |
}.lparams { | |
centerInParent() | |
} | |
} | |
} | |
} | |
This file contains hidden or 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.example.nevin.myapplication | |
import android.app.Activity | |
import android.content.Context | |
import android.transition.TransitionManager | |
import android.widget.EditText | |
import android.widget.FrameLayout | |
import android.widget.LinearLayout | |
import org.jetbrains.anko.* | |
/** | |
* Created by nevin on 11/7/15. | |
*/ | |
public class ViewController(val context: Context) { | |
companion object { | |
// interface for you to show this custom view to screen | |
// pass info as kind of "bind" data | |
public fun showLogin(act: Activity , info: String) { | |
val root = act.find<FrameLayout>(android.R.id.content) | |
TransitionManager.beginDelayedTransition(root) | |
root.addView(genLoginView(act,info)) | |
} | |
// this is where you used to inflate your layout.xml and setup child after onFinishInflate() | |
private fun genLoginView(context: Context, info: String ): LinearLayout { | |
val layout = context.verticalLayout { | |
backgroundColor = 0xffffff.opaque | |
textView("View added @$info") | |
val name = editText{ | |
hint = "Enter your name" | |
} | |
button("Say Hello") { | |
onClick { | |
name.validateWith(lengthCheck) | |
} | |
} | |
} | |
return layout | |
} | |
} | |
} | |
// function expression | |
val lengthCheck: (String) -> Boolean = { it.length > 2 } | |
// extension function for EditText to validate its' text | |
fun EditText.validateWith(func: (String) -> Boolean) { | |
if (func(this.text.toString())) { | |
context.toast("Welcome ${this.text.toString()}!") | |
}else{ | |
context.toast("Name not valid") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment