diff --git a/.documentation/features.md b/.documentation/features.md index 0bba1153..cf70e474 100644 --- a/.documentation/features.md +++ b/.documentation/features.md @@ -55,6 +55,7 @@ cropImage.launch( setMaxCropResultSize(99999, 99999) setActivityTitle("") setActivityMenuIconColor(0) + setActivityMenuTextColor(Color.BLACK) setOutputUri(outputUri) setOutputCompressFormat(Bitmap.CompressFormat.JPEG) setOutputCompressQuality(90) @@ -73,6 +74,10 @@ cropImage.launch( setIntentChooserPriorityList(listOf("com.miui.gallery", "com.google.android.apps.photos")) setActivityBackgroundColor(Color.BLACK) setToolbarColor(Color.GRAY) + setToolbarColor(Color.WHITE) + setToolbarTitleColor(Color.BLACK) + setToolbarBackButtonColor(Color.BLACK) + setToolbarTintColor(Color.BLACK) } ) ``` \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d7d0f38..f00d312f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - `Security` in case of vulnerabilities. ## [x.x.x] - unreleased +### Added +- Added options to customize colors of toolbar back button, title and menu texts. [#437](https://github.com/CanHub/Android-Image-Cropper/issues/437) +### Fixed +- Fixed and issue where setting toolbar color to white would do nothing. [#437](https://github.com/CanHub/Android-Image-Cropper/issues/437) ## [4.3.2] - 08/09/2022 ### Fixed diff --git a/cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt b/cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt index 489abb3c..56582869 100644 --- a/cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt +++ b/cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt @@ -1,10 +1,15 @@ package com.canhub.cropper import android.content.Intent +import android.graphics.PorterDuff +import android.graphics.PorterDuffColorFilter import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import android.net.Uri import android.os.Bundle +import android.text.Spannable +import android.text.SpannableString +import android.text.style.ForegroundColorSpan import android.util.Log import android.view.KeyEvent import android.view.Menu @@ -79,6 +84,10 @@ open class CropImageActivity : latestTmpUri = savedInstanceState.getString(BUNDLE_KEY_TMP_URI)?.toUri() } + setCustomizations() + } + + private fun setCustomizations() { cropImageOptions.activityBackgroundColor.let { activityBackgroundColor -> binding.root.setBackgroundColor(activityBackgroundColor) } @@ -90,9 +99,31 @@ open class CropImageActivity : else resources.getString(R.string.crop_image_activity_title) it.setDisplayHomeAsUpEnabled(true) - cropImageOptions.toolbarColor.takeIf { color -> color != -1 }?.let { toolbarColor -> + cropImageOptions.toolbarColor?.let { toolbarColor -> it.setBackgroundDrawable(ColorDrawable(toolbarColor)) } + cropImageOptions.toolbarTitleColor?.let { toolbarTitleColor -> + val spannableTitle: Spannable = SpannableString(title) + spannableTitle.setSpan( + ForegroundColorSpan(toolbarTitleColor), + 0, + spannableTitle.length, + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE + ) + title = spannableTitle + } + cropImageOptions.toolbarBackButtonColor?.let { backBtnColor -> + try { + val upArrow = ContextCompat.getDrawable( + this, + androidx.appcompat.R.drawable.abc_ic_ab_back_material + ) + upArrow?.colorFilter = PorterDuffColorFilter(backBtnColor, PorterDuff.Mode.SRC_ATOP) + it.setHomeAsUpIndicator(upArrow) + } catch (e: Exception) { + e.printStackTrace() + } + } } } @@ -111,7 +142,9 @@ open class CropImageActivity : ) cropImageOptions.let { options -> options.intentChooserTitle - ?.takeIf { title -> title.isNotBlank() } + ?.takeIf { title -> + title.isNotBlank() + } ?.let { icTitle -> ciIntentChooser.setIntentChooserTitle(icTitle) } @@ -239,6 +272,19 @@ open class CropImageActivity : ) } } + cropImageOptions.activityMenuTextColor?.let { menuItemsTextColor -> + val menuItemIds = listOf( + R.id.ic_rotate_left_24, + R.id.ic_rotate_right_24, + R.id.ic_flip_24, + R.id.ic_flip_24_horizontally, + R.id.ic_flip_24_vertically, + R.id.crop_image_menu_crop + ) + for (itemId in menuItemIds) { + updateMenuItemTextColor(menu, itemId, menuItemsTextColor) + } + } return true } @@ -380,6 +426,28 @@ open class CropImageActivity : } } + /** + * Update the color of a specific menu item to the given color. + */ + open fun updateMenuItemTextColor(menu: Menu, itemId: Int, color: Int) { + val menuItem = menu.findItem(itemId) + val menuTitle = menuItem.title + if (menuTitle?.isNotBlank() == true) { + try { + val spannableTitle: Spannable = SpannableString(menuTitle) + spannableTitle.setSpan( + ForegroundColorSpan(color), + 0, + spannableTitle.length, + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE + ) + menuItem.title = spannableTitle + } catch (e: Exception) { + Log.w("AIC", "Failed to update menu item color", e) + } + } + } + enum class Source { CAMERA, GALLERY } private companion object { diff --git a/cropper/src/main/java/com/canhub/cropper/CropImageContractOptions.kt b/cropper/src/main/java/com/canhub/cropper/CropImageContractOptions.kt index 16b07904..503bdbbb 100644 --- a/cropper/src/main/java/com/canhub/cropper/CropImageContractOptions.kt +++ b/cropper/src/main/java/com/canhub/cropper/CropImageContractOptions.kt @@ -344,6 +344,15 @@ data class CropImageContractOptions @JvmOverloads constructor( return this } + /** + * the color to use for action bar items texts.

+ * *Default: NONE* + */ + fun setActivityMenuTextColor(activityMenuTextColor: Int): CropImageContractOptions { + cropImageOptions.activityMenuTextColor = activityMenuTextColor + return this + } + /** * the Android Uri to save the cropped image to.

* *Default: NONE, will create a temp file* @@ -556,6 +565,35 @@ data class CropImageContractOptions @JvmOverloads constructor( fun setToolbarColor(@ColorInt color: Int) = cropImageOptions.apply { this.toolbarColor = color } + + /** + * Sets the toolbar color of the Crop Image Activity screen. + */ + fun setToolbarTitleColor(@ColorInt color: Int) = cropImageOptions.apply { + this.toolbarTitleColor = color + } + + /** + * Sets the toolbar back button color of the Crop Image Activity screen. + */ + fun setToolbarBackButtonColor(@ColorInt color: Int) = cropImageOptions.apply { + this.toolbarBackButtonColor = color + } + + /** + * Sets the tint color for all items on the toolbar of the Crop Image Activity screen. + * This includes setting the colors of the following together + * 1. Toolbar back button + * 2. Toolbar text + * 3. Toolbar menu icon color + * 4. Toolbar menu text color + */ + fun setToolbarTintColor(@ColorInt color: Int) = cropImageOptions.apply { + this.activityMenuTextColor = color + this.activityMenuIconColor = color + this.toolbarTitleColor = color + this.toolbarBackButtonColor = color + } } fun options( diff --git a/cropper/src/main/java/com/canhub/cropper/CropImageOptions.kt b/cropper/src/main/java/com/canhub/cropper/CropImageOptions.kt index 9a2aa29b..41f25d5b 100644 --- a/cropper/src/main/java/com/canhub/cropper/CropImageOptions.kt +++ b/cropper/src/main/java/com/canhub/cropper/CropImageOptions.kt @@ -43,11 +43,13 @@ open class CropImageOptions : Parcelable { /** The shape of the cropping window. */ @JvmField var cropShape: CropShape + /** * The shape of cropper corners */ @JvmField var cornerShape: CropImageView.CropCornerShape + /** * The radius of the circular crop corner */ @@ -167,6 +169,7 @@ open class CropImageOptions : Parcelable { @JvmField @ColorInt var borderCornerColor: Int + /** * The fill color of circle corner */ @@ -235,6 +238,11 @@ open class CropImageOptions : Parcelable { @ColorInt var activityMenuIconColor: Int + /** the color to use for action bar items texts */ + @JvmField + @ColorInt + var activityMenuTextColor: Int? = null + /** the Android Uri to save the cropped image to */ @JvmField var customOutputUri: Uri? @@ -349,7 +357,19 @@ open class CropImageOptions : Parcelable { /** Toolbar color **/ @JvmField - var toolbarColor: Int = -1 + var toolbarColor: Int? = null + + /** Toolbar color **/ + @JvmField + var toolbarTitleColor: Int? = null + + /** Toolbar color **/ + @JvmField + var toolbarBackButtonColor: Int? = null + + /** Toolbar tint color **/ + @JvmField + var toolbarTintColor: Int? = null /** Init options with defaults. */ constructor() { @@ -393,6 +413,7 @@ open class CropImageOptions : Parcelable { maxCropResultHeight = 99999 activityTitle = "" activityMenuIconColor = 0 + activityMenuTextColor = null customOutputUri = null outputCompressFormat = CompressFormat.JPEG outputCompressQuality = 90 @@ -418,7 +439,10 @@ open class CropImageOptions : Parcelable { cropperLabelTextColor = Color.WHITE showCropLabel = false activityBackgroundColor = Color.WHITE - toolbarColor = -1 + toolbarColor = null + toolbarTitleColor = null + toolbarBackButtonColor = null + toolbarTintColor = null } /** Create object from parcel. */ @@ -461,6 +485,7 @@ open class CropImageOptions : Parcelable { maxCropResultHeight = parcel.readInt() activityTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel) activityMenuIconColor = parcel.readInt() + activityMenuTextColor = parcel.readValue(Int::class.java.classLoader) as Int? customOutputUri = parcel.readParcelable(Uri::class.java.classLoader) outputCompressFormat = CompressFormat.valueOf(parcel.readString()!!) outputCompressQuality = parcel.readInt() @@ -487,7 +512,10 @@ open class CropImageOptions : Parcelable { cropperLabelText = parcel.readString()!! showCropLabel = parcel.readByte().toInt() != 0 activityBackgroundColor = parcel.readInt() - toolbarColor = parcel.readInt() + toolbarColor = parcel.readValue(Int::class.java.classLoader) as Int? + toolbarTitleColor = parcel.readValue(Int::class.java.classLoader) as Int? + toolbarBackButtonColor = parcel.readValue(Int::class.java.classLoader) as Int? + toolbarTintColor = parcel.readValue(Int::class.java.classLoader) as Int? } override fun writeToParcel(dest: Parcel, flags: Int) { @@ -529,6 +557,7 @@ open class CropImageOptions : Parcelable { dest.writeInt(maxCropResultHeight) TextUtils.writeToParcel(activityTitle, dest, flags) dest.writeInt(activityMenuIconColor) + dest.writeValue(activityMenuTextColor) dest.writeParcelable(customOutputUri, flags) dest.writeString(outputCompressFormat.name) dest.writeInt(outputCompressQuality) @@ -555,7 +584,10 @@ open class CropImageOptions : Parcelable { dest.writeString(cropperLabelText) dest.writeByte((if (showCropLabel) 1 else 0).toByte()) dest.writeInt(activityBackgroundColor) - dest.writeInt(toolbarColor) + dest.writeValue(toolbarColor) + dest.writeValue(toolbarTitleColor) + dest.writeValue(toolbarBackButtonColor) + dest.writeValue(toolbarTintColor) } override fun describeContents(): Int { diff --git a/sample/src/main/java/com/canhub/cropper/sample/SampleCrop.kt b/sample/src/main/java/com/canhub/cropper/sample/SampleCrop.kt index 6674be43..cee96248 100644 --- a/sample/src/main/java/com/canhub/cropper/sample/SampleCrop.kt +++ b/sample/src/main/java/com/canhub/cropper/sample/SampleCrop.kt @@ -169,6 +169,7 @@ internal class SampleCrop : Fragment() { // setMaxCropResultSize(999, 999) // setActivityTitle("CUSTOM title") // setActivityMenuIconColor(RED) +// setActivityMenuTextColor(Color.BLACK) // setOutputUri(outputUri) // setOutputCompressFormat(Bitmap.CompressFormat.PNG) // setOutputCompressQuality(50) @@ -193,7 +194,10 @@ internal class SampleCrop : Fragment() { )) */ // setActivityBackgroundColor(Color.BLACK) -// setToolbarColor(Color.GRAY) +// setToolbarColor(Color.WHITE) +// setToolbarTitleColor(Color.BLACK) +// setToolbarBackButtonColor(Color.BLACK) +// setToolbarTintColor(Color.BLACK) } ) }