When you make layouts in Android, you could make default views like TextView or ImageView. But when you use appcompat or material library, they automatically map these views to appcompat or material views. So my question is how they do that, and how I could do that?
For example I have custom MyTextView, and I want all TextView views to be mapped to MyTextView.
CodePudding user response:
This is some stuff that your activity (extended from AppCompatActivity) is doing under the hood.
Basically it looks like this:
- Activity calls
setFactory2
method of the defaultlayoutInflator
with custom implementation ofLayoutInflator.Factory2
interface. - This implementation passes all the
createView
calls to the instance of AppCompatViewInflater class. AppCompatViewInflater
checks the names of the views it needs to create, and creates AppCompat views instead of default ones, if there are any.
For Material it's the same story, but with MaterialComponentsViewInflater instead of AppCompatViewInflater.
You can made your own CustomViewInflater by extending from AppCompatViewInflater (or material), and put this line with the path to your inflater into your app theme:
<item name="viewInflaterClass">your.app.package.CustomViewInflater</item>