Search code examples
javaandroidimagespan

set imagespan in the next line of text and in the center


I want to add an image to the edit text and I am successful in adding an image. Now I want this image in the next line and in the center. I want my image to be inserted in the center of the horizontal layout and not to one side. I searched a lot of posts but could not find my answer. Here are two images what my results are now and what I want in reality.

enter image description here

and this I want

enter image description here

my code for Imagespan is

private void setImageinText(Bitmap myBitmap){
   myBitmap = scaleDown(myBitmap, 1360, true);
    imageView = new ImageView(context);
    imageView.setImageBitmap(myBitmap);
    Drawable d = imageView.getDrawable();
   SpannableString ssd = new SpannableString("abcde");
   d.setBounds(0, 0, d.getIntrinsicWidth()/2, d.getIntrinsicHeight()/2);
   ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
   ssd.setSpan(span, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
   texto.setTransformationMethod(null);
   texto.getText().insert(texto.getSelectionStart(), ssd);
}

Solution

  • add this to new spannableString for lines.

    SpannableString ssd = new SpannableString("\n a \n");
    

    and then set span

    ssd.setSpan(span, 1 , 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    

    for the second part of the question Image in the centre, Let's wait for others to post. I am not sure how to do the other part.