IconCompat

Added in 1.1.0

public class IconCompat implements VersionedParcelable


Helper for accessing features in android.graphics.drawable.Icon.

Summary

Constants

static final int

An icon that was created using createWithAdaptiveBitmap.

static final int

An icon that was created using createWithBitmap.

static final int

An icon that was created using createWithData.

static final int

An icon that was created using createWithResource.

static final int

Value returned when the type of an Icon cannot be determined.

static final int

An icon that was created using createWithContentUri.

static final int

An icon that was created using createWithAdaptiveBitmapContentUri.

Public methods

static @Nullable IconCompat

Extracts an icon from a bundle that was added using toBundle.

static @Nullable IconCompat
@RequiresApi(value = 23)
createFromIcon(@NonNull Context context, @NonNull Icon icon)

Creates an IconCompat from an Icon.

static @NonNull IconCompat

Create an Icon pointing to a bitmap in memory that follows the icon design guideline defined by android.graphics.drawable.AdaptiveIconDrawable.

static @NonNull IconCompat

Create an Icon pointing to an image file specified by URI.

static @NonNull IconCompat

Create an Icon pointing to an image file specified by URI.

static @NonNull IconCompat

Create an Icon pointing to a bitmap in memory.

static @NonNull IconCompat

Create an Icon pointing to an image file specified by URI.

static @NonNull IconCompat

Create an Icon pointing to an image file specified by URI.

static @NonNull IconCompat
createWithData(@NonNull byte[] data, int offset, int length)

Create an Icon pointing to a compressed bitmap stored in a byte array.

static @NonNull IconCompat

Create an Icon pointing to a drawable resource.

@DrawableRes int

Gets the drawable resource id used to create this icon.

@NonNull String

Gets the package used to create this icon.

int

Gets the type of the icon provided.

@NonNull Uri

Gets the uri used to create this icon.

@Nullable Drawable

Returns a Drawable that can be used to draw the image inside this Icon, constructing it if necessary.

void

Called immediately after this object has been deserialized, can be used to handle any custom fields that cannot be easily annotated.

void
onPreParceling(boolean isStream)

Called immediately before this object is going to be serialized, can be used to handle any custom fields that cannot be easily annotated.

@NonNull IconCompat
setTint(@ColorInt int tint)

Store a color to use whenever this Icon is drawn.

@NonNull IconCompat

Store a color to use whenever this Icon is drawn.

@NonNull IconCompat

Store a blending mode to use whenever this Icon is drawn.

@NonNull Bundle

Adds this Icon to a Bundle that can be read back with the same parameters to createFromBundle.

@NonNull Icon
@RequiresApi(value = 23)
toIcon()

This method is deprecated.

Use toIcon to generate the Icon object.

@NonNull Icon
@RequiresApi(value = 23)
toIcon(@Nullable Context context)

Convert this compat object to Icon object.

@NonNull String

Constants

TYPE_ADAPTIVE_BITMAP

Added in 1.3.0
public static final int TYPE_ADAPTIVE_BITMAP = 5

An icon that was created using createWithAdaptiveBitmap.

TYPE_BITMAP

Added in 1.3.0
public static final int TYPE_BITMAP = 1

An icon that was created using createWithBitmap.

TYPE_DATA

Added in 1.3.0
public static final int TYPE_DATA = 3

An icon that was created using createWithData.

TYPE_RESOURCE

Added in 1.3.0
public static final int TYPE_RESOURCE = 2

An icon that was created using createWithResource.

TYPE_UNKNOWN

Added in 1.1.0
public static final int TYPE_UNKNOWN = -1

Value returned when the type of an Icon cannot be determined.

TYPE_URI

Added in 1.3.0
public static final int TYPE_URI = 4

An icon that was created using createWithContentUri.

TYPE_URI_ADAPTIVE_BITMAP

Added in 1.3.0
public static final int TYPE_URI_ADAPTIVE_BITMAP = 6

An icon that was created using createWithAdaptiveBitmapContentUri.

Public methods

createFromBundle

Added in 1.1.0
public static @Nullable IconCompat createFromBundle(@NonNull Bundle bundle)

Extracts an icon from a bundle that was added using toBundle.

createFromIcon

Added in 1.1.0
@RequiresApi(value = 23)
public static @Nullable IconCompat createFromIcon(@NonNull Context context, @NonNull Icon icon)

Creates an IconCompat from an Icon.

createWithAdaptiveBitmap

Added in 1.1.0
public static @NonNull IconCompat createWithAdaptiveBitmap(@NonNull Bitmap bits)

Create an Icon pointing to a bitmap in memory that follows the icon design guideline defined by android.graphics.drawable.AdaptiveIconDrawable.

Parameters
@NonNull Bitmap bits

A valid android.graphics.Bitmap object

createWithAdaptiveBitmapContentUri

Added in 1.3.0
public static @NonNull IconCompat createWithAdaptiveBitmapContentUri(@NonNull String uri)

Create an Icon pointing to an image file specified by URI. Image file should follow the icon design guideline defined by AdaptiveIconDrawable.

Parameters
@NonNull String uri

A uri referring to local content:// or file:// image data.

createWithAdaptiveBitmapContentUri

Added in 1.3.0
public static @NonNull IconCompat createWithAdaptiveBitmapContentUri(@NonNull Uri uri)

Create an Icon pointing to an image file specified by URI. Image file should follow the icon design guideline defined by AdaptiveIconDrawable.

Parameters
@NonNull Uri uri

A uri referring to local content:// or file:// image data.

createWithBitmap

Added in 1.1.0
public static @NonNull IconCompat createWithBitmap(@NonNull Bitmap bits)

Create an Icon pointing to a bitmap in memory.

Parameters
@NonNull Bitmap bits

A valid android.graphics.Bitmap object

See also
createWithBitmap

createWithContentUri

Added in 1.1.0
public static @NonNull IconCompat createWithContentUri(@NonNull String uri)

Create an Icon pointing to an image file specified by URI.

Parameters
@NonNull String uri

A uri referring to local content:// or file:// image data.

createWithContentUri

Added in 1.1.0
public static @NonNull IconCompat createWithContentUri(@NonNull Uri uri)

Create an Icon pointing to an image file specified by URI.

Parameters
@NonNull Uri uri

A uri referring to local content:// or file:// image data.

createWithData

Added in 1.1.0
public static @NonNull IconCompat createWithData(@NonNull byte[] data, int offset, int length)

Create an Icon pointing to a compressed bitmap stored in a byte array.

Parameters
@NonNull byte[] data

Byte array storing compressed bitmap data of a type that android.graphics.BitmapFactory can decode (see android.graphics.Bitmap.CompressFormat).

int offset

Offset into data at which the bitmap data starts

int length

Length of the bitmap data

See also
createWithData

createWithResource

Added in 1.1.0
public static @NonNull IconCompat createWithResource(@NonNull Context context, @DrawableRes int resId)

Create an Icon pointing to a drawable resource.

Parameters
@NonNull Context context

The context for the application whose resources should be used to resolve the given resource ID.

@DrawableRes int resId

ID of the drawable resource

getResId

Added in 1.1.0
public @DrawableRes int getResId()

Gets the drawable resource id used to create this icon.

Only valid for icons of type TYPE_RESOURCE. Note: This resource may not be available if the application changes at all, and it is up to the caller to ensure safety if this resource is re-used and/or persisted.

getResPackage

Added in 1.1.0
public @NonNull String getResPackage()

Gets the package used to create this icon.

Only valid for icons of type TYPE_RESOURCE. Note: This package may not be available if referenced in the future, and it is up to the caller to ensure safety if this package is re-used and/or persisted.

getType

Added in 1.1.0
public int getType()

Gets the type of the icon provided.

Note that new types may be added later, so callers should guard against other types being returned.

getUri

Added in 1.1.0
public @NonNull Uri getUri()

Gets the uri used to create this icon.

Only valid for icons of type TYPE_URI. Note: This uri may not be available in the future, and it is up to the caller to ensure safety if this uri is re-used and/or persisted.

loadDrawable

Added in 1.1.0
public @Nullable Drawable loadDrawable(@NonNull Context context)

Returns a Drawable that can be used to draw the image inside this Icon, constructing it if necessary.

Parameters
@NonNull Context context

Context in which to load the drawable; used to access Resources, for example.

Returns
@Nullable Drawable

A fresh instance of a drawable for this image, yours to keep.

onPostParceling

Added in 1.6.0
public void onPostParceling()

Called immediately after this object has been deserialized, can be used to handle any custom fields that cannot be easily annotated.

onPreParceling

Added in 1.6.0
public void onPreParceling(boolean isStream)

Called immediately before this object is going to be serialized, can be used to handle any custom fields that cannot be easily annotated.

setTint

Added in 1.1.0
public @NonNull IconCompat setTint(@ColorInt int tint)

Store a color to use whenever this Icon is drawn.

Parameters
@ColorInt int tint

a color, as in setTint

Returns
@NonNull IconCompat

this same object, for use in chained construction

setTintList

Added in 1.1.0
public @NonNull IconCompat setTintList(@Nullable ColorStateList tintList)

Store a color to use whenever this Icon is drawn.

Parameters
@Nullable ColorStateList tintList

as in setTintList, null to remove tint

Returns
@NonNull IconCompat

this same object, for use in chained construction

setTintMode

Added in 1.1.0
public @NonNull IconCompat setTintMode(@Nullable PorterDuff.Mode mode)

Store a blending mode to use whenever this Icon is drawn.

Parameters
@Nullable PorterDuff.Mode mode

a blending mode, as in setTintMode, may be null

Returns
@NonNull IconCompat

this same object, for use in chained construction

toBundle

Added in 1.1.0
public @NonNull Bundle toBundle()

Adds this Icon to a Bundle that can be read back with the same parameters to createFromBundle.

toIcon

Added in 1.1.0
Deprecated in 1.3.0
@RequiresApi(value = 23)
public @NonNull Icon toIcon()

toIcon

Added in 1.3.0
@RequiresApi(value = 23)
public @NonNull Icon toIcon(@Nullable Context context)

Convert this compat object to Icon object.

Returns
@NonNull Icon

Icon object

toString

public @NonNull String toString()