Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

androidx.core.content

Classes

ContentResolverCompat

Helper for accessing features in android.content.ContentResolver in a backwards compatible fashion.

ContextCompat

Helper for accessing features in android.content.Context.

FileProvider

FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content://Uri for a file instead of a file:///Uri

IntentCompat

Helper for accessing features in android.content.Intent.

MimeTypeFilter

Provides utility methods for matching MIME type filters used in ContentProvider.

PermissionChecker

This class provides permission check APIs that verify both the permission and the associated app op for this permission if such is defined.

SharedPreferencesCompat

Top-level functions summary

ContentValues
contentValuesOf(vararg pairs: Pair<String, Any?>)

Returns a new ContentValues with the given key/value pairs as elements.

Extension functions summary

For android.content.Context
T

Return the handle to a system-level service by class.

Unit
Context.withStyledAttributes(set: AttributeSet? = null, attrs: IntArray, defStyleAttr: Int = 0, defStyleRes: Int = 0, block: TypedArray.() -> Unit)

Executes block on a TypedArray receiver.

Unit
Context.withStyledAttributes(resourceId: Int, attrs: IntArray, block: TypedArray.() -> Unit)

Executes block on a TypedArray receiver.

For android.content.SharedPreferences
Unit
SharedPreferences.edit(commit: Boolean = false, action: Editor.() -> Unit)

Allows editing of this preference instance with a call to apply or commit to persist the changes.

Top-level functions

contentValuesOf

fun contentValuesOf(vararg pairs: Pair<String, Any?>): ContentValues

Returns a new ContentValues with the given key/value pairs as elements.

Exceptions
IllegalArgumentException When a value is not a supported type of ContentValues.

Extension functions

edit

inline fun SharedPreferences.edit(commit: Boolean = false, action: Editor.() -> Unit): Unit

Allows editing of this preference instance with a call to apply or commit to persist the changes. Default behaviour is apply.


prefs.edit {
    putString("key", value)
}

To commit changes:


prefs.edit(commit = true) {
    putString("key", value)
}

systemService

inline fun <reified T> Context.systemService(): T

Return the handle to a system-level service by class.

The return type of this function intentionally uses a platform type to allow callers to decide whether they require a service be present or can tolerate its absence.

withStyledAttributes

inline fun Context.withStyledAttributes(set: AttributeSet? = null, attrs: IntArray, defStyleAttr: Int = 0, defStyleRes: Int = 0, block: TypedArray.() -> Unit): Unit

Executes block on a TypedArray receiver. The TypedArray holds the attribute values in set that are listed in attrs. In addition, if the given AttributeSet specifies a style class (through the style attribute), that style will be applied on top of the base attributes it defines.

Parameters
set The base set of attribute values.
attrs The desired attributes to be retrieved. These attribute IDs must be sorted in ascending order.
defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies defaults values for the TypedArray. Can be 0 to not look for defaults.
defStyleRes A resource identifier of a style resource that supplies default values for the TypedArray, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.

withStyledAttributes

inline fun Context.withStyledAttributes(resourceId: Int, attrs: IntArray, block: TypedArray.() -> Unit): Unit

Executes block on a TypedArray receiver. The TypedArray holds the the values defined by the style resource resourceId which are listed in attrs.

Parameters
attrs The desired attributes. These attribute IDs must be sorted in ascending order.