I have an ExpandableListView and in the ViewGroup method i have an image span set to some textview like this:
String statusText = postsList.get(groupPosition).get(TAG_userStatus).substring(urlStr.length(), postsList.get(groupPosition).get(TAG_userStatus).length());
Bitmap bm = BitmapFactory.decodeFile(urlStr);//urlStr is the path to my image: /mnt/sdcard/1.jpg
Bitmap resized = Bitmap.createScaledBitmap(bm, 200, 200, true);
TextView userStatus1 = (TextView)v.findViewById( R.id.userStatus );
SpannableStringBuilder ssb = new SpannableStringBuilder(" "+statusText );
ImageSpan span = new ImageSpan(resized, ImageSpan.ALIGN_BOTTOM);
ssb.setSpan( span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
userStatus1.setText( ssb, BufferType.SPANNABLE );
I need to be able to click on the image and show it in full screen.But I don't know how to make the image clickable. I have read similar threads but nothing works for me. I think it's because expandableListView but I'm not sure. I hope you can help me. Thank you!
Related
I am trying to add multiple smiles in textview using this code.
This is my TextView.
<TextView
android:id="#+id/textViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:bufferType="spannable" />
And this is add smiley function.
public void addSmily() {
int resource = R.drawable.smily ;
Spannable spannable = Spannable.Factory.getInstance().newSpannable(" ");
Drawable d = ContextCompat.getDrawable(this, resource);
d.setBounds(0, 0, 40, 40);
ImageSpan smilySpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
spannable.setSpan(smilySpan, spannable.length()-1, spannable.length(), 0);
sendText.append(spannable);
}
Smiles are adding perfectly but the problem is when I add lots of smiles did not fit in a single line then the first line of smiles become invisible and they start from the 2nd line.
This is what happening. Plz, someone help me.
Solution:
Try this inside your button:
SpannableString ss = new SpannableString("abc");
Drawable d = ContextCompat.getDrawable(your_activity.this, R.drawable.your_smiley_drawable);
d.setBounds(0, 0, 40, 40);
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
edittext.append(ss);
Note: Also, EditText's inputtype must be textMultiline.
Try it, Works in my lap, Let's Hope it helps to you too.
You can also set smiley by unicode in textview.
how set emoji by unicode in a textview?
int unicode = 0x1F60A;
Which can be used with
public String getEmojiByUnicode(int unicode){
return new String(Character.toChars(unicode));
}
So Textview displays 😊 without Drawable
Try it with http://apps.timwhitlock.info/emoji/tables/unicode
Hope it may help you.
I have a simple layout contains just one TextView.
I wanna load an image into TextView using ImageSpan.
Below method creates Spannable:
private Spannable getImageSpannable(int drawableId, int targetWidth, int targetHeight) {
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), drawableId);
Bitmap bitmap = Bitmap.createScaledBitmap(originalBitmap, targetWidth, targetHeight, true);
Drawable dr = new BitmapDrawable(getResources(), bitmap);
dr.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
Spannable imageSpannable = new SpannableString("\uFFFC");
ImageSpan imgSpan = new ImageSpan(dr, DynamicDrawableSpan.ALIGN_BOTTOM);
imageSpannable.setSpan(imgSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return imageSpannable;
}
I use this method to create content like this:
public void setContent() {
SpannableStringBuilder content = new SpannableStringBuilder();
content.append(getImageSpannable(R.drawable.my_image, 100, 260));
content.append("\n");
txtContent.setText(content);
}
When I call setContent() method my result is something like this:
As you see there is small gap between ImageSpan and top of TextView.
This is not line spacing, because I set line spacing to 0.
And interesting point is when I remove "\n" from content(declared in setContent method) this space is gone.
And another point is that when I tried to measure content size using StaticLayout, with "\n" at the end bottom of line 0 it returns 270 and without "\n" it returns 260.
This behavior causes some difficulties for me, because I have to measure text and ImageSpan using StaticLayout and decide witch one can fit into TextView.
I appreciate everyone can help me.
Thanks.
Here's my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txtContent"
android:layout_width="300dp"
android:layout_height="500dp"
android:background="#fed9f4"
android:textSize="22sp"/>
</LinearLayout>
I'v done some tests and I find that font size is affects ImageSpan rendering.
Can somebody explain this affect please?
I hope this method works
The following line of code
public void setContent() {
SpannableStringBuilder content = new SpannableStringBuilder();
content.append(getImageSpannable(R.drawable.my_image, 100, 260));
content.append("\n");
txtContent.setText(content);
}
Change to
public void setContent() {
SpannableStringBuilder content = new SpannableStringBuilder();
content.append(getImageSpannable(R.drawable.my_image, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
content.append("\n");
txtContent.setText(content);
}
And resize "R.drawable.my_image" dimensions
I am trying to extend the html transformation capabilities within Android 4.4.
I am needing to be able to change the text size arbitrarily within a block of text to an absolute value.
So I have a starting block of text sized with an AbsoluteSizeSpan like this :
ass = new AbsoluteSizeSpan(Integer.valueOf(18), true);
output.setSpan(ass, 0, 255, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Now, when I need to change the size of a word or sentence within this span, I am doing the same again -
ass = new AbsoluteSizeSpan(Integer.valueOf(12), true);
output.setSpan(ass, 100, 185, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
However, this is not being taken into account.
Does this mean that this is not possible ?
Do I need to change something in the textview to allow this ?
AbsoluteSizeSpan ass18 = new AbsoluteSizeSpan(Integer.valueOf(18), true);
AbsoluteSizeSpan ass12 = new AbsoluteSizeSpan(Integer.valueOf(12), true);
output.setSpan(ass18, 0, 100, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
output.setSpan(ass12, 100, 185, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
output.setSpan(ass18, 185, 255, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
You can find the solution from the example in this link http://www.android--tutorials.com/2016/08/android-absolutesizespan-example.html?m=1
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 need to add more than one smileys in a single edittext box. For add a single smiley I follow this link
How to add more smileys in a single Edittext box? Thanks in advance..
You can add as many ImageSpans to a Spannable as you like. Just follow the concept laid out by the code you're linking. You probably want to use a SpannableStringBuilder too.
Drawable happySmiley = mContext.getResources().getDrawable(R.drawable.happy);
happySmiley .setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
Drawable sadSmiley = mContext.getResources().getDrawable(R.drawable.sad);
sadSmiley .setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("Some text [happy_smiley_anchor]");
builder.setSpan(new ImageSpan(happySmiley), builder.length()-"[happy_smiley_anchor]".length(), builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(". Some more text [sad_smiley_anchor]");
builder.setSpan(new ImageSpan(sadSmiley), builder.length()-"[sad_smiley_anchor]".length(), builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
edittext.setText(builder);
Obviously you can use any anchor text/character you like - it gets replaced when the ImageSpan is inserted. It might even work with an empty char/string, but I didn't try that.
You can add as many ImageSpans to a Spannable as you like. Just follow the concept laid out by the code you're linking. You probably want to use a SpannableStringBuilder too.
SpannableStringBuilder ssb = new SpannableStringBuilder("Some Text");
Bitmap image1 = BitmapFactory.decodeResource( getResources(), R.drawable.yourimage1 );
Bitmap image2 = BitmapFactory.decodeResource( getResources(), R.drawable.yourimage1 ); ssb.setSpan( new ImageSpan( image1 ), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
ssb.setSpan( new ImageSpan( image2 ), 2,3, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
deleteButton.setText( ssb, BufferType.SPANNABLE );
I tried above code and it works fine. I added two image span in one text view, likewise you can add as many image span to textview.