Home > Back-end >  Android: how appcompat and material create appcompat and material views from default?
Android: how appcompat and material create appcompat and material views from default?

Time:11-20

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:

  1. Activity calls setFactory2 method of the default layoutInflator with custom implementation of LayoutInflator.Factory2 interface.
  2. This implementation passes all the createView calls to the instance of AppCompatViewInflater class.
  3. 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>
  • Related