Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When activity theme is not a material theme, Beagle view crashes. #117

Open
kingori opened this issue Dec 14, 2022 · 3 comments
Open

When activity theme is not a material theme, Beagle view crashes. #117

kingori opened this issue Dec 14, 2022 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@kingori
Copy link

kingori commented Dec 14, 2022

When trying to show Beagle view on activities which uses non-material theme, app crashes ( Drawer ) or dialog not shown ( Modal).

I think it is caused by using MaterialButton class in xml or code.

Is it possible to replace MaterialButton to Button?

crash log is like below:

java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
 at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:247)
 at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:216)
 at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:144)
 at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:76)
 at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:230)

I've assigned non-material theme in init function like below:

 Beagle.initialize(application, Appearance( mypkg.R.style.My_non_material_theme))
@pandulapeter pandulapeter self-assigned this Dec 14, 2022
@pandulapeter pandulapeter added the bug Something isn't working label Dec 14, 2022
@pandulapeter
Copy link
Owner

pandulapeter commented Dec 14, 2022

Hi!
Thanks for bringing this up.
Indeed the current version of the library expects a Material theme.
But you're right, there's no reason for using MaterialButton (if your Activity has a Material theme, the layout inflater will replace all Views with their Material counterparts - something I didn't know back then).
I'll make sure to fix this in the next update, hopefully in the coming days. I'll keep you posted!

@kingori
Copy link
Author

kingori commented Dec 14, 2022

@pandulapeter thanks for the reply.
If you expect material-theme in this library, what about to wrap context with material-themed context?

This is code snippet I am using in my project

fun Activity.toMaterialContext(): Context {
    // this code is dangerous - using private material theme attribute
    val typedArray = this.theme.obtainStyledAttributes(intArrayOf(com.google.android.material.R.attr.isMaterialTheme))
    val isMaterialTheme = typedArray.getBoolean(0, false)
    typedArray.recycle()

    return if (isMaterialTheme) {
        this
    } else {
        wrapMaterialTheme( R.style.my_default_material_theeme )
        )
    }
}

fun Activity.wrapMaterialTheme(@StyleRes newMaterialThemeResId: Int): Context {
    return ContextThemeWrapper(
        this,
        newMaterialThemeResId
    ).apply {
        val materialContext = this
        val delegate = AppCompatDelegate.create(
            materialContext,
            requireNotNull(this),
            null
        )

        val inflater = LayoutInflater.from(materialContext)
        LayoutInflaterCompat.setFactory2(
            inflater,
            delegate as LayoutInflater.Factory2
        )
    }
}

If you apply this code, you can ensure library is using material themed context. But if user is applyed non-material theme, their theme is overridden. But I don't think this cause big(?) problem.

@pandulapeter
Copy link
Owner

Thanks, that's really useful!
I'll try to take care of this as soon as possible :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants