Picasso image loading library shows error while use with:
ColorDrawable cd = new ColorDrawable(ContextCompat.getColor(mContext,R.color.light_thirty_alpha));
Picasso.with(mContext)
.load(cd)
.fit().centerCrop()
.transform(new RoundedTransformation(5, 0))
.error(cd)
.placeholder(cd)
.into(imageView_ovelay_slider);
It is showing error that cannot resolve method .load(colorDrawable). I already knows that it is not accept colorDrawable but I am asking can we cast colorDrawable to something else that accepts by .load().
If you look at the source code, you will see that, Picasso's load method can accept any one of the following parameters:
Uri
String
File
int (Resource Id)
It does not accept ColorDrawable
.load method doesn't accepts ColorDrawable as parameter. Look at the official documentation for accepted parameters.
load(File file) - image request using the specified image file.
load(int resourceId) - mage request using the specified drawable resource ID.
load(String path) - image request using the specified path.
load(android.net.Uri uri) - image request using the specified URI.
http://square.github.io/picasso/2.x/picasso/
Solution was so easy finally found by using
solution 1(worked & tested):
ColorDrawable cd = new ColorDrawable(ContextCompat.getColor(mContext,R.color.light_thirty_alpha));
Picasso.with(mContext)
.load(String.valueOf(cd))
.fit().centerCrop()
.transform(new RoundedTransformation(5, 0))
.error(cd)
.placeholder(cd)
.into(imageView_ovelay_slider);
solution 2(worked & tested):
alternate way i found is to create shape drwable.xml file with radius.
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape android:shape="rectangle">
<stroke
android:width="0dp"
android:height="0dp"
android:color="#color/transparent" />
<!-- apply button background transparent, full opacity -->
<solid android:color="#color/box" />
<corners android:radius="2.5dp" />
<padding android:bottom="2dp"
android:left="4dp"
android:right="4dp"
android:top="2dp" />
</shape>
</item>
and use it as background drawable. Thank you #Prerak Sola & Alok
Related
I've an app with over 5000 downloads and more than 500 users active daily and out of nowhere I started get an weird error on Crashlytics:
Caused by android.content.res.Resources$NotFoundException: File res/drawable-anydpi-v24/ic_tickets.xml from drawable resource ID #0x7f0700b3
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2748)
at android.content.res.Resources.loadDrawable(Resources.java:2643)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.widget.TextView.<init>(TextView.java:921)
at android.widget.TextView.<init>(TextView.java:703)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:76)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:72)
at java.lang.reflect.Constructor.newInstance(Constructor.java)
Caused by android.content.res.Resources$NotFoundException: File res/drawable-anydpi-v24/$ic_tickets__0.xml from color state list resource ID #0x7f070017
at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2858)
at android.content.res.Resources.loadColorStateList(Resources.java:2807)
at android.content.res.TypedArray.getColor(TypedArray.java:439)
at android.graphics.drawable.VectorDrawable$VFullPath.updateStateFromTypedArray(VectorDrawable.java:1605)
at android.graphics.drawable.VectorDrawable$VFullPath.inflate(VectorDrawable.java:1584)
at android.graphics.drawable.VectorDrawable.inflateInternal(VectorDrawable.java:666)
at android.graphics.drawable.VectorDrawable.inflate(VectorDrawable.java:571)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1215)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1124)
Caused by org.xmlpull.v1.XmlPullParserException: Binary XML file line #6: invalid color state list tag gradient
at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:217)
at android.content.res.ColorStateList.createFromXml(ColorStateList.java:201)
at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2854)
at android.content.res.Resources.loadColorStateList(Resources.java:2807)
at android.content.res.TypedArray.getColor(TypedArray.java:439)
at android.graphics.drawable.VectorDrawable$VFullPath.updateStateFromTypedArray(VectorDrawable.java:1605)
The stack trace is way longer than this, more than 500 lines on Crashlytics, I pasted here what I thought might be useful to understand the problem.
If I got it right it is complaining like one of my resources is missing or corrupted; what could it be?
But the resource is intact in the apk:
<vector android:height="24dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="24dp"
xmlns:aapt="http://schemas.android.com/aapt" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:pathData="M381.845,292.27c0,11.032 -8.943,19.975 -19.975,19.975s-19.975,-8.943 -19.975,-19.975c0,-11.032 8.943,-19.975 19.975,-19.975S381.845,281.238 381.845,292.27zM361.869,332.221c-11.032,0 -19.975,8.943 -19.975,19.975s8.943,19.975 19.975,19.975s19.975,-8.943 19.975,-19.975S372.902,332.221 361.869,332.221zM361.869,392.147c-11.032,0 -19.975,8.943 -19.975,19.975s8.943,19.975 19.975,19.975s19.975,-8.943 19.975,-19.975S372.902,392.147 361.869,392.147zM307.418,136.36c-9.983,4.696 -14.268,16.596 -9.572,26.578c4.696,9.983 16.596,14.268 26.578,9.572c9.983,-4.696 14.268,-16.596 9.572,-26.578C329.3,135.95 317.401,131.664 307.418,136.36zM451.759,352.197c0,22.029 17.922,39.951 39.951,39.951h19.975V512H0.314V392.147H20.29c22.029,0 39.951,-17.922 39.951,-39.951s-17.922,-39.951 -39.951,-39.951H0.314V192.393h0.509l-0.042,-0.089L408.489,0l50.866,108.128l-17.013,8.861c-13.29,6.921 -21.544,20.506 -21.544,35.453c0,22.029 17.922,39.951 39.951,39.951h50.937v119.853H491.71C469.681,312.246 451.759,330.168 451.759,352.197zM94.244,192.393h297.352c-6.822,-11.763 -10.75,-25.403 -10.75,-39.951c0,-23.307 10.023,-44.953 27.021,-59.913l-18.505,-39.335l-81.134,38.269c4.355,9.886 0.068,21.491 -9.762,26.115c-9.818,4.618 -21.473,0.536 -26.323,-9.095L94.244,192.393zM411.808,352.197c0,-37.163 25.503,-68.482 59.926,-77.374v-42.479h-89.889c0,11.032 -8.943,19.975 -19.975,19.975s-19.975,-8.943 -19.975,-19.975H40.265v42.479c34.424,8.891 59.926,40.211 59.926,77.374s-25.503,68.482 -59.926,77.374v42.479h301.629c0,-11.032 8.943,-19.975 19.975,-19.975s19.975,8.943 19.975,19.975h89.889V429.57C437.31,420.678 411.808,389.36 411.808,352.197z">
<aapt:attr name="android:fillColor">
<gradient android:endX="256.00745" android:endY="511.99982"
android:startX="256.00745"
android:startY="-0.0150070675" android:type="linear">
<item android:color="#FF2AF598" android:offset="0"/>
<item android:color="#FF009EFD" android:offset="1"/>
</gradient>
</aapt:attr>
</path>
</vector>
The drawable is a very small and simple icon for a button in the app, I downloaded it from a icon library it opens and run fine on all my tests.
The second weird thing about this is that over 500 active users I had today this is happening only with 4 users all with the same smartphone:
Device
Brand: LGE
Model: LG K10 LTE
Orientation: Portrait
RAM free: 170.92 MB
Disk free: 574.64 MB
Operating System
Version: 6.0
Orientation: Portrait
Rooted: No
Crash
Is there any know issue about this smartphone model? What can I do in order to fix this for the affected users?
This is because you use a few attributes not supported prior to API level 24.
eg. startX, endX, startY, endY and offset.
Android Marshmallow 6.0 is API level 23 ...
That invalid color state list tag gradient is coming from the offset:
<item android:color="#FF2AF598" android:offset="0"/>
<item android:color="#FF009EFD" android:offset="1"/>
This issue most likely isn't LG specific, but Marshmallow specific.
Use app:srcCompat instead of src on the ImageView
In your app build.gradle write below code :
android.defaultConfig.vectorDrawables.useSupportLibrary = true
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
...
}
Now you see it works.
The actual problem is with attributes you are defining like android:fillColor
which expects to be a single value color and not a list of colors like gradient or color list. It works from API 24. My suggestion is to tell your designer to create exact logo for below 24 API
Refer to the link for more details
Instead of using .xml to set the image resource, just set the image resource programmatically from your java code.
Faced this issue in Android API 21-23 with the vector drawable. By using this, now its fixed
app:drawableStartCompat="#drawable/ic_placeholder"
and by programmatically:
Drawable img = button.getContext().getResources().getDrawable(
R.drawable.ic_brush );
button.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
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/>
How to dynamically create a user picture from the first letter of his user name like Gmail App on android does?
I'm working under Django and Bootstrap, But i'm open for any solution in any language or framwork.
Now I'm managing user profile by using a default picture for new users
and letting them uploading their own profile picture if they want to.
class Profile(models.Model):
user = models.OneToOneField(User)
....
def get_picture(self):
no_picture = settings.STATIC_URL + 'img/user.png'
try:
filename = settings.MEDIA_ROOT + '/profile_pictures/' + self.user.username + '.jpg'
picture_url = settings.MEDIA_URL + 'profile_pictures/' + self.user.username + '.jpg'
if os.path.isfile(filename):
return picture_url
else:
return no_picture
except Exception, e:
return no_picture
.....
PS: I can do that by uploading 26 picture with different Letters, but here i want a dynamic solution for some reasons
For context,
I do not believe the Gmail Android app is actually generating an image. This effect can be achieved in Android with the following shape as a drawable background:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/listview_background_shape">
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<solid android:color="#f5d2c3" />
<size android:width="75dp"
android:height="75dp" />
</shape>
And apply it to a textview:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:background="#drawable/square_background"
android:textSize="40dp"
android:text="J" />
A better solution would be for you to just center text in a div with a background applied. This is much easier then generating an image for something so simple.
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);
}
}
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);