Text viewed as HTML - android

Hey everyone this is a update from my previous question
https://stackoverflow.com/questions/6676440/android-showing-a-edittext-text-in-webview
I got the webview to see the text inside my Edit text field but its displaying it as text
this is the coding im using
case R.id.Preview:
Intent j = new Intent(this, Preview.class);
j.putExtra(com.afajje.htmlzero.Preview.URL,
myEditText.getText().toString());
startActivity(j);
}
return false;}
Want im trying to have it do is view the Edit text, text as HTML
For example if I put in the Edittext field
<html>
<head>
</head>
<body>
<p>This is a Preview</p>
</body>
</html>
In the web view itll just just the "This is a Preview"

Here ya go, a working demo:
package com.stackexchange.test;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.widget.EditText;
public class home extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText box = (EditText) findViewById(R.id.edittext);
String htmltext = "<b>This is bold. </ b> and <i> this is italic </ i>." ;
Spanned text = Html.fromHtml( htmltext );
box.setText( text );
}
}
You should see (except that I've edited the text in the app):

Try the below code to show the html text into the EditText.
String str = "<html><head></head><body><p>This is a Preview</p></body></html>";
editText = (EditText)findViewById(R.id.editText);
editText.setText(Html.fromHtml(str));

Well, in the editText, if you dont use "Html.fromHtml", the string will be display with the html tags. In a WebView, I really dont know what zou want to do...sorry

Related

TextView with HTML content - Hide images/empty squares

I have html content and setting this into (android) textbox as follows
textbox.setText(Html.fromHtml(myhtml));
HTML content contains some img tags as well, I do not want display images, currently it displays empty square boxes where ever there is a img tag. How do I hide those square boxes?
You could do a regex replace <img.+?> on htmlString.
textView.setText(Html.fromHtml(htmlString.replaceAll("<img.+?>", "")));
Untested
Update:
since images can look like:
<img ...></img>
and
<img... />
This solution will match both cases:
String htmlBody = htmlString.replaceAll("<img.+/(img)*>", "");
textView.setText(Html.fromHtml(htmlBody));
This works
Create Html.ImageGetter to return empty image as follows
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
public class EmptyImageGetter implements Html.ImageGetter {
private static final Drawable TRANSPARENT_DRAWABLE = new ColorDrawable(Color.TRANSPARENT);
#Override
public Drawable getDrawable(String source) {
return TRANSPARENT_DRAWABLE;
}
}
and then create a static instance
private static final EmptyImageGetter EMPTY_IMAGE_GETTER = new EmptyImageGetter();
set this into text view
textView.setText(Html.fromHtml(myhtml, EMPTY_IMAGE_GETTER, null));

how to display data in alert Box with splitview in android

package com.example.example2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
public class MainActivity extends Activity {
WebView web1;
WebView web2;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String str1 = "<html>This first Html data</html>";
final String str2 = "<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework"
+ "help help with homework homework assignments elementary school high school middle school"
+ "// --><font color='#60c000' size='4'><strong>Please!</strong></font>"
+ "<img src='http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif' />";
web1 = (WebView) findViewById(R.id.webfirst);
web2 = (WebView) findViewById(R.id.websecond);
btn = (Button) findViewById(R.id.compare);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
web1.loadDataWithBaseURL("", str1, "text/html", "UTF-8", "");
web2.loadDataWithBaseURL("", str2, "text/html", "UTF-8", "");
// we have display This on Alert view with spliting .
}
});
}
}
This is my split view activity when i click on Button we are able to see data in web view with spliting activity But i have to do that when i click on Button then string 1 and string 2 should display in Pop window within same activity with splitted data how to split alert or pop view in android Please help me am unable to do this i have tired much But not getting how iw ill do it
Make your activity to dialog in manifest is the one way
<activity android:theme="#android:style/Theme.Dialog" />
by adding the above code in your activity you can see the activity as dialog
(OR)
Try to display custom dialog in this way
Dialog dialog_help = new Dialog(getActivity());
dialog_help.setContentView(R.layout.help_screen);
TextView tv_1 = (TextView) dialog_help.findViewById(R.id.tv_help_1);
tv_1.setText("");
dialog_help.setCancelable(true);
dialog_help.setTitle(" Help ");
dialog_help.setCanceledOnTouchOutside(true);
dialog_help.show();
dialog_help.getWindow().setGravity(Gravity.CENTER);
dialog_help.getWindow().setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);

How to show html codes inside TextView or something else on Android?

How can I show html source as rendered in TextView or something else?
I tried this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.tv1);
String s = "<img src=\"http://images2.layoutsparks.com/1/146278/purple-lotus-lovely-dew.jpg\"/>";
Spanned sp = new SpannableString(s);
tv.setText(Html.toHtml(sp));
}
But I got:
HTML support in a TextView is pretty limited, but here is an example of some of the things you can do:
https://github.com/brianjolly/TextFun
The key is escaping the opening bracket in your strings.xml
<string name="fun_text">I like <font color=\'0x00b0f1\'>BLUE!</font> and <b> Bold</b> and <i> italic</i> and <u> underline</u></string>
And then resetting the text like so:
String styledText = tv.getText().toString();
tv.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
The beauty of open source is you can see what Html.fromHtml() supports in the android source here:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/Html.java
If you're looking to do something more advanced take a look at WebView

show the text of an EditText into a TextView

Describing the functionality of my application: I have put in a Relative Layout a TextView, an EditText and a button. All I am trying to do is: when the user writes something in the EditText and push the button, then the content of the EditText is appeared in the TextView(just like a chat-virtual chat). Everything works perfectly, but when the EditText is empty,and the button get pushed, an empty line is appeared in the TextView(and i don't want to..). Although I've tried to solve it using an if the empty line is still appearing in the TextView. I would be really greatfull, if you could help!!! Than you in advance!
Here is my code:
package teiath.android.appliacation;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class M_chat extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**Code for the scroll bars in the TextView. */
final TextView tv = (TextView)findViewById(R.id.TEXT_VIEW);
tv.setMovementMethod(new ScrollingMovementMethod());//for the scroll bars
/** Code for the scroll bars in the EditText. */
final EditText wr = (EditText)findViewById(R.id.EDIT_TEXT);
wr.setMovementMethod(new ScrollingMovementMethod());//for the scroll bars
final Button button = (Button) findViewById(R.id.btn1);//find the button by id in main.xml
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String wrcontent = wr.getText().toString();//gets the text of the EditText and put it in "tvcontent" variable.
String tvcontent = tv.getText().toString();//gets the text of the textView and put it in "tvcontent" variable.
if (wrcontent!="")//if the EditText is not empty
{
//check if the TextView is empty or not
if (tvcontent!="")//If it is not empty...
{
tv.setText(tvcontent + "\n" + wrcontent);//add its current(TextView's text) text, new line and the text of the EditText as the new text of TextView.
//tv.setVisibility(0);//makes visible the textView with the cloud1.png background
wr.setText("");//set the text of the Edit Text as empty
//wrcontent = "";
}
else//if the TextView is empty...
{
tv.setText(wrcontent);//add the text of the editText as the new text of the TextView
wr.setText("");
}
}
/**if (wrcontent=="")
{
}*/
//finish();
}
});
}
}
Don't use !="" for String comparison. To check for empty text, use something like
if ( wrcontent != null && wrcontent.trim().length() == 0 ) {
Better yet, include Guava libraries in your code.

In an Android TextView, is it possible to insert paragraphs?

Below is an example of the type of problem that I have. I have data in a pojo that I need to display in a textview... the data has pseudo code that denotes each paragraph with [p]
I would like to somehow parse the [p]'s into paragraphs when they are displayed in the textview. Can this be done? Is there something I can substitute for the [p] that will make a new paragraph in the textview?
Question question = new Question();
question.setText("Here is the first paragraph.[p] And this should be the second.");
TextView view = (TextView) findViewById(R.id.qtext);
view.setText(question.getParsedText());
Hi i would parse the whole String and then replace every [p] with \n or even \n\n as you like.
\n in the String makes a linebreak. For example use it like that:
question.setText("Here is the first paragraph.\n\n And this should be the second.");`
The method which can do that easily is String.replace(...)
You can also try this
String summary = "<html><font color=\"#FFFFFF\"" +
"<p align="+ "\"" +"left" + "\""+ ">" + "MY data" +"</p>"+
"</font></html>";
mTextView.setText(Html.fromHtml(summary));
TextView tw = findViewById(R.id.t);
tw.setText("first line\nsecond line\nthird line");
<br> and <p> work if you need other formatting like bold and are in the middle of an Html.fromHtml:
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView tv = new TextView(this);
tv.setText(Html.fromHtml("first<br><b>second</b>"));
setContentView(tv);
}
}
Tested on Android 22.

Categories

Resources