Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Add image to text (in text View)?

Tags:

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=)

like image 711
user1162299 Avatar asked Jan 21 '12 13:01

user1162299


People also ask

How do I change text view on Android?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute. You can either set the text as attribute value directly, or reference a text defined in the strings.

How do I append text in Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. In the above code, we have taken editext, button and textview. When user click on button after inserting value in edittext,it will append the data using string writer.

What does text view do in Android Studio?

A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.


2 Answers

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

like image 79
R.daneel.olivaw Avatar answered Sep 23 '22 03:09

R.daneel.olivaw


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);
}

}
like image 33
ndeverge Avatar answered Sep 23 '22 03:09

ndeverge