ImageSpan not working on Android 5

I have this feature that works great on Android 4.4.1, but breaks on 5.0+.

public static SpannableStringBuilder prependImage(Drawable drawable, String text) { SpannableStringBuilder builder = new SpannableStringBuilder(" " + text); builder.setSpan(new ImageSpan(drawable), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return builder; } 

And I use it as follows:

 class MyButton extends Button { // ... snip ... setText( prependImage( getDrawable(imageResource, color), getContext().getString(stringResource)), BufferType.SPANNABLE); 

The following is the getDrawable() method above:

  private Drawable getDrawable(int resource, int color) { final Resources resources = getContext().getResources(); Drawable drawable = resources.getDrawable(resource); if (drawable != null) { drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); } return drawable; } 

When I debug everything seems successful, but the image is not drawn. Any ideas what I can do wrong?

+7
android
Aug 27 '15 at 19:08
source share
4 answers

Your Spannables code is fine. You can verify this by setting the text for the TextView.

The problem is the material design of the button on Android 5.0.

 <style name="Widget.Material.Button"> <item name="background">@drawable/btn_default_material</item> <item name="textAppearance">?attr/textAppearanceButton</item> <item name="minHeight">48dip</item> <item name="minWidth">88dip</item> <item name="stateListAnimator">@anim/button_state_list_anim_material</item> <item name="focusable">true</item> <item name="clickable">true</item> <item name="gravity">center_vertical|center_horizontal</item> </style> 

There are two solutions.

The first is just a TextView as your button and setText with an image on it.

For another (and maybe more correct) you need to expand the button style ( Widget.Material.Button ) as follows:

 <style name="BtnStyle" parent="android:Widget.Material.Button"> <item name="android:textAppearance">@null</item> </style> 

Then in your layout:

 <Button android:id="@+id/test2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Test" style="@style/BtnStyle"/> 

After that, you will see the images in the button.

Do not forget that for the version of Android that is lower than 5.0, you must create BtnStyle too, but in a different resource directory (res / values-v14 / style.xml).

+3
Sep 30 '15 at 7:42
source share

By default, the Material buttons are styled to display text in all caps. However, there is an error in the AllCapsTransformationMethod method used for capitalization, which causes it to discard Spannable data.

You can override the default button style and disable all caps by specifying android: textAllCaps = "false" on your button.

 <Button ... android:textAllCaps="false" /> 

look here

+10
Sep 30 '15 at 13:19
source share

Perhaps you need to check the contents of this Drawable, you use getDrawable () to get the Drawable, but the API definition does not seem to match your call parameters.

For Android 5.0+

Drawable getDrawable (int id) This method is deprecated at API level 22. Instead, use getDrawable (int, Theme).

Drawable getDrawable (int id, Resources.Theme theme) Returns the return object associated with a specific resource identifier and designed for the specified theme.

The second parameter looks like a theme, not a color. right?

0
Sep 30 '15 at 1:46
source share

try it

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getResources().getDrawable(R.drawable.your_drawable, getTheme()); } else { getResources(). getDrawable(R.drawable.your_drawable); } 
0
Sep 30 '15 at 6:54
source share



All Articles