ColorDrawable.getColor() will only work with API levels above 11, so you can use this code to support it from the start. I use reflection below API level 11.
public static int getBackgroundColor(TextView textView) { Drawable drawable = textView.getBackground(); if (drawable instanceof ColorDrawable) { ColorDrawable colorDrawable = (ColorDrawable) drawable; if (Build.VERSION.SDK_INT >= 11) { return colorDrawable.getColor(); } try { Field field = colorDrawable.getClass().getDeclaredField("mState"); field.setAccessible(true); Object object = field.get(colorDrawable); field = object.getClass().getDeclaredField("mUseColor"); field.setAccessible(true); return field.getInt(object); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return 0; }
Akhil dad
source share