Last active
May 21, 2019 09:36
-
-
Save hwd6190128/6e73c06da6a2cdca24e2143e6907b342 to your computer and use it in GitHub Desktop.
ResUtils fun of android dev
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
/** | |
* 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