png
png

Reputation: 4408

Android : display html data

I want to display the content of an html page and an image to the right of that in my view.

So i have defined my layout as

<ScrollView android:id="@+id/scrllvwNo1"
    android:layout_width="fill_parent" android:layout_height="wrap_content">
    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:background="@drawable/home_bg">
        <ImageView android:id="@+id/aboutcmkimage"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignParentRight="true" android:src="@drawable/about"
            android:padding="5dip" />
        <WebView android:id="@+id/aboutcmk" android:layout_width="wrap_content"
            android:layout_height="fill_parent" android:textColor="#000000"
            android:layout_toLeftOf="@+id/aboutcmkimage"
            android:layout_alignParentTop="true" android:layout_alignParentLeft="true" />

    </RelativeLayout>

</ScrollView>

and

I try to load the html page as below

WebView web = (WebView)findViewById(R.id.aboutcmk);
         web.loadData(getString(R.layout.about),"texl/html","utf-8");

in this case i am getting the error "this page contains error at line1 ... "

if I try

web.loadDataWithBaseURL(null,getString(R.layout.about),"texl/html","utf-8",null); 

no html output and no error

In both case the image is coming

Can anyone help me in debugging

My html has bullets , so i cannot use textview instead of webview

here is the definition of my htmlstring in xml file

<string name="About"><html><body><b>What is CMK?</b> ......</body></html> 

Thanks a lot for the help

Upvotes: 0

Views: 1521

Answers (3)

Chris Fei
Chris Fei

Reputation: 1317

Instead of storing the HTML in strings.xml, you can store it in its own file in the res/raw/ directory. For example, let's say you save the file in res/raw/mypage.html. I haven't tested this, but you should be able to open up raw resources and load them up in a WebView like:

try {
    Resources resources = getResources();
    InputStream inputStream = resources.openRawResource(R.raw.mypage);
    byte[] bytes = new byte[inputStream.available()];
    inputStream.read(bytes);

    String htmlStr = new String(bytes);
    webView.loadData(htmlStr, "text/html", "UTF-8");
} catch(Exception e) {
    //blah
}

Upvotes: 0

Arnab Chakraborty
Arnab Chakraborty

Reputation: 7472

First of all, if you have saved your HTML specification as a string resource, your should access it as R.string.about and not R.layout.about. Change that, if it still doesn't work, try escaping the less than characters in your string, like this :

<string name="about">&lt;html>&lt;body>&lt;b>What is CMK?&lt;/b> ......&lt;/body>&lt;/html> </string>

I think you will have to escape the less than characters. Before loading the text, Log it. You'll see the problem.

Upvotes: 1

kalpana c
kalpana c

Reputation: 2739

are you put this line What is CMK? ...... in strings.xml if yes then the correction is in this line web.loadDataWithBaseURL(null,getString(R.layout.about),"texl/html","utf-8",null);

Correct one:

web.loadDataWithBaseURL(null, getString(R.string.About), "text/html", "UTF-8", null);

Upvotes: 1

Related Questions