16

What is the difference between using them, and when should they be used?

An example of the documentation for an AppCompatView is:

A tint aware EditText. This will automatically be used when you use EditText in your layouts. You should only need to manually use this class when writing custom views

Why should the AppCompatView only be used for custom views?

There is a similar question, but I am looking for a good explanation for why the AppCompatView should only be used for custom views.

Community
  • 1
  • 1
NaiveBz
  • 834
  • 2
  • 8
  • 19

2 Answers2

13

Some material design features like the material theme and custom activity transitions are only available on Android 5.0 (API level 21) and above. However, you can design your apps to make use of these features when running on devices that support material design and still be compatible with devices running previous releases of Android.

Que-> Why the AppCompatView should only be used for custom views.

Answer -> In simple terms AppCompatView is used for maintaining compatibility. If your app uses the Material theme as with Theme.Material but does not provide an alternative theme, your app will not run on versions of Android earlier than 5.0.

If the layouts that you design according to the material design guidelines do not use any of the new XML attributes introduced in Android 5.0 (API level 21), they will work on previous versions of Android. Otherwise, you can provide alternative layouts. You can also provide alternative layouts to customize how your app looks on earlier versions of Android.

Making backwards compatible material design Android applications is much easier with AppCompat, especially when you understand how its styles and themes are working together to dynamically tint the user interface.

With AppCompat, you should spend less time fiddling with assets and backwards compatibility, and more time focusing on actually building your application.

Currently, new projects created through Android Studio incorporate this library by default.

Note: This library depends on the v4 Support Library.

Below are few links for references

  1. Android Material Themes Made Easy With AppCompat
  2. Migrating to the AppCompat Library
  3. Getting Material Design for Pre-Lollipop Devices with AppCompat v21
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
9

In your layouts, you should be using the "normal" views, since the support library injects the compatibility code automatically. The classes provided by the support library are only needed when writing a new custom view. In this case you have to extend the AppCompat views in order to get the new properties (such as tint).

Valentin Kuhn
  • 753
  • 9
  • 25
  • Yes, but why do you need to extend the `AppCompat` views to do so? – Farbod Salamat-Zadeh Jun 23 '16 at 13:11
  • 1
    The AppCompat views are only injected when you use the normal views in layouts, replacing the normal ones. When you extend a normal view, you will just have the functionality of the normal view without the AppCompat functionality. – Valentin Kuhn Jun 24 '16 at 14:05