Bug P3
Status Update
Comments
vi...@google.com <vi...@google.com>
lb...@gmail.com <lb...@gmail.com> #2
For now, this is how it can be done, when Android L is the minimal:
https://stackoverflow.com/a/76633615/878126
Meaning:
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun getMaterialInflater(context: Context, @StyleRes theme: Int): LayoutInflater {
//https://stackoverflow.com/q/76303909/878126
//https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:appcompat/appcompat/src/main/java/androidx/appcompat/app/AppCompatDelegateImpl.java;l=1710
val contextThemeWrapper = ContextThemeWrapper(context, theme)
val inflater = LayoutInflater.from(contextThemeWrapper)
val factory = object : LayoutInflater.Factory2 {
val myInflater = MaterialComponentsViewInflater()
override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View? {
return myInflater.createView(
parent,
name,
context,
attrs,
false, // "true" to use the parent's context
false, // androidTheme: Emulate the android:theme attribute for devices before L.
true, // appTheme: Emulate the android:theme attribute for devices before L.
// original code here is VectorEnabledTintResources.shouldBeUsed(), but that's till API 20. No need from API 21
false /* Only tint wrap the context if enabled */
)
}
override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
return onCreateView(null, name, context, attrs)
}
}
LayoutInflaterCompat.setFactory2(inflater, factory)
return inflater
}
Meaning:
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun getMaterialInflater(context: Context, @StyleRes theme: Int): LayoutInflater {
//
//
val contextThemeWrapper = ContextThemeWrapper(context, theme)
val inflater = LayoutInflater.from(contextThemeWrapper)
val factory = object : LayoutInflater.Factory2 {
val myInflater = MaterialComponentsViewInflater()
override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View? {
return myInflater.createView(
parent,
name,
context,
attrs,
false, // "true" to use the parent's context
false, // androidTheme: Emulate the android:theme attribute for devices before L.
true, // appTheme: Emulate the android:theme attribute for devices before L.
// original code here is VectorEnabledTintResources.shouldBeUsed(), but that's till API 20. No need from API 21
false /* Only tint wrap the context if enabled */
)
}
override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
return onCreateView(null, name, context, attrs)
}
}
LayoutInflaterCompat.setFactory2(inflater, factory)
return inflater
}
Description
Sadly, not only that this code seems very fragile as it depends on a library's implementation and relation between quite inner classes, but it won't work because it fails to be built.
It shows an error due to using 2 private functions: appCompatViewInflater.createView , VectorEnabledTintResources.shouldBeUsed()
The error is:
e: file:///C:/Users/User/Desktop/MyApplication/app/src/main/java/com/lb/myapplication/Foo.kt:64:46 Cannot access 'createView': it is package-private in 'AppCompatViewInflater'
And that's before I even try to use this new code...