Android : display html data - android

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

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

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"><html><body><b>What is CMK?</b> ......</body></html> </string>
I think you will have to escape the less than characters. Before loading the text, Log it. You'll see the problem.

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
}

Related

How to add images,links in between string.xml

I added scrolling activity(textscoll) to my Project.So I wanted to add huge text to that activity and several images in between that huge text.So I added string file in string.xml and called it in scrolling activity(content_textscoll.xml)...
My string file
<string name="string_text_file">This is huge text...</string>
My content_textscoll.xml
<TextView
android:layout_width="wrap_content"
android:textColor="#000000"
android:textSize="20dp"
android:lineSpacingMultiplier="1.3"
android:layout_height="wrap_content"
android:layout_margin="#dimen/text_margin"
android:text="#string/string_text_file" />
My problem is,
*I want to add several images in between above text.How to resolve that problem....?
I can add images to drawble folder.How to call them?
*How to add html links or other activity links
make web view :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="abhiandroid.com.htmlexample.MainActivity">
<WebView
android:id="#+id/simpleWebView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp" />
</RelativeLayout>
make assest folder if not exist and create new html file there with any name you want, then put your text and images there like this :
<p>in this tag (p tag) put your text </p>
<img>in this tag (img tag) put your images </img>
in your activity make something like this :
WebView webView;
public String fileName = "yourAssestFileName.html";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// init webView
webView = (WebView) findViewById(R.id.simpleWebView);
// displaying content in WebView from html file that stored in assets folder
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/" + fileName);
}

How to write the Bitcoin logo as text?

I am developing an android app and I want to write the Bitcoin symbol in a TextView.
Is there any code for that like "\u20B9" for Rupees?
Android supports Bitcoin Unicode symbol since Android O, you can read more about it from this link. Just try to run your code in Android O and I'm sure that everything will be fine.
But if still want to use TextView, to show BTC symbol, consider FontAwesome, it's actually designed for WEB, but it also possible to use in Android.
Download icons pack.
Put ttf font file to your assets folder
Then get Typeface object and set it to your TextView:
String fontName = "fa-brands-400.ttf";
Typeface fontAwesome = Typeface.createFromAsset(getAssets(), "fonts/" + fontName);
textView.setTypeface(fontAwesome);
Add string XML resource with FontAwesome code point of BTC symbol:
<string name="btc_fa"></string>
Set this string to TextView:
textView.setText(R.string.btc_fa);
As the result, you will get this:
Create a string in strings.xml like the following one:
<string name="bitCoin">\u20BF</string>
Use the following to create the textView with BitCoin Symbol:
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/bitCoin"/>
You will have your bitcoin symbol in textView like below

How do i display html Special Characters in my android textview

Actually i am retrieving text from json web service which is HTML text containing tags and all that ,which i am executing with Html.fromHtml ,but the problem is that the Special Characters are displayed as diamond with question mark which i am not able to display in its correct form.
below is the code where i got the problem
Description=Description.replaceAll("\\<.*?>","");
Description=Description.replaceAll("\\“", "");
Description=Description.replaceAll("\\”", "");
// Description=Description.replaceAll("♦", "");
//Description=Description.replace("“", "");
details.setText(Html.fromHtml(Description).toString());
Description is the String variable where i have stored the html text,i have googled alot but doesn't get anything.hope i got something from here
thanks in advance
In my webview I did sothing like this.
productInfoWebview.loadData(value, "text/html; charset=UTF-8", null);
Decoding HTML is decoding HTML entities to Java raw unicode characters.
String html = "B & This is HTML";
String java = Html.fromHtml(html);
> Output: "B \u0026 This is HTML"
String strJava = Html.fromHtml(html).toString();
> Output: "B & This is HTML"

Opening a local HTML file

I have a textview in a layout (called - t_c) with the code:-
<TextView
android:id="#+id/GoToTCContacting"
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="2"
android:background="#drawable/border2"
android:clickable="true"
android:onClick="GoToTCContacting"
android:padding="10dp"
android:text="Contacting"
android:textColor="#FFF"
android:textSize="18sp" />
I want the textview to open another layout (called - t_c_contacting) on click which has a webview in and that webview to open a html file I have in my layout folder called con.html.
This is how the t_c_contacting layout is coded:-
<WebView
android:id="#+id/contactingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
This is how my class is set up to open t_c_contacting which works fine but I can't work out how to populate the webview with my file.
public void GoToTCContacting(View view)
{
setContentView(R.layout.t_c_contacting);
}
If you already have the HTML stored localy, simply call:
webView.loadDataWithBaseURL(mUrl, mHtmlData, MIME_TYPE, CHARSET, "");
Where:
mUrl can be a dummy Url if you want, or the actual URL of the page, so the WebView can figure out how to place some UI elements such as Images.
mHtmlData is a String that contains the actual HTML content.
MIME_TYPE is a String that represents the mimeType of the data, say: "text/html"
CHARSET is a String that represents the encoding of the data, say: "utf-8"
I hope that helps.

Specifying drawable resource id in a separate xml

I have an xml file that specifies a set of image button names. Also, I would like to specify the image resource id as an attribute of an xml node as shown below:
<button name="close" resLocation="R.drawable.close" />
I am parsing the xml in the code, I would like to set the image background for the dynamically created button using the resLocation attribute. Since resLocation is a string, I cannot convert to a Drawable object directly. Is there a way I can workaround?
You can use get getResources().getIdentifier:
String myResourceId = "close"; // Parsed from XML in your case
getResources().getIdentifier(myResourceId, "drawable", "com.my.package.name");
This would require your XML to be a little different:
<button name="close" resLocation="close" />
If you need to keep the R.type.id format in your XML, then you would just need to parse out the type and id:
String myResourceId = "R.drawable.close";
String[] resourceParts = myResourceId.split("\\.");
getResources().getIdentifier(resourceParts[2], resourceParts[1], "com.my.package.name");
You can try
<button name="close" resLocation="#drawable/close" />
or try
ImageButton imgButton=new ImageButton(this);
imgButton.setImageResource(getResources().getDrawable(R.drawable.close));

Categories

Resources