Show image in TextView

I want to show images in a TextView. My images are saved in the res / raw directory. I tried using HTML.ImageGetter but could not find the full link for this.

+6
source share
3 answers
import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Html; import android.text.Html.ImageGetter; import android.widget.TextView; public class TextImageActivity extends Activity { int imageNumber = 1; //int to check which image is displayed @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tvText = (TextView) findViewById(R.id.text); final String testContent = "<html><body><b>Test</b><i>Italic</i><br/>" + "<img src=\"icon.png\"/>This is like testing if this thing works" + "<img src=\"a.png\"/>" + " in a more elaborate</body></html>"; tvText.setText(Html.fromHtml(testContent, imgGetter, null)); } private ImageGetter imgGetter = new ImageGetter() { public Drawable getDrawable(String source) { Drawable drawable = null; if(imageNumber == 1) { drawable = getResources().getDrawable(R.raw.icon); ++imageNumber; } else drawable = getResources().getDrawable(R.raw.a); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable .getIntrinsicHeight()); return drawable; } }; } 
+14
source

Try it, so you don’t have to hack using TextView. This will allow you to take into account an arbitrary number of images marked in the source line. it will also allow contiguous images, as well as allow blank lines leading or ending text lines.

 LinearLayout parent = //get parent layout String input = //get input LayoutInflater inflater = this.getLayoutInflater(); int imageIndex = input.indexOf("<image tag begin"); int endIndex = -1; //find index one past the end of the image tag while (imageIndex > -1){ TextView tv = inflater.inflate(R.layout.my_textview, null); tv.setText(input.substring(0, imageIndex)); ImageView iv = inflater.inflate(R.layout.my_imageview, null); String imgTag = input.substring(imageIndex,endIndex); iv.setImageResource(-1); //or however you set your image from the tag parent.addView(tv); parent.addView(iv); input = input.substring(endIndex); } 

This should dynamically create all the images you need and analyze your inputs accordingly. if you want to keep the original rows in the database, this is probably good.

Saving text in text images and images in the image view will make your life a lot easier if you need to set styles / change attributes to whatever you want. Sibling views are generally better than cracking the wrong kind of information / too much information in one layout element.

BTW, I have inflated custom XML layouts for viewing images and text comments, just in case you want to provide some custom style settings in all their instances. You can just generate normal Android text and text images if they are sufficient.

This was my initial decision, which only works with maximum maximum image.

 String input = //whatever boolean hasImage = //find image tag int imageBegin = //find index where image tag begins int imageEnd = //find index one past where image tag ends String leadingText = input.substring(0,imageBegin); String imageTag = input.substring(imageBegin, imageEnd); String trailingText = input.substring(imageEnd, input.length()); ((TextView)findViewById(R.id.leading_textview)).setText(leadingText); if (hasImage){ //use imageTag to set content of Image View) findViewById(R.id.image_view_id).setVisibility(View.VISIBLE); ((TextView)findViewById(R.id.trailing_textview)).setText(trailingText); } 

If you can have more than one inline image, you will have to dynamically create ImageViews and terminate the TextViews and add them to the parent of the corresponding layout.

+3
source

u can use this: setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)

follow this example u can do the same for textview

0
source

Source: https://habr.com/ru/post/901812/


All Articles