Skip to content

Instantly share code, notes, and snippets.

View alz-ahm's full-sized avatar
😎
coding...

A.A. alz-ahm

😎
coding...
View GitHub Profile
ImageView.doSomething(otherArguments){
//we have direct access to all image view properties (width, height, etc.) here
//Also this will refer to image view
}
//Calling Static method
MyUtil.doSomethingOnImage(imageView, otherArguments)
//Calling extension method
imageView.doSomething(otherArguments)
//Without extension
Toast.makeText(this, “Hello”, Toast.LENGTH_SHORT).show()
//With Extension
showToast("Hello")
//Extension Method
fun Context.showToast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, text, duration).show()
}
//Without Extension
view1.setOnClickListener...
view2.setOnClickListener...
viewN.setOnClickListener...
//With Extension
constraintGroup.setAllOnClickListener {
}
//without extension
liveData.value = liveData.value
//With extension
liveData.notifyObservers()
//Extension function
fun <T> MutableLiveData<T>.notifyObservers() {
value = value //resetting the value forces to notify observers
}
//Without extension
inputET.addTextChangedListener(object: TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
//Withoug extension
Picasso.get().load(/* image url */).placeholder(/* placeholder */).into(myImageView)
//With extension
myImageView.load(/* image url */)
//Extension definition
fun ImageView.load(path: String) {
val placeholder = R.id.whatEver
var fullPath = path
//Using helper methods in ViewModel is right
fun getAdminAccesssVisibility(): Int {
return if(/** some condition **/) View.VISIBLE else View.GONE
}
//This is right!!
viewModel.adminAccessVisibility.observe(this, Observer {
myView.visibility = it
})
//This is right!!
viewModel.adminAccessVisibility.observe(this, Observer {
myView.visibility = it
})