Last active
December 24, 2015 02:28
-
-
Save stepango/ff1ee389d8d0717aeec5 to your computer and use it in GitHub Desktop.
Android Activity extensions
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
import android.app.Activity | |
import android.widget.Toast | |
import android.view.View | |
import kotlin.properties.Delegates | |
import android.app.Fragment | |
fun Activity.toast(s: CharSequence, length: Int = Toast.LENGTH_SHORT) = | |
Toast.makeText(this, s, length).show() | |
fun <T : View?>Activity.find(id: Int) = | |
Delegates.lazy { this.findViewById(id) as T } | |
fun <T> Activity.service(name: String) = | |
Delegates.lazy { this.getSystemService(name) as T } | |
fun Activity.click(id: Int, f: ((View) -> Unit)?) = | |
this.findViewById(id)?.setOnClickListener(f) | |
fun Activity.click(v: View?, f: ((View) -> Unit)?) = | |
v?.setOnClickListener(f) | |
fun Fragment.toast(s: CharSequence, length: Int = Toast.LENGTH_SHORT) = | |
getActivity()?.toast(s, length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment