You may have problem with width of images from img-tags.
If the image is wider than the screen, it will be ugly. I can help you.
If the image is need to be scaled welcome too.
We have image in drawable resources named drawable_name.png and string in string.xml:
<string name="text_with_html_and_img"><![CDATA[ <h3>Hello, bro!</h3><img src="drawable_name"> ]]></string>
Here the code from activity:
String texts = getString(R.string.text_with_html_and_img); TextView textView = findById(this, R.id.text_view_id); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { textView.setText(Html.fromHtml(texts, Html.FROM_HTML_MODE_LEGACY, new ResourceImageGetter(this), null)); } else { textView.setText(Html.fromHtml(texts, new ResourceImageGetter(this), null)); }
Here our magic class (i realy like it):
public static class ResourceImageGetter implements Html.ImageGetter { private Context mContext; public ResourceImageGetter(Context context) { mContext = context; } @Override public Drawable getDrawable(String source) { int mult = 1; if (source.contains("/")) { mult = Integer.valueOf(source.substring(source.indexOf('/') + 1)); source = source.substring(0, source.indexOf('/')); } Resources resources = mContext.getResources(); int identifier = resources.getIdentifier(source, "drawable", mContext.getPackageName()); Drawable drawable = ContextCompat.getDrawable(mContext, identifier); DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); int widthDisplay = displayMetrics.widthPixels; int widthDrawable = drawable.getIntrinsicWidth(); if (widthDisplay < widthDrawable) drawable.setBounds(0, 0, widthDisplay, (widthDisplay * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth())); else { int heightDrawable = drawable.getIntrinsicHeight(); drawable.setBounds(0, 0, widthDrawable / mult, heightDrawable / mult); } return drawable; } }
If you want reduce the width and height of the image proportionally use this example
<string name="text_with_html_and_img"><![CDATA[ <h3>Hello, bro!</h3><img src="drawable_name/4"> ]]></string>