Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why extends ImageView marked as error?

Android Studio marks as error this line:

public class ParallaxView extends ImageView

Here the error:

This custom view should extend android.support.v7.widget.AppCompatImageView instead less... (Ctrl+F1) 

In order to support features such as tinting, the appcompat library will automatically load special appcompat replacements for the builtin widgets.

However, this does not work for your own custom views.  Instead of extending the android.widget classes directly, you should instead extend one of the delegate classes in android.support.v7.widget.AppCompat.

It recommends me to extend AppCompatImageView but then my JUnit test don't pass because AppCompatImageView needs a mock Context with resources and Imageview doesn't need this.

Here the question to solve the other problem:
NullPointerException creating an AppCompatImageView with mock Context

Can I ignore this error and use ImageView? Any other solution?

like image 850
MarcGV Avatar asked Mar 23 '17 10:03

MarcGV


People also ask

What is ImageView in android?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.

What is Appcompat ImageView?

androidx.appcompat.widget.AppCompatImageView. A ImageView which supports compatible features on older versions of the platform, including: Allows dynamic tint of its background via the background tint methods in androidx.


1 Answers

Using AppCompat widgets allows you to have some material design (and other new) features on devices with pre-Lollipop versions of Android.

At this point AppCompatImageView only provides the support for background tint and vector drawables. If you don't use them, then extending the regular ImageView will be fine.

like image 115
dev.bmax Avatar answered Sep 17 '22 20:09

dev.bmax