svg-android imageview not working - android

I have a need to show svg files in my android app. svg-android seems like the only library that has any documentation and thus my first approach. The only example available demonstrates how to create an imageview attach an svg image and attach it to the main content view. I however want a svg file to show up on a RelativeLayout I already have defined. I attempted an implementation like so:
ImageView imageView = new ImageView(this);
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.logo);
imageView.setImageDrawable(svg.createPictureDrawable());
RelativeLayout home_header = (RelativeLayout) findViewById(R.id.home_header);
home_header.addView(imageView);
All appears well (no warnings/errors) but when I test the app log cat reports:
05-27 11:25:43.940: I/Adreno200-EGLSUB(28492): <ConfigWindowMatch:2078>: Format RGBA_8888.
05-27 11:25:43.950: E/(28492): Can't open file for reading
05-27 11:25:43.960: E/(28492): Can't open file for reading
I have verified the following:
- File is not open in any other program
- File is properly formatted
What am I missing here? Any suggestions on what might be going on?

android:hardwareAccelerated="false" will disable hardware rendering for the whole activity. An alternative might be to just use:
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Which should disable it only for that View.
PS. If you are looking for an SVG library with better documentation (and more features), try mine: http://code.google.com/p/androidsvg/

After some debugging and comparing the emulator to the native app I discovered that the "can't open file for reading" is not related to the svg files not displaying. Instead it was related to hardware acceleration. I had to set the following in my manifest
android:hardwareAccelerated="false"
problem solved. Time wasted. Brain blown.

There is another option now that doesn't require android:hardwareAccelerated="false"
Use this library - https://github.com/wnafee/vector-compat (api 14+)
android {
// use version 22 or higher
buildToolsVersion "22.0.1"
...
}
dependencies {
compile 'com.wnafee:vector-compat:1.0.5'
...
}
And create a custom ImageView class that uses vector compat class -
public class SvgImageView extends ImageView {
private Drawable icon;
public SvgImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.button_left, 0, 0);
try {
int resId = ta.getResourceId(R.styleable.button_left_b_icon, -1);
if (resId != -1) {
icon = ResourcesCompat.getDrawable(this.getContext(), resId);
}
} finally {
ta.recycle();
}
if (icon != null) {
setImage(icon);
}
}
public void setImage(Drawable icon) {
SvgImageView.this.setImageDrawable(icon);
}
}
Vector image example -
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:width="#dimen/logo_dimen"
android:height="#dimen/logo_dimen"
android:viewportWidth="#integer/view_port_dimen_logo"
android:viewportHeight="#integer/view_port_dimen_logo"
app:vc_viewportWidth="#integer/view_port_dimen_logo"
app:vc_viewportHeight="#integer/view_port_dimen_logo">
<group
android:name="rotationGroup"
android:pivotX="0"
android:pivotY="0"
android:rotation="0">
<path
android:name="v"
android:fillColor="#color/white"
android:pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z"
app:vc_fillColor="#color/white"
app:vc_pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z" />
</group>
</vector>
Example -
<packagename.SvgImageView
app:b_icon="#drawable/google_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3" />

Using androidx.appcompat.widget.AppCompatImageView instead of ImageView worked for me.

AppCompatImageView
from package android.support.v7.widget
Check this post, I have given all the details to use svg. As per my experience, you can use svg in Android flawlessly.
Pros:
No third party library (official android support library needed) No changes in gradle file
Use `android:src` for all `ImageViews` instead of 'app:srcCompat` for svg and `android:src` for other images.
No need to use AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); in static block of BaseActivity.

Related

Support library VectorDrawable Resources$NotFoundException

I am using Design Support Library version 23.4.0. I have enabled the gradle flag:
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
I am using build tools version 23.0.2, but still, I am getting Resources$NotFoundException on KitKat or lower.
It is occurring when I use android:drawableLeft or imageView.setImageResource(R.drawable.drawable_image).
And yes, I am putting this on every activity where I am using drawables
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Is this a bug of the support library?
It took 3 separate things for me to get this to work using support library 23.4.0:
Add this to build.gradle
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
Add the following to onCreate of your Application class
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
(From the reference of this link - "https://stackoverflow.com/a/45582033/10752962")
In API less then 21,use this line before setContentView();
For all XML views in which you are setting a vector drawable replace
android:src
with
app:srcCompat
and in the code replace this:
imageView.setImageResource(...);
with
imageView.setImageDrawable(...);
To complement some of the answers here: backward-compatible support for VectorDrawables comes with a price and doesn't work in all cases.
In which cases does it work? I've made this diagram to help (valid for Support Library 23.4.0 to at least 25.1.0).
Try using:
imageView.setImageDrawable(VectorDrawableCompat.create(getResources(), drawableRes, null));
You don't have to add AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
this way.
Just inflate your vector drawables using VectorDrawableCompat and you're all set.
We had the same issue. Vector drawables were not visible on Kitkat. I solved this issue by adding AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); to the onCreate method of Activities.
Before that dont forget to add:
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
and call setImageResource for the view that you use the vector drawable. My view is ImageButton. I have Android SDK build tools version 23.0.3
Sorry for being late to the party but this answer may help users who want to enable the flag AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); for all activities.
1. Create a class which extends to Application (android.app.Application)
public class MyApplicationClass extends Application
{
#Override
public void onCreate()
{
super.onCreate();
}
}
2. Head over to Manifest.xml and add the following line to your tag
<application
android:name=".MyApplicationClass"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
...
</application>
3. Add the following code above onCreate in MyApplicationClass.java
// This flag should be set to true to enable VectorDrawable support for API < 21
static
{
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Complete code for MyApplicationClass.java
import android.app.Application;
import android.support.v7.app.AppCompatDelegate;
/**
* Created by Gaurav Lonkar on 23-Dec-17.
*/
public class MyApplicationClass extends Application
{
// This flag should be set to true to enable VectorDrawable support for API < 21
static
{
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
#Override
public void onCreate()
{
super.onCreate();
}
}
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
use this in app.gradle
Then use AppCompatDrawableManager to setDrawable and getDrawable. Works for me
Support for vector drawables in places like android:drawableLeft was disabled in support library 23.3. It was announced on Google+:
we’ve decided to remove the functionality which let you use vector
drawables from resources on pre-Lollipop devices due to issues found
in the implementation in version 23.2.0/23.2.1. Using app:srcCompat and setImageResource()
continues to work.
Links to issues:
https://code.google.com/p/android/issues/detail?id=205236
https://code.google.com/p/android/issues/detail?id=204708
However, if you can live with those issues, in 23.4 you can re-enable this functionality using AppCompatDelegate.setCompatVectorFromResourcesEnabled().
If you're curious how this works, the best person to learn from is Chris Banes, who authored this functionality. He explains in detail on his blog.
change
imageView.setImageResource(R.drawable.drawable_image)
to
imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.drawable_image));
if you want to use vectordrawable in xml, use this:
app:srcCompat="#drawable/drawable_image"
I had a similar problem long ago, it did not work by setting
vectorDrawables.useSupportLibrary = true
only worked when I created the "mipmap" folder, and the code used
imageView.setImageResource (R.mipmap.drawable_image)
It has more Info here
Inflating Drawable's
`VectorDrawable` and `AnimatedVectorDrawable` in this support library can be inflated in this way:
Calling static getDrawable() methods:
//This will only inflate a drawable with <vector> as the root element
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);
//This will only inflate a drawable with <animated-vector> as the root element
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);
// This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);
If inflating the Drawable in java code, it is recommended to always use ResourcesCompat.getDrawable() as this handles Lollipop fallback when applicable. This allows the system to cache Drawable ConstantState and hence is more efficient.
The library has the following morph (bi-directional) animations :
Play-Pause morph animation
Play-Stop morph animation
Arrow-Hamburger menu morph animation
As you can see, I produced the above image on my API 16 phone:
import com.wnafee.vector.compat.AnimatedVectorDrawable;
mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector);
Look at the github README for vector-compat here: https://github.com/wnafee/vector-compat
This will fix your problem (down to API 14) if you merge it with your app module's build.gradle dependencies (usually at the end of file):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
//not needed
// compile 'com.android.support:support-vector-drawable:25.0.0'
compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat
// Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0
//not needed
// compile 'com.android.support:support-animated-vector-drawable:25.0.0'
}
Do not put your vectors in drawable-anydpi
, old devices does not support that
put them in drawable
In my particular case, I had this problem because I was using a drawable selector as the image resource with several vectors in the selector, as in:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/vector_select_blue"/>
<item android:state_pressed="true" android:drawable="#drawable/vector_select_black"/>
.
.
etc
</selector>
Yes, pretty bad, but didn't know better at the time.
So, the right way of doing this is using the tint property in your vector file, as in:
<vector ..vector properties..
android:tint="#color/vector_color_selector">
<path ..path properties../>
</vector>
(You can also use the app:tint attribute in the AppCompatImageView)
And now, your vector_color_selector file should have the colors you want, as in:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/blue"/>
<item android:state_pressed="true" android:color="#color/black"/>
.
.
etc
</selector>
I hope this helps someone if previous answers didn't work for you. Stating the obvious, but I must say that you still need to set vectorDrawables.useSupportLibrary = true in gradle, use AppCompatImageView and use app:srcCompat or setImageDrawable + AppCompatResources.getDrawable to avoid any troubles with the vector compat library.
Use AppCompatImageView instead of ImageView as said by Harish Gyanani in comments , it works fine with this for me.
Official docs
I had the same problem and actually what was missing is I was using app:srcCompat on AppCompatTextView except of AppCompatImageView.
The way I have found the problematic part:
My error looks like:
Fatal Exception: android.content.res.Resources$NotFoundException
Resource ID #0x7f0700d1
Here are the steps I followed the resource id of the mentioned drawable :
APK Analyzer -> classesXXX.dex
In this dex file I opened the directory of my apps package name and went to R$drawable file
R$drawable -> Show as byte code.
Search for ID [0x7f0700d1] (check your own ID)
Find the image and check for all the usages (CMD + F7) of the resource
Fix
Hope it will help somebody.

Android VectorDrawable as Compound Drawables

As of stated by this android developer blog post, we can now use VectorDrawables on Android API 7+ using the AppCompat 23.2.0 and later versions.
Everything seems to work fine for me, except when it comes to use drawables as a compound to a TextView.
Normally, one would do something like:
customTab.setCompoundDrawablesWithIntrinsicBounds(
0,
R.drawable.my_vector,
0,
0
);
Unfortunately this is not working at the moment, and I wasn't able to find a workaround for this problem.
As of stated by the post, the only available and working methods are the xml one, using app:srcCompat="#drawable/..." and the Java setImageResource(...)
How can I use the new vector drawable support with the setCompoundDrawable() method?
Thanks in advance.
Edit:
as requested, here's the result of the VectorDrawableCompat class:
the xml is:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="98"
android:viewportHeight="102">
<path
android:fillColor="#4D4D4D"
android:strokeWidth="2"
android:strokeColor="#4D4D4D"
android:pathData="M63.3336386,72.2631001 C56.7778507,76.9021242
48.7563953,79.6307404
40.09319,79.6307404 C17.9503315,79.6307404 0,61.804793 0,39.8153702
C0,17.8259473 17.9503315,0 40.09319,0 C62.2360484,0 80.1863799,17.8259473
80.1863799,39.8153702 C80.1863799,50.8100816 75.6987973,60.7639242
68.4433567,67.9690887 L96.7320074,96.0617174 C98.0293966,97.3501165
97.9978616,99.4159703 96.6953405,100.709466 C95.3837385,102.011979
93.2974318,102.019264 92.0151615,100.745879 L63.3336386,72.2631001
L63.3336386,72.2631001 L63.3336386,72.2631001 Z M40.09319,74.9465792
C59.6310061,74.9465792 75.4695341,59.217802 75.4695341,39.8153702
C75.4695341,20.4129383 59.6310061,4.6841612 40.09319,4.6841612
C20.5553738,4.6841612 4.71684588,20.4129383 4.71684588,39.8153702
C4.71684588,59.217802 20.5553738,74.9465792 40.09319,74.9465792
L40.09319,74.9465792 L40.09319,74.9465792 Z" />
</vector>
Starting from support library 23.2 you can use the next solution:
Drawable drawable=AppCompatDrawableManager.get().getDrawable(mContext, R.drawable.drawable);
view.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
Following the precious suggestions given by #pskink I was able to load correctly a drawable inside my view.
My problem was the selector I was using as my xml to give the "current active tab" feedback in my TabLayout.
I've solved my problem by doing a cast:
Drawable drawable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable = ContextCompat.getDrawable(mContext, tabIcons[i]);
} else {
drawable = getResources().getDrawable(tabIcons[i]);
}
StateListDrawable stateListDrawable = (StateListDrawable) drawable;
customTab.setCompoundDrawablesWithIntrinsicBounds(
null,
stateListDrawable,
null,
null
);
You can solve it by data-binding also:
create adapter method
public class Bindings {
#BindingAdapter({"bind:drawableStartId"})
public static void setDrawableStart(TextView textView, #DrawableRes int id) {
Drawable drawable = AppCompatDrawableManager.get().getDrawable(textView.getContext(), id);
Drawable drawables[] = textView.getCompoundDrawablesRelative();
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, drawables[1], drawables[2], drawables[3]);
}
}
and use app:drawableStartId in your xml file.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="your.path.to.R" />
</data>
<TextView
app:drawableStartId="#{isSelected ? R.drawable.one :R.drawable.another}"
/>
<layout/>

VectorDrawable Backwards Compatibility And Installing Unofficial Support Libraries

Bear with me, I'm new!
I want to use vectors in my android app, and I want my app to be backwards compatible. I found this support library that looks pretty cool!*
So I'm confused about how I would I 'install' it. It gives you a link to download the .pom, .aar, javadoc.jar, and the sources.jar file. Which one should I download, and where (what folder) should I put the file?
(I'm using Android Studio!)
*(Anybody know a different VectorDrawable support library? I'd be interested in hearing everybody's experience!)
Here is a option that worked for me
Use this library - https://github.com/wnafee/vector-compat (api 14+)
android {
// use version 22 or higher
buildToolsVersion "22.0.1"
...
}
dependencies {
compile 'com.wnafee:vector-compat:1.0.5'
...
}
And create a custom ImageView class that uses vector compat class -
public class SvgImageView extends ImageView {
private Drawable icon;
public SvgImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.button_left, 0, 0);
try {
int resId = ta.getResourceId(R.styleable.button_left_b_icon, -1);
if (resId != -1) {
icon = ResourcesCompat.getDrawable(this.getContext(), resId);
}
} finally {
ta.recycle();
}
if (icon != null) {
setImage(icon);
}
}
public void setImage(Drawable icon) {
SvgImageView.this.setImageDrawable(icon);
}
}
Vector image example -
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:width="#dimen/logo_dimen"
android:height="#dimen/logo_dimen"
android:viewportWidth="#integer/view_port_dimen_logo"
android:viewportHeight="#integer/view_port_dimen_logo"
app:vc_viewportWidth="#integer/view_port_dimen_logo"
app:vc_viewportHeight="#integer/view_port_dimen_logo">
<group
android:name="rotationGroup"
android:pivotX="0"
android:pivotY="0"
android:rotation="0">
<path
android:name="v"
android:fillColor="#color/white"
android:pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z"
app:vc_fillColor="#color/white"
app:vc_pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z" />
</group>
</vector>
Example -
<packagename.SvgImageView
app:b_icon="#drawable/google_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3" />
Google just announced Android Studio 1.4 with backwards compatibility for Vector Drawables. It will generate .png files in the appropriate sizes for the different screen densities for pre-Lollipop devices and will use the vector format for Lollipop and up. See this link: http://android-developers.blogspot.com/2015/09/android-studio-14.html
Just make sure that your Gradle Build version is 1.4.0 or above!
Thanks for the people who ported this lib before Google!
Great news is that google released Android Support Library 23.2 Support Vector Drawables and Animated Vector Drawables !
Note:
- Vector images all the way back to API 7 (Android 2.1 Eclair).
- Animated vectors are a bit more limited, going only as far back as API 11
The best solution I found is the BetterVectorDrawable lib together with the SVG to VectorDrawable Converter.
BetterVectorDrawable is the VectorDrawable implementation for Android 4.0+ with configurable fall-back behavior on Android 5.0+. The lib can be added to a project with just one line (see readme).
SVG to VectorDrawable Converter is the batch converter of SVG images to Android VectorDrawable XML resource files. Online version is here.
Links point to readmes, which provide enough information on how to use the lib and the converter.

Resources$NotFoundException: Resource is not a Drawable (color or path)?

I have a textview, when it is clicked, I am populating a listView inside a dialog. This code used to work fine, but today it is throwing exception.
this is my code:
tvSelectedFont = (TextView)findViewById(R.id.lblQuoteSelectedFont);
tvSelectedFont.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ListView listView = new ListView(context);
listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,
new String[] {"Default", "Serif", "Monospace"}));
final Dialog dialog = new Dialog(context);
dialog.setContentView(listView);
dialog.setTitle(R.string.txt_settings_QuotefontName);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedTypeFace = ((TextView)view).getText().toString();
tvSelectedFont.setText(selectedTypeFace);
switch(selectedTypeFace)
{
case "Serif":
selectedQuoteTypeFace = Typeface.SERIF;
break;
case "Monospace":
selectedQuoteTypeFace = Typeface.MONOSPACE;
break;
default:
selectedQuoteTypeFace = Typeface.DEFAULT;
break;
}
tvQuoteTextSample.setTypeface(selectedQuoteTypeFace, selectedQuoteFontStyle);
dialog.dismiss();
}
});
dialog.show();
}
});
The logcat error shows this:
Device driver API version: 29
User space API version: 29
03-17 14:33:24.701 23220-23220/com.example.manas.dailyquoteswidget E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Tue Jul 22 19:59:34 KST 2014
03-17 14:33:27.926 23220-23220/com.example.manas.dailyquoteswidget E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.manas.dailyquoteswidget, PID: 23220
android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f0100a7 a=3}
at android.content.res.Resources.loadDrawable(Resources.java:3415)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.AbsListView.<init>(AbsListView.java:1089)
at android.widget.ListView.<init>(ListView.java:152)
at android.widget.ListView.<init>(ListView.java:148)
at android.widget.ListView.<init>(ListView.java:144)
at com.example.manas.dailyquoteswidget.DailyQuotesWidgetConfigureActivity$6.onClick(DailyQuotesWidgetConfigureActivity.java:182)
at android.view.View.performClick(View.java:4640)
at android.view.View$PerformClick.run(View.java:19425)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Cant figure it out the problem. Any help please?
I encountered this problem in the recent app I made. In my case, the problem was I put an image in the folder called drawable-v21, which is not available in older android API.
The solution is to put your drawable in drawable-...dpi folders too.
In Android Studio change Project hierarchy to Project Files.
Then go to the res folder, you will see multiple drawable folders. Copy the images to appropriate folder(drawable) or based on Api level.
In my case image was present in drawable-24 folder therefore it was not available on api<24 devices.
On the Mac in finder, I just simply moved all the files in the folder ../drawable/drawable-24 to /drawable and everything worked both on older android versions and Oreo. Also when you copy and paste the images into Android Studio make sure to paste them into drawable not drawable//drawable-24 which may be the default.
I figured it out, it was not an issue with the code, but the theme. I recently changed the theme from android:theme="#style/AppTheme"to android:theme="#style/Theme.AppCompat.NoActionBar" after that the problem started. I reverted back the old AppTheme thene it started working again. It seems that the NoActionBar theme was not compatible for dialog boxes.
I ran into this error in a different situation, and it turned out that I'd accidentally set a drawable to R.id.something instead of R.drawable.something!
Make sure you followed the above solutions, try this if non-of-them worked for you.
In my case, the problem was due to drawable icons in only hdpi image resolution. Change these icons to the folder, which contains xhdpi, mdpi, xxhdpi and xxxhdpi resolution also.
And that solved my problem.
PS:
I convert icons to different image resolution by installing plugin "Android drawable importer".
Maybe your view background resource not in drawable dir, in color. view background not support resource in color.
view background like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<solid android:color="#ffffffff"/>
</shape>
</item>
<item android:state_selected="false">
<shape>
<solid android:color="#00000000"/>
</shape>
</item>
</selector>
You can get it working by making sure that your drawable.xml not included (v24)
<ImageView
...
android:src="#drawable/realtconsult" />
If you will write it from a view then this will works.
There are more than one folder included in drawable folders(not visible), if you get these kind of error then please copy paste the file on which error occured to all the directories or atleast copy paste to night and normal drwable folder
enter image description here
When you put an image into the drawable folder, by default they are -v24 named at the end.
If you are not sure, just drag and drop the image again into the drawable folder. Make sure the name is not -v24 at the end.
Solution for old APIs
move all your ic_xyz.xml folders to drawable (v24) ie select your folder which is having this issue and move it to or select---> refactor--->move file-->(change the path) to Drawable(v24).

How to use custom font in a project written in Android Studio

I was trying to use custom font in Android Studio as we did in Eclipse. But unfortunately could not figure out where to put the 'assets' folder!
Update 2021:
Create a folder named font inside the res folder and copy your font
All font names must be only: lowercase a-z, 0-9, or underscore.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/abc_font" />
For programmatic use:
textView.setTypeface(ResourcesCompat.getFont(context, R.font.abc_font))
For Android Studio 4.2+ there's even now a menu option:
Select File>New>Folder>Assets Folder
Click finish
Right click on assets and create a folder called fonts
Put your font file in assets > fonts
Use code below to change your textView's font
TextView textView = (TextView) findViewById(R.id.textView);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/yourfont.ttf");
textView.setTypeface(typeface);
There are many ways to set custom font family on field and I am using like that below.
To add fonts as resources, perform the following steps in the Android Studio:
1) Right-click the res folder and go to New > Android resource directory. The New Resource Directory window appears.
2) In the Resource type list, select font, and then click OK.
Note: The name of the resource directory must be font.
3) Add your font files in the font folder.
Add font in desired view in your xml file:
Note: But you required the following things for that:
Android Studio above to 3.0 canary.
Your Activity extends AppCompatActivity.
Update your Gradle file like that:
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildtoolsVersion above to 26 and minimum targetSdkVersion required 26
Add dependencies in build.gradle file:
classpath 'com.android.tools.build:gradle:3.0.0-beta4'
gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
I think instead of downloading .ttf file we can use Google fonts. It's very easy to implements. only you have to follow these steps.
step 1) Open layout.xml of your project and the select font family of text view in attributes (for reference screen shot is attached)
step 2) The in font family select More fonts.. option if your font is not there. then you will see a new window will open, there you can type your required font & select the desired font from that list i.e) Regular, Bold, Italic etc.. as shown in below image.
step 3) Then you will observe a font folder will be auto generated in /res folder having your selected fonts xml file.
Then you can directly use this font family in xml as
android:fontFamily="#font/josefin_sans_bold"
or pro grammatically you can achieve this by using
Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);
fontText.setTypeface(typeface);
Hello here we have a better way to apply fonts on EditTexts and TextViews on android at once and apply it in whole project.
First of All you need to make fonts folder. Here are Steps.
1: Go to the (project folder) Then app>src>main
2: Create folders named 'assets/fonts' into the main folder.
3: Put your fonts into the fonts folder. Here I Have 'MavenPro-Regular.ttf'
Here are the Steps for applying custom fonts on EditText and using this approach you can apply fonts on every input.
1 : Create a Class MyEditText (your preferred name ...)
2 : which extends EditText
3 : Apply your font
Here is code Example;
public class MyEditText extends EditText {
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyEditText(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/MavenPro-Regular.ttf");
setTypeface(tf);
}
}
}
And in Here is the code how to use it.
MyEditText editText = (MyEditText) findViewById(R.id.editText);
editText.setText("Hello");
Or in Your xml File
<MyEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#fff"
android:textSize="16dp"
android:id="#+id/editText"
/>
With Support Library 26.0 (and Android O) fonts can be loaded from resource easily with:
Typeface typeface = ResourcesCompat.getFont(Context context, int fontResourceId)
Docs for the method.
More info can be found here.
I want to add my answer for Android-O and Android Studio 2.4
Create folder called font under res folder. Download the various fonts you wanted to add to your project example Google fonts
Inside your xml user font family
example :
<TextView
android:fontFamily="#font/indie_flower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/sample_text" />
3.If you want it to be in programmatic way use following code
Typeface typeface = getResources().getFont(R.font.indie_flower);
textView.setTypeface(typeface);
for more information follow the link to my blog post Font styles for Android with Android Studio 2.4
As per new feature available in Android O, font resources in XML is avilable as new feature.
To add fonts as resources, perform the following steps in the Android Studio:
1) Right-click the res folder and go to New > Android resource directory. The New Resource Directory window appears.
2) In the Resource type list, select font, and then click OK.
Note: The name of the resource directory must be font.
3) Add your font files in the font folder.
You can access the font resources with the help of a new resource type, font. For example, to access a font resource, use #font/myfont, or R.font.myfont.
eg. Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);
You can use easy & simple EasyFonts third party library to set variety of custom font to your TextView. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation. You will be free from creating asset folder too.
Simply:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
There are many type of fonts provided by this library.
Create folder assets in Project -> app (or your app name) -> src -> main -> right click -> New -> Directory.
Then create a new directory inside assets called "fonts".
To assign the font to the textView:
TextView textView = (TextView) findViewById(R.id.your_textView);
final Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/your_font_name");
your_font_name includes font extension.
1st add font.ttf file on font Folder. Then Add this line in onCreate method
Typeface typeface = ResourcesCompat.getFont(getApplicationContext(), R.font.myfont);
mytextView.setTypeface(typeface);
And here is my xml
<TextView
android:id="#+id/idtext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:gravity="center"
android:text="My Text"
android:textColor="#000"
android:textSize="10sp"
/>
To use the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher, use the Support Library 26. For more information on using the support library, refer to the Using the support library section.
To add fonts as resources, perform the following steps in the Android Studio:
Right-click the res folder and go to New > Android resource directory.
The New Resource Directory window appears.
In the Resource type list, select font, and then click OK.
Add your font files in the font folder.
Creating a font family
To create a font family, perform the following steps in the Android Studio:
Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
Enter the file name, and then click OK. The new font resource XML opens in the editor.
Enclose each font file, style, and weight attribute in the element. The following XML illustrates adding font-related attributes in the font resource XML:
If your minSdkVersion is API level 26 and higher
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="#font/lobster_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="#font/lobster_italic" />
</font-family>
and if your minSdkVersion is lower than API level 26
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:font="#font/lobster_italic"
app:fontStyle="normal"
app:fontWeight="400" />
</font-family>
Then you can use it any where like this
android:fontFamily="#font/your_custom_font_file"
Adding font to your project
To add fonts as resources, perform the following steps in the Android Studio:
1 - Right-click the res folder and go to New > Android resource directory.
The New Resource Directory window appears.
2 - In the Resource type list, select font, and then click OK.
3 - Add your font files in the font folder just by a simple copy and paste. note that the name of the fonts should be in lowercase.
Using fonts in XML layouts
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/lobster"/>
Adding fonts to style
<style name="customfontstyle" parent="#android:style/TextAppearance.Small">
<item name="android:fontFamily">#font/lobster</item>
</style>
Using fonts programmatically
Kotlin:
val typeface = resources.getFont(R.font.myfont)
textView.typeface = typeface
JAVA:
Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);
If you are very new to Android like I am this can be a little tricky. Make sure you call:
TextView myTextView = (TextView) findViewById(R.id.textView);
Typeface typeface=Typeface.createFromAsset(getAssets(), "fonts/your font.ttf");
myTextView.setTypeface(typeface);
method within a method such as onCreate.
Android 8.0 (API 26) introduced new features related to fonts.
1) Fonts can be used as resources.
2) Downloadable fonts.
If you want to use external fonts in your android application, you can either include font files in apk or configure downloadable fonts.
Including font files in APK : You can download font files, save them in res/font filer, define font family and use font family in styles.
For more details on using custom fonts as resources see http://www.zoftino.com/android-using-custom-fonts
Configuring downloadable fonts : Define font by providing font provider details, add font provider certificate and use font in styles.
For more details on downloadable fonts see http://www.zoftino.com/downloading-fonts-android
I could not load fonts because I had named my font file Poppins-Medium.tff, renaming it to poppins_medium.tff worked for me.
Rest of the steps remained the same:
Create the font resource directory under res folder
Copy and paste your tff file in that directory
Then use in TextView in XML using fontFamily attribute.
If above steps dont work, you can create a FontFamily of that font using this link
First create assets folder then create fonts folder in it.
Then you can set font from assets or directory like bellow :
public class FontSampler extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.custom);
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
tv.setTypeface(face);
File font = new File(Environment.getExternalStorageDirectory(), "MgOpenCosmeticaBold.ttf");
if (font.exists()) {
tv = (TextView) findViewById(R.id.file);
face = Typeface.createFromFile(font);
tv.setTypeface(face);
} else {
findViewById(R.id.filerow).setVisibility(View.GONE);
}
}
}
Add your fonts to assets folder in app/src/main/assets
make a custom textview like this :
class CustomLightTextView : TextView {
constructor(context: Context) : super(context){
attachFont(context)
}
constructor(context: Context, attrs: AttributeSet): super(context, attrs){
attachFont(context)
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
attachFont(context)
}
fun attachFont(context: Context) {
this.setTypeface(FontCache.getInstance().getLightFont(context))
}
}
Add FontCache : So that you don't have to create typeface again and again like :
class FontCache private constructor(){
val fontMap = HashMap<String,Typeface>()
companion object {
private var mInstance : FontCache?=null
fun getInstance():FontCache = mInstance?: synchronized(this){
return mInstance?:FontCache().also { mInstance=it }
}
}
fun getLightFont(context: Context):Typeface?{
if(!fontMap.containsKey("light")){
Typeface.createFromAsset(context.getAssets(),"Gotham-Book.otf");
fontMap.put("light",Typeface.createFromAsset(context.getAssets(),"Gotham-Book.otf"))
}
return fontMap.get("light")
}
}
And you are done !
P.S.From android O, you can directly add fonts.
Now there are so many ways to apply font one of easiest way is like this,
1) Right-click the res folder
go to New > Android resource directory.
2) From Resource type list, select font, and then click OK.
3) Put your font files in the font folder.
put fonts in asset folder
then apply fontfamily:''your fonts
Kotlin Answer
If you need to use fonts in code side, you can use this function also it has version code control.
fun getFontJarvisWhite(): Typeface {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) resources.getFont(R.font.jarvis_white)
else context?.let { ResourcesCompat.getFont(it, R.font.jarvis_white) }!!
}
For new readers
You can use this library
Gloxey Custom Font Views
gradle dependency
dependencies{
compile 'io.gloxey.cfv:custom-font-views:1.0.2'
}
How to use?
Create folder assets -> fonts. Copy your fonts into fonts folder.
Use property app : font_name = "font_name_string" to apply font on view.
Example
<!--Font Names in srings.xml-->
<string name="aadhunik">aadhunik.ttf</string>
<string name="kung_fool">kungfool.ttf</string>
<string name="skrova">skrova.otf</string>
<string name="painting_in_the_sun_light">painting_in_the_sun_light.ttf</string>
<!--Include views in layout.xml-->
<io.gloxey.cfv.CFTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Aadhunik"
android:textColor="#ff00"
android:textSize="40sp"
app:font_name="#string/aadhunik" />
<io.gloxey.cfv.CFButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Kung Fool"
android:textColor="#154748"
app:font_name="#string/kung_fool" />
<io.gloxey.cfv.CFEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello world"
android:textSize="30sp"
app:font_name="#string/skrova" />
<io.gloxey.cfv.CFCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Painting In The Sun Light"
android:textSize="30sp"
app:font_name="#string/painting_in_the_sun_light" />

Categories

Resources