Skip to content

Instantly share code, notes, and snippets.

@hwd6190128
Last active May 21, 2019 09:36
Show Gist options
  • Save hwd6190128/6e73c06da6a2cdca24e2143e6907b342 to your computer and use it in GitHub Desktop.
Save hwd6190128/6e73c06da6a2cdca24e2143e6907b342 to your computer and use it in GitHub Desktop.
ResUtils fun of android dev
/**
* Created by Howard Chang on 2018/10/25.
*/
object ResUtils {
fun getStringArray(@ArrayRes id: Int, context: Context): Array<String> {
return context.resources.getStringArray(id)
}
fun getIntArray(@ArrayRes id: Int, context: Context): IntArray {
return context.resources.getIntArray(id)
}
fun getInt(@IntegerRes id: Int, context: Context): Int {
return context.resources.getInteger(id)
}
fun getString(
@StringRes id: Int, context: Context,
formatArgs: Any? = null
): String {
return if (formatArgs != null) {
context.resources.getString(id, formatArgs)
} else {
context.resources.getString(id)
}
}
fun getDimension(@DimenRes id: Int, context: Context): Float {
return context.resources.getDimension(id)
}
fun getDimensionPixelSize(@DimenRes id: Int, context: Context): Int {
return context.resources.getDimensionPixelSize(id)
}
fun getColor(@ColorRes id: Int, context: Context): Int {
return ContextCompat.getColor(context, id)
}
fun getDrawable(resource: Int, context: Context): Drawable {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
context.getDrawable(resource)
} else {
ContextCompat.getDrawable(context, resource)!!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment