Android Add image to text (in text View)?
Asked Answered
L

5

21

first post here=) I've been looking for this answer and haven't been able to find it. What I want to do is have some text, then add a image, then rest of text. For example:

                             ____
                            |   |
Hi there, this is the photo |___|, hope you like it..

I've been looking but all I can find is add text to image or add image to image View, and I don't think that's what I want because the app is mainly text but with images on it.

So my question is: How do I add an Image to text?

thanks


UPDATE: I used the advice R.daneel.olivaw gave me and it worked nice=)

But I have a problem. lets say i have: "a b c" where I set the position of b as spanable. But if I try to delete the text and I delete b, the next time I write something it will become the image I used in spanable. How do I correct this? Any1one got any advice? thanks=)

Legality answered 21/1, 2012 at 13:32 Comment(0)
R
26

I think you are looking for the Spannable interface, By using this you can add images to a text view.

This link might help.

enter image description here

Restorative answered 21/1, 2012 at 13:50 Comment(5)
this seams to work.awsome,i'll keep trying it and see if it does what I want=)Legality
Hello, Did it turn out to be useful for you?Restorative
yes,totally worked.Thanks. But I have a problem. lets say i have: "a b c" where I set the position of b as spanable. but if I try to delete the text and I delete b the next time I write something it will become the image I used in spanable. How do I correct this?Legality
new ImageSpan( smiley ) is deprecated btw (the article linked in this answer)Goulette
Thank you @VickyS, Broken link fixedRestorative
N
2

your best option is to create your own view, and to override the onDraw() method using canvas.drawText() for text, and canvas.drawBitmap() for images.

See the Canvas doc : http://developer.android.com/reference/android/graphics/Canvas.html

Here is an example which draw some text at the center of the screen :

public class OverlayView extends View {

public OverlayView(final Context context) {
    super(context);
}

/**
 * Draw camera target.
 */
@Override
protected void onDraw(final Canvas canvas) {

    // view size
    int width = getWidth();
    int height = getHeight();

    float square_side = height - width * 0.8f; // size of the target square

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);

    // text size is 5% of the screen height
    paint.setTextSize(height * 0.05f);

    // draw message depending of its width
    String message = getResources().getString(R.string.photo_target_text);

    float message_width = paint.measureText(message);

    paint.setColor(getResources().getColor(R.color.color_foreground));
    canvas.drawText(message, (width - message_width) / 2,
            (height - square_side) / 4, paint);

    super.onDraw(canvas);
}

}
Nymphomania answered 21/1, 2012 at 13:46 Comment(0)
C
0

as I know, there's no way to do it.

but when you use webview and generated html you can do simillar one.

generate html from your contents, and load it to webview, set webview settings to no zoom control.

Controversial answered 21/1, 2012 at 13:37 Comment(0)
Q
0

This can be done with image getter: Html.ImageGetter

You will have to use HTML tags for each image.

Questioning answered 21/1, 2012 at 20:7 Comment(0)
F
0
< string name="text" > "Hi there, this is the photo [img src=img.jpge/] , hope you like it. < / string>

Add this in to your String and it will work

Firn answered 17/6, 2017 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.