I'm using Html.fromhtml to insert image in an editText ... but the instead of displaying the image, it displays [obj] " the text obj inside a square"
what could be the problem ??
This is the imageGetter
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = getResources().getDrawable(R.drawable.e11);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
This is the code line:
CharSequence cs =
Html.fromHtml("<img src='" +getResources().getDrawable(R.drawable.e11)+ "'/>"
,imageGetter, null);
A better way to do this would be to add a WebView.
Your imageGetter instance is not doing the correct thing. Where is it trying to retrieve the images from and what does it look like?
Related
I am using textview to load the HTML text. The HTML text has images. I am using ImageGetter to load images. But the image is getting cropped in the right. Below is the code:
textView.setText(Html.fromHtml(htmlText, new ImageGetter() {
#Override
public Drawable getDrawable(String source) {
String path = "/sdcard/" + source;
Drawable bmp = Drawable.createFromPath(path);
bmp.setBounds(0, 0, bmp.getIntrinsicWidth(), bmp.getIntrinsicHeight());
return bmp; }}, null));
I have a custom class that extends TextView that replaces the last character with a custom bitmap emoji. For some reason the emoji is always much smaller than the text. here is the overridden method
#Override
public void setText(CharSequence text, BufferType type) {
super.setText("H", type);
SpannableString span = new SpannableString(text);
Bitmap emoji = BitmapFactory.decodeResource(getResources(), R.drawable.emoji);
int size = (int) (-this.getPaint().ascent());
Bitmap scaledEmoji = Bitmap.createScaledBitmap(emoji, size, size, true);
emoji.recycle();
ImageSpan ispan = new ImageSpan(c, scaledEmoji, ImageSpan.ALIGN_BASELINE);
span.setSpan(ispan, text.length()-1, text.length(), 0);
}
super.setText(span, type);
}
Edit: ok so the problem is the setText method is being called before the constructor. I don't get the issue if I set the text pragmatically rather than in the xml. But I would like to know if there is a fix for that and how it's even possible to call a method on an object before instantiating it.
try this
ImageSpan ispan = new ImageSpan(c, scaledEmoji,ImageSpan.ALIGN_BASELINE);
spannable.setSpan(ispan, getText().length()-1,
getText().length() + emoji.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
I want custom item list view same image
what can i do to create item list view with small image view just like in the picture
Thank you very much
It looks like you actually want the image to be inline with the text.
In this case, you wouldn't use an ImageView. You would create a Spannable that contains an ImageSpan and assign it to the TextView.
String str = "Thank you very much [icon]";
int start = str.indexOf("[icon]");
int end = start + "[icon]".length();
SpannableString ss = new SpannableString(str);
Drawable d = getResources().getDrawable(R.drawable.headphones);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d);
ss.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
Use a textView, create html text with image tag, use Html.fromHtml. Read the docs on fromHtml, as you will still have to replace the images.
Alternatively, you can use a WebView, though I don't like the overhead.
I would like to insert jpeg images in edittext attached as shown below.
Stackoverflow has recommended this post but the reference projects provided are no longer valid.
how to insert jpeg files in edittext android
Will there be any reference projects?
try this,
EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null,null,getResources().getDrawable(R.drawable.check), null);
That's it.
You can use ImageSpan like this...
buildImageSpan(EditText et){
SpannableString ss = new SpannableString("anystring");
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0,0,d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, "anystring", ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, anystring.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
et.setText(et.getText()+ss);
}
You can set compound drawable top bottom left right using
edittext.setCompoundDrawables(left,top,right, bottom);
I'm not able to show an image which is saved in res/drawable folder.
I use ImageGetter to do this. The code is below:
ImageGetter imageGetter3 = new ImageGetter() {
public Drawable getDrawable(String source) {
int id=0;
if (source.equals("smiley")) {
id = R.drawable.smiley;
}
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n"
+ "Sta met je rug voor de deur\n"
+ Html.fromHtml("<img src=\"smiley\">", imageGetter3, null) + " Draai naar links\n";
What I see on the screen when running is a little square with "obj" text on it.
So what is wrong? The image cannot be read or something else?
How to show images?
Honestly I have Googled a lot and tried other methods of ImageGetter as well, but none of them seems to work, I tried these too, they don't work:
ImageGetter imageGetter2 = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = null;
d = Drawable.createFromPath(source);
d.setBounds(0,0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawFromPath;
int path = Route.this.getResources().getIdentifier(source, "drawable","com.prj56.tracingapp");
drawFromPath = (Drawable) Route.this.getResources().getDrawable(path);
drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(), drawFromPath.getIntrinsicHeight());
return drawFromPath;
}
};
=========================================================
if (....) {
ImageView iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.smiley);
directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n"
+ "Sta met je rug voor de deur\n";
HERE COMES THE IMAGE! BUT HOW TO DO THIS? It's within directions textview...
directions += " Draai naar links\n";
}
Update 12 Dec 2016:
getResources().getDrawable() was deprecated in api 22 so you should now be using ContextCompat.getDrawable e.g.
Drawable d = ContextCompat.getDrawable(context, R.drawable.smiley);
In your activity you can call this to get your Drawable resource as a Drawable object
Drawable d = getResources().getDrawable(R.drawable.smiley);
If you want to show your drawable in an ImageView you can do this:
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.smiley);
or in your xml file
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/smiley"/>
You can get the image with the help of pImg.Based on the adapter position get the string that is the image name.The image should be present in the drawable folder.
ImageView prodImg=(ImageView)view.findViewById(R.id.img_proj_name);
String pImg=prod.get(position);
int resID = view.getResources().getIdentifier("#drawable/"+pImg , "drawable", parentView.getContext().getPackageName());
prodImg.setImageResource(resID);
A super easy way to show it is to write this in your ImageView widget in your xml:
android:background="#drawable/your_file_name"
... and make sure you have put that image file (whether png or jpg, etc.) into your drawable folder. When you write your_file_name, you only need the title, not the file extension.
I prefer this way over programmatically writing in your .java file (as shown with other answers above), because your xml will show you the image in preview, programmatically will not, it will just show the ImageView placeholder.
Easiest way - Can be consider the below code
We can take advantage of Imageview setImageResource , refer below code for the same.
put image in the drawable like the below order
image_1.png, image_2.png, etc.
int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", getPackageName());
imageview.setImageResource(resId);
Drawable drawable = getResources().getDrawable(R.mydrawableID);
Now you can use it as Image, Like:-
ImageView myImage = findViewById(R.id.yourImageView);
Now fire this -
myImage.setImageResource(drawable);
You should use ContextCompat for backward compatibility features on Android.
//Using ButterKnife
#BindView(R.id.DogImg)
ImageView myImage;
//Or
ImageView myImage = findViewById(R.id.MyImage);
...
Drawable MyImageDrw = ContextCompat.getDrawable(context,R.drawable.myImageOnResources);
myImage.setImageDrawable(MyImageDrw);