Reputation: 3447
I simply want to extend ImageView:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
}
But Android Studio returns the compile error:
This custom view should extend android.support.v7.widget.AppCompatImageView instead
But importing android.support.v7.widget.AppCompatImageView
insted of android.widget.ImageView
does not solve the problem because ImageView is marked then as not imported...
What am I doing wrong here?
Upvotes: 2
Views: 8227
Reputation: 11688
For those looking for an answer and are using the androidx libraries, here's an update.
Yes, you still need to use AppCompatImageView
, but just use the new libraries. Here's the new import statement:
import androidx.appcompat.widget.AppCompatImageView;
Just changing this import to this other answer should do the trick.
Upvotes: 5
Reputation: 765
Using AppCompat
widgets allows you to have some features on devices with pre-Lollipop versions of Android.
Use AppCompatImageView
,ImageView
will be fine now.
Upvotes: 3
Reputation: 4513
This works perfectly fine
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
public class CustomImageView extends AppCompatImageView {
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
When you import AppCompatImageView
then you have to use it too. Not only this, in JAVA, what you import is what you use.putting it other way, you import what you want to use
Upvotes: 8