2018-11-14 13:31:21
What you’ll learn
- How to indicate support library classes in your app.
- How to tell the difference between the values for
compileSdkVersion
,targetSdkVersion
, andminSdkVersion
. - How to recognize deprecated or unavailable APIs in your code.
- More about the Android support libraries.
compileSdkVersion, targetSdkVersion, and minSdkVersion
- compileSdkVersion: The compile version is the Android framework version your app is compiled with in Android Studio.
- minSdkVersion: The minimum version is the oldest Android API version your app runs under.
- targetSdkVersion: The target version indicates the API version your app is designed ans tesed for.If the API of the Android platform is higher than this number (that is, your app is running on a newer device), the platform may enable compatibility behaviors to make sure that your app continues to work the way it was designed to. For example, Android 6.0 (API 23) provides a new runtime permissions model. If your app targets a lower API level, the platform falls back to the older install-time permissions model.
Change color
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState != null) {
helloText.setTextColor(savedInstanceState.getInt("color", R.color.black))
}
changeColorButton.setOnClickListener {
val colorName = colorArray[Random().nextInt(20)]
// get resource indentifier
val resourceName = resources.getIdentifier(colorName, "color", applicationContext.packageName)
// get color int
val colorRes = ContextCompat.getColor(this, resourceName)
helloText.setTextColor(colorRes)
}
}
// save color status
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("color", helloText.currentTextColor)
}
Summary
Android uses three directives to indicate how your app should behave for different API versions:
minSdkVersion
: the minimum API version your app supports.compileSdkVersion
: the API version your app should be compiled with.targetSdkVersion
: the API version your app was designed for.
The ContextCompat
class provides methods for compatibility with context and resource-related methods for both old and new API levels.