From cd7b988fe1cfc5f0181b41f84f7d4d73d0f29b8c Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Fri, 1 Dec 2023 21:30:52 +0530 Subject: [PATCH] Adjusted the size of the hamburger icon. * To address unexpected behavior, where setting the icon directly to the hamburger resulted in it taking the full width and height of the toolbar, we resized the app icon and applied it to the hamburger icon. --- .../core/extensions/ContextExtensions.kt | 42 +++++++++++++++++++ core/src/main/res/values/dimens.xml | 1 + .../custom/main/CustomReaderFragment.kt | 8 +++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ContextExtensions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ContextExtensions.kt index cae26c73c..d1f27f793 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ContextExtensions.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ContextExtensions.kt @@ -21,9 +21,14 @@ package org.kiwix.kiwixmobile.core.extensions import android.content.Context import android.content.Intent import android.content.IntentFilter +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.drawable.BitmapDrawable +import android.graphics.drawable.Drawable import android.util.TypedValue import android.widget.Toast import androidx.annotation.AttrRes +import androidx.core.content.ContextCompat import org.kiwix.kiwixmobile.core.base.BaseBroadcastReceiver import java.util.Locale @@ -59,3 +64,40 @@ fun Context.getAttribute(@AttrRes attributeRes: Int) = with(TypedValue()) { else throw RuntimeException("invalid attribute $attributeRes") } + +fun Context.getResizedDrawable(resourceId: Int, width: Int, height: Int): Drawable? { + val drawable = ContextCompat.getDrawable(this, resourceId) + + return if (drawable != null) { + val bitmap = Bitmap.createScaledBitmap( + getBitmapFromDrawable(drawable), + width, + height, + false + ) + + BitmapDrawable(resources, bitmap).apply { + bounds = drawable.bounds + } + } else { + null + } +} + +fun Context.getBitmapFromDrawable(drawable: Drawable): Bitmap { + if (drawable is BitmapDrawable) { + return drawable.bitmap + } + + val bitmap = Bitmap.createBitmap( + drawable.intrinsicWidth, + drawable.intrinsicHeight, + Bitmap.Config.ARGB_8888 + ) + val canvas = Canvas(bitmap) + drawable.setBounds(0, 0, canvas.width, canvas.height) + drawable.draw(canvas) + + return bitmap +} + diff --git a/core/src/main/res/values/dimens.xml b/core/src/main/res/values/dimens.xml index b81eed089..b35232693 100644 --- a/core/src/main/res/values/dimens.xml +++ b/core/src/main/res/values/dimens.xml @@ -20,4 +20,5 @@ 25dp 48dp + 36dp diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt index e117b9146..1abb7c256 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt @@ -29,6 +29,7 @@ import androidx.drawerlayout.widget.DrawerLayout import androidx.navigation.fragment.findNavController import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setupDrawerToggle +import org.kiwix.kiwixmobile.core.extensions.getResizedDrawable import org.kiwix.kiwixmobile.core.extensions.isFileExist import org.kiwix.kiwixmobile.core.main.CoreReaderFragment import org.kiwix.kiwixmobile.core.main.MainMenu @@ -57,6 +58,7 @@ class CustomReaderFragment : CoreReaderFragment() { var dialogShower: DialogShower? = null private var permissionRequiredDialog: Dialog? = null private var appSettingsLaunched = false + @Suppress("NestedBlockDepth") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) @@ -77,7 +79,11 @@ class CustomReaderFragment : CoreReaderFragment() { if (BuildConfig.DISABLE_TITLE) { // if the title is disable then set the app logo to hamburger icon, // see https://github.com/kiwix/kiwix-android/issues/3528#issuecomment-1814905330 - it.setNavigationIcon(R.mipmap.ic_launcher) + val iconSize = + resources.getDimensionPixelSize(org.kiwix.kiwixmobile.core.R.dimen.hamburger_icon_size) + getResizedDrawable(R.mipmap.ic_launcher, iconSize, iconSize)?.let { drawable -> + it.navigationIcon = drawable + } } } }