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
Related
I have used one textview and value set for it is like ex:11,234.45 and after decimal values like 45 should be shown with reduced front like 12sp and different colour like grey and 11,234 with black colour with front 16sp. how to achieve this using single textview in android?
You can achieve that if you follow this steps
First you need to split you string into 2 strings with this code lines
String s ="11,234.45";
String[] split = s.split(".");
String firstSubString = split[0];
String secondSubString = split[1];
Then you change the font size and color of secondSubString like this
SpannableString ss= new SpannableString(secondSubString);
ss.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); // set size
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
You can change firstSubString size and color like this
SpannableString ss1= new SpannableString(firstSubString);
ss1.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 5, 0);// set color
Finally you set your string to the TextView
TextView tv= (TextView) findViewById(R.id.textview);
tv.setText(ss1 + "." + ss);
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 styles (e.g., BackgroundColorSpan) and I'd like to set it to multiple parts of the texts. But if the same instance of BackgroundColorSpan, etc. is assigned to other part of the text, the style moves.
E.g.,
Object HighlightStyle = new BackgroundColorSpan(Color.argb(255, 255, 215, 0));
TextView X = (TextView) findViewById(R.id.Text);
SpannableStringBuilder SpannableBuilder = new SpannableStringBuilder();
SpannableBuilder.append("1");
SpannableBuilder.setSpan(HighlightStyle, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // This is hilited
SpannableBuilder.append("2");
SpannableBuilder.setSpan(HighlightStyle, 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // This is hilited and 1 is no more hilited
SpannableBuilder.append("3");
SpannableBuilder.append("4");
SpannableBuilder.setSpan(HighlightStyle, 3, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // This is hilited and 1 and 2 are no more hilited
X.setText(SpannableBuilder);
// I want hilited 1,2,4 all at the same time
Why I do it ? A user can search for some text and that text should be highlighted.
How I'd like to do it ? All text is in a single TextView. I have a single SpannableStringBuilder. I have a method that breaks the text into parts - String and Boolean. String is part, Boolean says whether the text is matched - should be highligted - or not. I just pass, e.g., instance of BackgroundColorSpan into that method and it should be applied to every part that was matched but because of the behavior of setSpan, only last part is highlighted.
Potential solutions: Style doesn't implement .clone method. I could make a workaround like serialize/deserialize it to create new instances but that seems really like a hack.
Is there some elegant way how to set the same style to X parts of the text ?
You should create new HighlightStyle object each time when you call setSpan(), you can do this with the next method:
CharacterStyle.wrap()
With all changes your code will be the next:
SpannableStringBuilder SpannableBuilder = new SpannableStringBuilder();
SpannableBuilder.append("1");
SpannableBuilder.setSpan(CharacterStyle.wrap(HighlightStyle), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableBuilder.append("2");
SpannableBuilder.setSpan(CharacterStyle.wrap(HighlightStyle), 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableBuilder.append("3");
SpannableBuilder.append("4");
SpannableBuilder.setSpan(CharacterStyle.wrap(HighlightStyle), 3, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
This is how it looks in my Application:
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!
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.