Skip to content Skip to sidebar Skip to footer

How Does AppCompat Inflate Layouts That Do Not Explicitly Use AppCompat Widgets?

It occurred to me while using AppCompat, that I had been using things like Button instead of android.support.v7.widget.AppCompatButton within my layout XML files. I did a test, via

Solution 1:

In the process of researching this topic in order to ask the question correctly, I discovered the answer myself.

When using AppCompatActivity, some interesting things happen:

  1. A LayoutInflater.Factory is applied to the default LayoutInflater, via setFactory. The AppDelegateImpl classes within AppCompat implement the Factory interface, and one of them is chosen as the factory delegate depending on the API level. There is also a slightly different Factory2, targeting later APIs.
  2. When your views are being inflated from XML, the name of the view's class is passed into the Factory's createView method, which is given the opportunity to override the actual view that is created.
  3. The name of the view is checked against a hard-coded hash table of strings in AppCompatViewInflater, and if a match is found the view is inflated by the delegate, instead of the default inflater.

Post a Comment for "How Does AppCompat Inflate Layouts That Do Not Explicitly Use AppCompat Widgets?"