Using HTML in a String - android

In my application I have to use a some HTML stuff in a string. But HTML is not working as intended. I have to use that string (Text) to send as an email. The sequence I required of HTML is:
Title (in the center)
Image (in the center)
Description (left align)
and then this HTML string is passed to an email intent. But neither image is showing up in the email nor the title text is getting center align. This is how I am doing this all:
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "");
it.setType("text/html");
String title = title;
String emailText = emailText;
it.putExtra(Intent.EXTRA_SUBJECT, title);
it.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(emailText));
this.startActivity(it);
and this is how the emailText is being formed:
emailText = "<p style= 'color:#000000; font:Georgia; font-size:18pt; text-align:center' align = 'center'><b>" + title +" </b></p>"
+"<br/><br />"
+"<img style=\"border:3px solid #173E8C\" src=\'" +imageUrl+"\' width=\"120\" height=\"90\"align=\"center\"/>"
+"<br/><br/>"
+"<p>" + description;
But I am unable to get the required result that I have mentioned right at the top, Any help is appreciated related to the issue. Thanks in advance..:-)

You must specify the type of email through the function setType () :
it.setType("text/html"); // for HTML
it.setType("text/plain"); // for plain text

You can't send a image as email body in android through Intent.

Related

Change text color of bold html text

I want to change text color before displaying it to TextView.
For example,I am getting a text from server like
<b>Pratik<b/> has shared photo with <b>you</b>.
Now the requirements are to display Pratik and you with Bold style and Blue text color. I tried several ways using <span> tag, but I am not getting clear way to display it.
String htmlBody = "<b>Pratik<b/> has shared photo with <b>you</b>.";
String formattedBody = "<span>" + htmlBody + "</span><style>b { color: #309ce8 ; } span {font-family: 'Avenir', 'Heavy'; font-size: 17; }</style>";
SpannableString text = new SpannableString(formattedBody);
tvMessage.setText(text, TextView.BufferType.SPANNABLE); // This is not working as expected.
tvMessage.setText(Html.fromHtml(htmlBody)); // This is not working as expected.
Help me achieve that.
String htmlBody = "<b style='color:blue !important;'>Pratik</b> has shared photo with <b style='color:blue !important;'>you</b>.";
Your Solution is:
String styledText = "<b><font color='red'>Pratik</font><b/> has shared photo with <b><font color='red'>you</font></b>";
You have to use like this
public void methodName() {
String html = "Some text <b>here</b>, and some <b>there</b>";
String result = html.replace("<b>","<font color=\"#ff0000\"><b>").replace("</b>", "</b></font>");
textView.setText(Html.fromHtml(result));
}

How to Show data from String Array ,in Single Android textview with cross image Each line contains Two Strings

1.I am getting data from server storing inside string Array.
2.then i want to show in Android TextView ,each TextView line should Contain 2 strings(Like Skill Sets) from String array.
3.then need to add cross Image in right side of the Textview.
please help me, how to achieve this.
String mystring = "This is my first sentence";
String arr[] = mystring.split(" ", 3);
String firstWord = arr[0]; //This
String secondWord = arr[1]; //is
String theRest = arr[2]; //my first sentence
yourtextview.setText(firstWord +" "+ secondWord);
and regarding for Image in right side of the Textview put this in ur txtview'sxml.
android:drawableRight="#drawable/image"

Android ACTION_SEND

i wanna make an action that the android app can send an email, and the email subject and body already fill with the edittext from the android app.
here's my code
Intent ii = new Intent(android.content.Intent.ACTION_SEND);
ii.setType("text/plain");
EditText txtName = (EditText)findViewById(R.id.txtName);
EditText txtAddress = (EditText)findViewById(R.id.txtAddress);
EditText txtLatitude = (EditText)findViewById(R.id.txtLatitude);
EditText txtLongitude = (EditText)findViewById(R.id.txtLongitude);
ii.putExtra(android.content.Intent.EXTRA_SUBJECT, txtName.getText().toString());
ii.putExtra(android.content.Intent.EXTRA_TEXT, txtAddress.getText().toString());
ii.putExtra(android.content.Intent.EXTRA_TEXT, txtLatitude.getText().toString());
ii.putExtra(android.content.Intent.EXTRA_TEXT, txtLongitude.getText().toString());
startActivity(Intent.createChooser(ii, "Share via"));
The subject is fine, but the body email is only fill with the last content(txtLongitude).
please tell me what to do.
Gratz.
You need to first make one string from the 3 EditTexts, then set it as an extra. For Example:
String body = txtName.getText().toString() + txtLatitude.getText().toString() + txtLongitude.getText().toString()
ii.putExtra(android.content.Intent.EXTRA_TEXT, body);
You'll probably also want to add some things like new line characters to format the text.
The subject is fine, but the body email is only fill with the last content(txtLongitude).
That is because you keep overriding the same EXTRA_TEXT extra. Call putExtra() for EXTRA_TEXT once, with the complete body of the email you want to send.

android: Set link with <a href> in TextView

I create a TextView dynamically and want to set the text as linkable. Text value is "Google". I referred to internet & blogs like this, which shows the same way, but I couldn't produce the expected results.
I tried different ways, but the output I see is the whole text with text only. The code I have tried with is :
TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
// Make Linkable
tv1.setMovementMethod(LinkMovementMethod.getInstance());
tv1.setText(Html.fromHtml(l.getLeftString()));
/*SpannableString s = new SpannableString(l.getLeftString());
Linkify.addLinks(s, Linkify.WEB_URLS);
tv1.setText(s);
tv1.setMovementMethod(LinkMovementMethod.getInstance());
*/
dialogLayout.addView(tv1);
In my output I see "Google" and no link. I also tried Clean project & building it again, but no success.
I am looking to see only "Google" as underlined with blue color (as default) and on clicking Google, the browser open with http://google.com.
What is lacking in my code to get the output ?
BTW For REF : I use 64bit Win 7, Java, Eclipse, Android API 8-2.2
Any help is highly appreciated.
I finally got it working using the following code :
TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
tv1.setText(Html.fromHtml("" + l.getLeftString() + ""));
tv1.setClickable(true);
tv1.setMovementMethod (LinkMovementMethod.getInstance());
dialogLayout.addView(tv1);
l.getRightString() - contains a url like http:\www.google.com
l.getLeftString() - contains text for the url like "Go to Google"
RESULTS :
Text "Go to Google" on my dialog with blue color and underlined, and on clicking it the browser opens up and shwows the respective page. On returning/Exiting the browser it again comes to the app from the state where it had left.
Hope this helps.
Save your html in a string
<string name="link"><a href="http://www.google.com">Google</a></string>
Set textview ID to to
textViewLinkable
In main activity use following code:
((TextView) findViewById(R.id.textViewLinkable)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.textViewLinkable)).setText(Html.fromHtml(getResources().getString(R.string.link)));
I was also facing the same issue I resolved using following
String str_text = "<a href=http://www.google.com >Google</a>";
TextView link;
link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());
link.setText(Html.fromHtml(str_text));
for changing the link color to blue use
link.setLinkTextColor(Color.BLUE);
Here is my simple implementation tested up to Android N.
String termsText = "By registering, you are agree to our";
String termsLink = " <a href=https://www.yourdomain.com/terms-conditions.html >Terms of Service</a>";
String privacyLink = " and our <a href=https://www.yourdomain.com/privacy-policy.html >Privacy Policy</a>";
String allText = termsText + termsLink + privacyLink;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText, Html.FROM_HTML_MODE_LEGACY));
}
else {
((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText));
}
txtview.setMovementMethod(LinkMovementMethod.getInstance());
Pass this statement to your textview, and in string.xml set an string as
<string name="txtCredits"> </string>
Now pass this string name " android:text="#string/txtCredits" to your xml class where the txtview is there .
use this code autolink-java On GitHub
like this
private String getLink(String string){
LinkExtractor linkExtractor = LinkExtractor.builder()
.linkTypes(EnumSet.of(LinkType.URL)) // limit to URLs
.build();
Iterable<Span> spans = linkExtractor.extractSpans(string);
StringBuilder sb = new StringBuilder();
for (Span span : spans) {
String text = string.substring(span.getBeginIndex(), span.getEndIndex());
if (span instanceof LinkSpan) {
// span is a URL
sb.append("<a href=\"");
sb.append(text);
sb.append("\">");
sb.append(text);
sb.append("</a>");
} else {
// span is plain text before/after link
sb.append(text);
}
}
return sb.toString(); // "wow http://test.com such linked"
}

linkyfy some text in TextView

I have a String in TextView and I want to Linkify a substring from that string. for example:
click here to know more.
I'm getting the string dynamically. So i have to search if it has click here and convert that to link .How can I linkify "click here".
To find a pattern inside a text and replace it, use this:
Pattern p = Pattern.compile("click here");
Matcher m = p.matcher("for more info, click here");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while(result) {
m.appendReplacement(sb, "click here");
result = m.find();
}
m.appendTail(sb);
String strWithLink = sb.toString();
yourTextView.setText(Html.fromHtml(strWithLink));
yourTextView.setMovementMethod(LinkMovementMethod.getInstance())
This code will search inside your string and replaces all "click here" with a link.
And at the end, do NOT add android:autoLink="web" to you XML resource (section TextView), otherwise A-tags are not rendered correctly and are not clickable any longer.
did your tried like this
Click here
for setting it to textview
 
//get this thru supstring
String whatever="anything dynamically";       
String desc = "what you want to do is<a href='http://www.mysite.com/'>"+whatever+":</a>";
yourtext_view.setText(Html.fromHtml(desc));
String urlink = "http://www.google.com";
String link = "<a href=\"+urlink+ >link</a>";
textView.setText(Html.fromHtml(link));
Raghav has the right approach using the fromHtml() method, but if you're searching for for a String with a fixed length, you could do something like:
String toFind = "click here";
if(myString.indexOf(toFind) > -1){
String changed = myString.substring(0, myString.indexOf(toFind)) + "<a href='http://url.whatever'>" + myString.substring(myString.indexOf(toFind), myString.indexOf(toFind) + toFind.length()) + "</a>" + myString.substring(myString.indexOf(toFind) + toFind.length());
}
else {
//String doesn't contain it
}
When setting the actual text, you need to use: tv.setText(Html.fromHtml(yourText)); or else it will just appear as a String without any additives. The fromHtml() method allows you to use certain HTML tags inside your application. In this case, the tag which is used for linking.

Categories

Resources