Android VectorDrawable as Compound Drawables - android

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/>

Related

getCompoundDrawables returns null inside of onCreateView, but not later

I want to run the code below in order to tint a button's drawable on pre-lollipop devices, however button.getCompoundDrawables() is returning null for all 4 elements of the array when called inside of the Fragment's onCreateView method.
If I inspect the same Drawable[] array at a later point in time - say upon a button click event - I can see the drawable value has been correctly assigned (3 are null, 1 is valid).
Is there some button life cycle or fragment life cycle that I can rely on the compound drawables array to have been already properly initialized?
Drawable[] drawables = button.getCompoundDrawables();
if( drawables[2] != null){
Drawable wrapDrawable = DrawableCompat.wrap(drawables[2]);
DrawableCompat.setTint(wrapDrawable, color);
button.invalidate();
}
Here's the lib versions I'm using:
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:support-v4:24.1.1'
compile 'com.android.support:design:24.2.0'
At request, I'm including also some xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
[...] >
<Button
android:id="#+id/bt1"
android:background="#android:color/transparent"
android:textAppearance="#style/ConfigButtonTheme"
android:text="Sincronizar Música"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:drawableEnd="#drawable/ic_chevron_right_white_24dp"
android:textAlignment="textStart"
android:layout_width="match_parent"
android:layout_height="60dp" />
</LinearLayout>
for android:drawableRight, you should use getCompoundDrawables(), where as for android:drawableEnd, you should use getCompoundDrawablesRelative().
getCompoundDrawablesRelative()
Change android:drawableEnd to android:drawableRight. Not sure why but drawableEnd returns null in onCreate() method and drawableRight works fine.
You could configure the drawable programmatically and then set it into the text view like so.
val textDrawable = resources.getDrawable(R.drawable.ic_arrow_upward_24dp, null)
val color = ResourcesCompat.getColor(resources, R.color.colorAccent, null)
textDrawable.setTint(color)
//setCompoundDrawablesRelativeWithIntrinsicBounds(left, top, right, bottom)
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, textDrawable, null, null)
Change android:drawableEnd to android:drawableRight. Not sure why but drawableEnd returns null in onCreate() method and drawableRight works fine.
OR
Another way to do without changing android:drawableEnd to android:drawableRight.
It will work 100%
just write your code as follow:
onCreate(){
//your all statement
//at the end
findViewById(android.R.id.content).post(new Runnable() {
#Override
public void run() {
//write your code here you will get all the drawables
}
});
}
In Kotlin returns a list of drawables:
val drawables = (compoundDrawables zip compoundDrawablesRelative).map {
it.first ?: it.second
}
My guess is that the drawable hasn't been created/inflated yet. Try putting that code in either onActivityCreated, onStart or onResume within the Fragment. These are in order of when they will be called within the lifecycle, ideally you want to do this as soon as possible.
It doesn't load your drawables within TextView at the beginning. You should use
TextView.post({
// get your drawables here.
})
this function to get your drawables when it's loaded.

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.

Change the Color of ScrollView Programmatically

What I'm currently doing
Currently, I have changed the scrollbar in my XML file using the android:scrollbarThumbVertical property like so:
<ScrollView
android:id="#+id/scrollView1"
android:scrollbarThumbVertical="#drawable/scrollbar_blue"
... >
And scrollbar_blue refers to my scrollbar_blue.xml file, which is this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="45"
android:centerColor="#color/blue"
android:endColor="#color/blue"
android:startColor="#color/blue" />
<corners android:radius="8dp" />
</shape>
What I want to do
I have a colour option for my app - so when the colour is on, it should stay blue; otherwise, it should be grey.
How can I programmatically (in my activity class) change my ScrollView to use my scrollbar_grey.xml?
If you look at the Android documentation on ScrollView, there is no corresponding method to android:scrollbarThumbVertical
I'm fine with another way change the colour as well.
Here is how I create the reference to my ScrollView:
ScrollView scr = (ScrollView)findViewById(R.id.scrollView1);
There is a method to change it programmatically but that method is not exposed. There doesn't seem to be anything else to change it programmatically from what I have read.
However, I did come across this one stackoverflow answer that uses reflection to do it.
Please upvote the answer there if it works for you: https://stackoverflow.com/a/19819843/3286163
The answer was for a listview but is the same for the scrollview:
ScrollView scr = (ScrollView)findViewById(R.id.scrollView1);
try
{
Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
mScrollCacheField.setAccessible(true);
Object mScrollCache = mScrollCacheField.get(scr); // scr is your Scroll View
Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
scrollBarField.setAccessible(true);
Object scrollBar = scrollBarField.get(mScrollCache);
Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
method.setAccessible(true);
// Set your drawable here.
method.invoke(scrollBar, getResources().getDrawable(R.drawable.scrollbar_blue));
}
catch(Exception e)
{
e.printStackTrace();
}
Only thing I could find. I gave it a try myself and it worked.
In API 29+ use ScrollView.setVerticalScrollbarThumbDrawable() otherwise use the accepted answer.
It is easy nowadays :)
scrollView.verticalScrollbarThumbDrawable = ColorDrawable(Color.CYAN)
scrollView.horizontalScrollbarThumbDrawable = ColorDrawable(Color.WHITE)
This method requires API 29 and higher :
public static void changeBarColor(ScrollView sv, int thumbColor,int trackColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
sv.getVerticalScrollbarThumbDrawable().setTint(thumbColor);
sv.getVerticalScrollbarTrackDrawable().setTint(trackColor);
}
}

svg-android imageview not working

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.

NotFoundException when looking for Drawable resource

I am starting with android and I want to add a border to cells as described in this answer. So I created my cell_background.xml file, which Eclipse created in res\drawable and that contains
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="#000"/>
<stroke android:width="1dp" android:color="#ff9"/>
</shape>
Having read that there are several issues with the drawable folder, I copied it verbatim into the res\drawable-*dpi directories
Now, my app crashes in the following line
Drawable drawable = Resources.getSystem().getDrawable(R.drawable.cell_background);
with this exception
12-16 14:26:28.624: E/AndroidRuntime(533): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020000
Both the project and the emulator are set to v3.0
Any ideas? I have already cleaned and rebuilt the project but it still crashes.
The problem is that you use Resources.getSystem(), which will give you a reference to the system resources. You should use context.getResources() instead.
Try with the following code to check whether the resource exists or not
int drawRes = getDrawableResourceID(context, "cell_background"));
if(drawRes>0){
getResources().getDrawable(drawRes);
}
//To detect whether the reource exits in drawable or not
public static int getDrawableResourceID(Context context,
String identifierName) {
return context.getResources().getIdentifier(identifierName,
"drawable", context.getPackageName());
}
not sure about issue regarding putting in Drawable folder only i haven't got issue any, Still try using this way tht i use generally
Drawable drawable = getResources().getDrawable(R.drawable.cell_background);

Categories

Resources