Created
September 13, 2020 17:26
-
-
Save CompileConnected/58a10368490892ddbbe6b0873206f8eb to your computer and use it in GitHub Desktop.
Android Extenstions
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
| import android.content.Context | |
| import android.content.res.ColorStateList | |
| import android.graphics.drawable.Drawable | |
| import androidx.core.content.ContextCompat | |
| import androidx.annotation.ColorRes | |
| import androidx.annotation.DrawableRes | |
| import androidx.appcompat.content.res.AppCompatResources | |
| fun <T> Context.getSystemServiceCompat(serviceClass: Class<T>):T = | |
| ContextCompat.getSystemService(this, serviceClass)!! | |
| fun Context.getColorCompat(@ColorRes id: Int): Int = getColorStateListCompat(id).defaultColor | |
| fun Context.getColorStateListCompat(@ColorRes id: Int): ColorStateList = | |
| AppCompatResources.getColorStateList(this, id)!! | |
| fun Context.getDrawableCompat(@DrawableRes id: Int): Drawable = | |
| AppCompatResources.getDrawable(this, id)!! |
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
| import android.content.Context | |
| import android.widget.Toast | |
| import androidx.fragment.app.Fragment | |
| fun Context.showToast(message: String?, duration: Int? = null, ellipsesAt: Int? = null) { | |
| this.createToast(message, duration, ellipsesAt).show() | |
| } | |
| fun Context.createToast(message: String?, duration: Int? = null, ellipsesAt: Int? = 40): Toast { | |
| return Toast.makeText( | |
| this, | |
| (message ?: "").ellipsesAt(ellipsesAt!!), | |
| duration ?: Toast.LENGTH_SHORT | |
| ) | |
| } | |
| fun Fragment.showToast(message: String?, duration: Int? = null) { | |
| this.createToast(message, duration)?.show() | |
| } | |
| fun Fragment.createToast(message: String?, duration: Int? = null): Toast? { | |
| return this.activity?.createToast(message, duration) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment