Skip to content

Instantly share code, notes, and snippets.

@stepango
Last active December 24, 2015 02:28
Show Gist options
  • Save stepango/ff1ee389d8d0717aeec5 to your computer and use it in GitHub Desktop.
Save stepango/ff1ee389d8d0717aeec5 to your computer and use it in GitHub Desktop.
Android Activity extensions
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