how to convert plain text into html text in android? - android

I am uploading the text to server, i just want to upload those string in html format
example
input:
Do you know the relation between two eyes...???
They never see each other... BUT
They blink together.
They move together.
They cry together.
They see together.
They sleep together.
They share a very deep bonded relationship...
However, when they see a pretty woman, one will blink and another will not...
sendtext = adding_textjoke.getText().toString();
//String htmlString = Html.toHtml(sendtext);
String str = "(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>?«»“”‘’]))";
Pattern patt = Pattern.compile(str);
Matcher matcher = patt.matcher(sendtext);
sendtext = matcher.replaceAll("$1");
System.out.println(sendtext);
Log.e("sendtext", sendtext);
new AddJokesTask().execute(sendtext);
How to do this in android?

You can do it like this
SpannableString contentText = (SpannableString) contentView.getText();
String htmlEncodedString = Html.toHtml(contentText)

SpannableStringBuilder text = (SpannableStringBuilder) contentView.getText();
String htmlEncodedString = Html.toHtml(text);

Related

Parsing a string?

So I have the following string:
String text = "\t\t\torder #168\n\t\t\tpaid\n\t\t\tview 4 items\n\t\t\tpicked up\n\t\t\tcomplete pickup\n\t\t\t2 stops";
How do I parse this string so that I always get the 2 in front of stops? I have tried the following, but it always returns 2 stops.
String substr = "complete pickup";
String numberOfStops = text.substring(text.indexOf(substr) + substr.length());
numberOfStops = numberOfStops.replaceAll("^\\s+","").replaceAll("\\s+$","");
The short way:
numberOfStops = numberOfStops.replaceAll("^\\s+","").replaceAll("\\s+$","").replace("stops","");
The flexible way is using Regex, and Pattern and Match classes. Let me know if you need it

in StringBuffer i want to add half bold text and half normal in append

how can i set half bold text by html.fromhtml i try but didn't work in append of BufferString out append its work fine please help me here is code
String amt = "AMOUNT: ";
Spanned tyyy =Html.fromHtml("<b>"+"TYPE: "+"</b>");
String Dat = "DATE: ";
String des = "DESCRIPTION: ";
buffer.append(Html.fromHtml("<b>"+amt+"</b>"+"<small>"+cc.getString(cc.getColumnIndex("AMOUNT"))+"</small>")+"\n");
buffer.append(tyyy+cc.getString(cc.getColumnIndex("TYPE"))+"\n");
If it doesn't need to be from HTML, you can use SpannableStringBuilder:
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("TYPE:");
// bold the current text
int boldStartIndex = 0;
int boldEndIndex = bulder.length();
builder.setSpan(new StyleSpan(Typeace.BOLD, boldStartIndex, boldEndIndex, Spanned.FLAG_EXCLUSIVE_EXCLUSIVE));
// append the rest, it will not be bolded
builder.append("the-rest-of-your-data");
You can then set the builder directly to a TextView via TextView.setText(builder);. This may be a better approach than using HTML, as HTML tags support isn't very well documented and may not be supported across various devices.

SKipping image while converting html text to string android

Iam converting some Html text from a webpage into a String by doing the following
mydescription =Html.fromHtml(data.getBody()).toString();
This is what data.getBody() returns:-
<div><p>​It's great to have great dynamic companies to work with, and NXP is no exception.</p><p><img alt="This is an image of NXP Logo" src="https://anprodstorage.blob.core.windows.net/b75ef288-0381-45c4-a4cd-809097370bec/untitled.png" style="margin:5px;" /><br></p><div><iframe width="560" height="315" src="https://www.youtube.com/embed/I6191gXXGog" frameborder="0"></iframe> </div><p>​<br></p></div>
But within that html text there is a image source as well. When I do the above I get a square image with obj written inside it instead of the image.
This is myDescription

I just want to get the text and not the image.
How do i just get the text and not the image
Try this way,hope this will help you to solve your problem.
String htmlString = "<div><p>​It's great to have great dynamic companies to work with, and NXP is no exception.</p><p><img alt=\"This is an image of NXP Logo\" src=\"https://anprodstorage.blob.core.windows.net/b75ef288-0381-45c4-a4cd-809097370bec/untitled.png\" style=\"margin:5px;\" /><br></p><div><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/I6191gXXGog\" frameborder=\"0\"></iframe> </div><p>​<br></p></div>";
String first = htmlString.substring(0,htmlString.indexOf("<img"));
String second = htmlString.substring(htmlString.indexOf("/>",htmlString.indexOf("<img"))+2,htmlString.length());
textview.setText(Html.fromHtml(first+second));
use this code:
String clippedBody = htmlString.replaceAll("<img[^>]*?>.*?/[^>]*?>", "");
I advise using libraries, like jsoup when working with HTML (with soup you will be able to get only text by calling Jsoup.parse(html).text())
Haven't tried it myself
private static final Pattern REMOVE_TAGS = Pattern.compile("<img>(\\S+)</img>");
public static String removeTags(String string) {
if (string == null || string.length() == 0) {
return string;
}
Matcher m = REMOVE_TAGS.matcher(string);
return m.replaceAll("");
}
If you want to strip down all the HTML code, then you can use:
replaceAll("\\<[^>]*>","")
For your second question (from Source 2):
// the pattern we want to search for
Pattern p = Pattern.compile("<p>(\\S+)</p>");
Matcher m = p.matcher(string);
// if we find a match, get the group
if (m.find())
{
// get the matching group
String codeGroup = m.group(1);
// print the group
System.out.format("'%s'\n", codeGroup);
}
Source: 1, 2 and 3

Android Matcher and Pattern cut up from the link

I have a string that contains a link.
ex. of the string:
We love to eat choco, eat one up at http://t.co/9BDZvcx59d.
So If I display this string as it is, it would be the same. But if I use matcher and pattern to detect the link and color it, it will cut it up.
It'll be: Bold is green color.
We love to eat choco, eat one up at http://t.co.
Pattern urlPattern = Patterns.WEB_URL;
Matcher m = urlPattern.matcher(sb.toString());
sb = new StringBuffer(sb.length());
while (m.find()) {
m.appendReplacement(sb, "<font color=\"#006600\">" + m.group(1) + "</font>");
}
m.appendTail(sb);
I also tried using
Pattern linkPattern = Pattern.compile("(http[A-Za-z0-9_-+)");
But couldn't insert : or // in the [].
You can try this for your second method.
Pattern linkPattern = Pattern.compile("(http://t.co/[A-Za-z0-9_-]+)");
It will compile after the t.co/ so it won't face symbols or anything but letter and numbers.
Try to add following attributes to your TextView in xml
android:autoLink="web"
android:textColorLink="your-color-code"

Removing space from Edit Text String

In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data.
Now, the method I use for getting the String value from Edit Text is like this :
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
Now normally it works fine, but if we the text in Edit Text contains space then my app crashes.
for eg. - if someone types "food" in the Edit Text Box, then it's OK
but if somebody types "Indian food" it crashes.
How to remove spaces and get just the String ?
Isn't that just Java?
String k = edittext.getText().toString().replace(" ", "");
try this...
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
String newData = k.replaceAll(" ", "%20");
and use "newData"
String email=recEmail.getText().toString().trim();
String password=recPassword.getText().toString().trim();
In the future, I highly recommend checking the Java String methods in the API. It's a lifeline to getting the most out of your Java environment.
You can easily remove all white spaces using something like this. But you'll face another serious problem if you just do that. For example if you have input
String input1 = "aa bb cc"; // output aabbcc
String input2 = "a abbcc"; // output aabbcc
String input3 = "aabb cc"; // output aabbcc
One solution will be to fix your application to accept white spaces in input string or use some other literal to replace the white spaces. If you are using only alphanumeric values you do something like this
String input1 = "aa bb cc"; // aa_bb_cc
String input2 = "a abbcc"; //a_abbcc
String input3 = "aabb cc"; //aabb_cc
And after all if you are don' caring about the loose of information you can use any approach you want.

Categories

Resources