android-TextView setText in Html.fromHtml to display image and text [duplicate]
Asked Answered
C

1

8

I try to display the text and image in Html.fromHtml() but it is doesn't work in image display.

message = (TextView) findViewById (R.id.message);  

message.setText(Html.fromHtml(
        "<p><b>First, </b><br/>" +
        "Please press the" + "<img src = 'addbutton.png' />" + " to insert a new event.</p>"));

The text are display well but the image cannot display. How can improve it?

Canalize answered 6/5, 2013 at 10:10 Comment(7)
use ImageGetter, for example see this https://mcmap.net/q/242297/-html-imagegetter-textview/…Redolent
I try already... still cannot get it... :(Canalize
what "you cannot get it"?Redolent
Still cannot display my image.Canalize
I sent you a working example, so what's the problem?Redolent
That is too complicated for me... anyway... thank a lot... My coding run well...Canalize
if i were you i will use webview for img tagClow
C
24

This is the coding for reference to user pskink...

    package com.tutorial.myjob;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.Bundle;
import android.text.*;
import android.text.Html.ImageGetter;
import android.widget.*;

public class HelpMenu extends Activity implements ImageGetter{

    TextView message;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help_menu);
        String code = "<p><b>First, </b><br/>" +
                "Please press the <img src ='addbutton.png'> button beside the to insert a new event.</p>" +
                "<p><b>Second,</b><br/>" +
                "Please insert the details of the event.</p>"
                "<p>The icon of the is show the level of the event.<br/>" +
                "eg: <img src = 'tu1.png' > is easier to do.</p></td>";

        message = (TextView) findViewById (R.id.message);       
        Spanned spanned = Html.fromHtml(code, this, null);
        message.setText(spanned);
        message.setTextSize(16);


    }

    @Override
    public Drawable getDrawable(String arg0) {
        // TODO Auto-generated method stub
        int id = 0;

        if(arg0.equals("addbutton.png")){
            id = R.drawable.addbutton;
        }

        if(arg0.equals("tu1.png")){
            id = R.drawable.tu1;
        }
        LevelListDrawable d = new LevelListDrawable();
        Drawable empty = getResources().getDrawable(id);
        d.addLevel(0, 0, empty);
        d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());

        return d;
    }

}

This is what I edit... These coding run well for me... Thanks a lot for those helping me... Appreciated~ ^^

Canalize answered 6/5, 2013 at 11:36 Comment(4)
where to put tui.png image?Piperine
Where to put the images???Birthroot
For those unsure of where to put the image.. The getDrawable() method that is being Overridden takes in a String argument which is the src= in the html (ie tu1.png). The method then parses the incoming String and assigns which drawable is to be returned (ie R.drawable.tu1). The image (or drawable) should be placed in the /res/drawable-xhdpi/ folder (or any/all of the other /res/drawable-xxxxxx/ folders).Pears
how to make image inline to textview using this solution?Metagalaxy

© 2022 - 2024 — McMap. All rights reserved.