Reputation: 9020
I initially wrote this SSCCE with a minsdkversion
of 21
(Lollipop), and it worked fine. But my need is to use a VectorDrawableCompat
in pre-lollipop. In the SSCCE, there is an ImageView
in the XML layout, in which the source of the image is a vector graphic defined in res/drawable/
directory.
Initially, when minsdkversion
was set to 21
in manifest, I used android:src="@drawable/vector_circle"
to set the vector graphic as the source of the image in ImageView
. This worked fine.
Then I changed minsdkversion
in manifest file to 11
. I also changed ImageView
's android:src="@drawable/vector_circle"
attribute to app:srcCompat="@drawable/vector_circle"
in XML layout. But I am getting
Unexpected namespace prefix "app" found for tag ImageView
on app:srcCompat="@drawable/vector_circle"
line. How should I fix this?
SSCCE:
res/drawable/vector_circle.xml:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="465dp"
android:height="420dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group>
<path android:strokeColor="#2196F3"
android:strokeWidth="2"
android:pathData="M150,10 L75,200 L225,200 Z" />
</group>
</vector>
activity_main:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/vector_circle" /><!-- app:srcCompat="@drawable/vector_circle" -->
</FrameLayout>
MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
EDIT:
I changed res/layout/activity_main.xml
's ImageView
element to
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/vector_circle"
tools:ignore="MissingPrefix" />
I also added layout-v21/activity_main.xml
to the res/
directory:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/vector_circle" /><!-- app:srcCompat="@drawable/vector_circle" -->
</FrameLayout>
and added the xmlns:tools="http://schemas.android.com/tools"
to FrameLayout
element.
The pre-compilation error went away. But when I run the app on Lollipop device, the vector graphic is drawn perfectly fine, but if I run the app on a pre-lollipop device, the application does not fail, but the vector graphic is not drawn at all.
Upvotes: 1
Views: 3527
Reputation: 1396
All you have to do to fix this is use the widget that can handle app:srcCompat
and that is AppCompatImageView
instead of ImageView
.
Upvotes: 2