Using ImageGetter and HTML, I have some emoticons in an edittext (code below), however I want to be able to recognize these images later in the edittext so it can be stored as a string. Does anyone know how to retrieve this, or should I keep a string updated with string value instead.
Thanks
ImageGetter imggtr = new ImageGetter()
{
public Drawable getDrawable(String source)
{
Drawable d = getActivity().getResources().getDrawable(getEmoticonDrawable(position));
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
Spanned cs = Html.fromHtml("<img src='" + getActivity().getResources().getDrawable(getEmoticonDrawable(position))
+ "'/>", imggtr, null);
mEditor.append(cs);
As you mentioned, you should keep a separate string in memory. If you try to parse the html back to a string, it's going to be complicated and not very flexible.
Related
My Android App has a few screens where the user can tap a button, and help text pops up in a DialogFragment. Currently the text for these DFs is stored in strings.xml, like this:
<string name="help_content">
<![CDATA[<h1>Welcome</h1>
<p>Here is some helpful information</p>]]></string>
Obviously, this lets me add styles to the text to make it look better.
In a couple of places, I'd like to explain what some icons do, and I want to include images of the icons, so I'd like to do something like this:
<string name="help_content">
<![CDATA[<h1>Welcome</h1>
<p><img src="path/to/icon1"> This is what Icon 1 does</p>
<p><img src="path/to/icon2"> This is what Icon 2 does</p>]]></string>
Is there a way to include the images so that they use the actual ones from the app? ie something along the lines of getResources().getDrawable(R.drawable.icon_1) or #drawable/icon_1 or something, which can be referenced from the CDATA. I've tried both of those, but got the red error line.
Thanks to the links above, I figured this out. I have my icons saved as drawables, and did the following:
In the help_content string, I put img tags with the name of the required drawable saved as src:
<string name="help_content">
<![CDATA[<h1>Welcome</h1>
<p><img src="icon1"> This is what Icon 1 does</p>
<p><img src="icon2"> This is what Icon 2 does</p>]]></string>
Then at the point where I add this string to the TextView, I previously had:
mHelpContent.setText(Html.fromHtml(mText));
and have replaced it with:
mHelpContent.setText(Html.fromHtml(mText, new ImageGetter(), null));
I then define the ImageGetter class as follows:
private class ImageGetter implements Html.ImageGetter {
public Drawable getDrawable(String source) {
int id;
if((source == null) || (source.equals(""))) {
return null;
}else {
id = mContext.getResources().getIdentifier(
source,
"drawable",
mContext.getPackageName()
);
if(id != 0) {
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0,
d.getIntrinsicWidth(),
d.getIntrinsicHeight());
return d;
}else return null;
}
}
}
This shows the icon as required.
I need to make text view with image at right. I need make text wrap around the image at left.
Text may be different length. Image place at top-right always.
Image example:
Х - measns image place.
The basic and simple way to achieve this...!
final String testContent = "<html><body><img src=\"ic_launcher.png\" style=\"float:right;\"/>This is like testing if this thing works ghdjdhfjkhgdjkhgkfdjgkljdfkljgklfdjkgljkfd kljlk lj kld jgkljdklgjfdkljgkfldjg kljfdkl dklfgjklfdj gklfdjklg This is like testing if this thing works ghdjdhfjkhgdjkhgkfdjgkljdfkljgklfdjkgljkfd kljlk lj kld jgkljdklgjfdkljgkfldjg kljfdkl dklfgjklfdj gklfdjklg jdfklgjkdfljg kldfjgkljdfklgjdklf. It XXX"
+ " in a more elaborate This is like testing if this thing works ghdjdhfjkhgdjkhgkfdjgkljdfkljgklfdjkgljkfd kljlk lj kld jgkljdklgjfdkljgkfldjg kljfdkl dklfgjklfdj gklfdjklg jdfklgjkdfljg kldfjgkljdfklgjdklf. It XXX"
+ " in a more elaborate This is like testing if this thing works ghdjdhfjkhgdjkhgkfdjgkljdfkljgklfdjkgljkfd kljlk lj kld jgkljdklgjfdkljgkfldjg kljfdkl dklfgjklfdj gklfdjklg jdfklgjkdfljg kldfjgkljdfklgjdklf. It XXX"
+ " in a more elaborate jdfklgjkdfljg kldfjgkljdfklgjdklf. It XXX";
textView.setText(Html.fromHtml(testContent, imgGetter, null));
}
private ImageGetter imgGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawable = null;
if (imageNumber == 1) {
drawable = getResources().getDrawable(R.drawable.ic_launcher);
++imageNumber;
} else
drawable = getResources().getDrawable(R.drawable.ic_launcher);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
return drawable;
}
};
In my app i need to integrate commenting functionality which includes smiley.And i am successfully showing the smiley's in android edit text box.But i am struck at... after writing the complete comment with smiley's user will click send.After user clicking the enter i need to send the comment as a text(need to replace smile with its code as string like ":-)")
. How to convert that smiley image to its respective code. Please any one help me to get out of this.
And to convert text to smileys i am using the bellow code
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = getResources().getDrawable(
cstlistemoji.images[index]);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
cs = Html.fromHtml(
"<img src='"
+ getResources()
.getDrawable(cstlistemoji.images[index])
+ "'/>", imageGetter, null);
System.out.println("cs is:- " + cs);
edttxtemoji.append(cs);
I'm attempting to download webpages for a user to view later. So far, I've downloaded the html, and the images. I can get the HTML to show up nicely formatted, but I cannot get the images to inline.
The code I'm using so far:
This is my method to get the article.
MyImageGetter mig = new MyImageGetter(this, urlId);
Spanned span = Html.fromHtml(contents[1], mig, null);
contents[1] = span.toString();
titleView.setText(contents[0]);
content.setText(contents[1]);
contents[] is an array that contains two strings. contents[0] is a simple string, contents[1] is a string with HTML markup.
MyImageGetter:
public class MyImageGetter implements Html.ImageGetter{
String urlId = null;
Context c = null;
public MyImageGetter(ArticleViewer articleViewer, String urlId2) {
c = articleViewer;
urlId = urlId2;
}
public Drawable getDrawable(String source) {
String[] brokenUrl = source.split("/");
String imgName = brokenUrl[brokenUrl.length-1];
File image = new File("/data/data/com.theHoloDev.Reader/Offline/" + urlId + "/" + imgName);
Log.w("MyApp", image.getAbsolutePath());
Bitmap bm = BitmapFactory.decodeFile(image.getAbsolutePath());
Drawable d = new BitmapDrawable(c.getResources(), bm);
return d;
}
}
When I have it log image.getAbsolutePath() it comes up with a file that exists in ddms. The Text content is there perfectly, but there are still little black boxes that say obj in each image. I had thought a textview would still be able to display images in that fashion, but either I'm doing something wrong, or there is another way to do this.
What is your desired end result? It is confusing to see that you are displaying html but you want the user to see the page... without html or did you do that for debugging? I assume the users will not see the html but rather the the page itself. In this case, I would go for a webview.
http://developer.android.com/reference/android/webkit/WebView.html
I don't think the text view is meant to be used like you are using it.
Good luck.
I figured it out. Instead of:
Spanned span = Html.fromHtml(contents[1], mig, null);
contents[1] = span.toString();
content.setText(contents[1]);
I need:
Spanned span = Html.fromHtml(contents[1], mig, null);
content.setText(span);
I was running into problems setting span to string.
Are there any limitations to adding an ImageSpan to a widget?
This identical code works fine in a standard TextView.
SpannableStringBuilder buf = new SpannableStringBuilder("");
if(!TextUtils.isEmpty(message.getMessageBody())) {
SmileyParser parser = SmileyParser.getInstance();
buf.append(parser.addSmileySpans(group ? message.getMessageBodyWithoutName() : message.getMessageBody()));
}
view.setTextViewText(R.id.message_body, buf);
Thanks.
Edit 1:
public CharSequence addSmileySpans(CharSequence text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
Matcher matcher = mPattern.matcher(text);
while (matcher.find()) {
int resId = mSmileyToRes.get(matcher.group());
builder.setSpan(new ImageSpan(mContext, resId),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return builder;
}
The smiley's are local assets.
I am going to interpret this literally, that you mean your images are in assets/.
My guess is that the home screen is having difficulty resolving your asset reference. As a test, try putting the images on external storage and using Uri.fromFile() to create your Uri. If that works, try putting them as drawable resources and using the resource IDs. Or, try the resource Uri syntax:
Uri.parse("android.resource://your.package.name.goes.here/" + R.raw.myvideo);