If the second line don't have any text, the first line will be covered. I don't want to use ImageSpan.ALIGN_BOTTOM.
How can I fix this issue?
Here is the code:
Drawable drawable = ContextCompat.getDrawable(mActivity, R.drawable.phone_download_can_play_icon);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
String spanTitle = title + " " +"[canPlay]";
SpannableString spannable = new SpannableString(spanTitle);
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
spannable.setSpan(span, title.length() + " ".length(), spanTitle.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
so sad, I don't have enough reputation to upload the images.
Here is the image link
Related
I want to set my Toolbar title to "Videos (4)" where the "Videos" part is one color, and the "(4)" part is another color. I want to use colors from my colors.xml resource file.
How can I do this?
Here is how I'm setting the Toolbar title:
getSupportActionBar().setTitle("Videos (" + numVideos + ")");
You can set custom font and colors with spannable.
Try this:
protected void setActionBarTitle(String title) {
//if API level below 18, there is a bug at Samsung and LG devices
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
getSupportActionBar().setTitle(title);
} else {
SpannableString s = new SpannableString(title);
s.setSpan(new TypefaceSpan(this, "Your-Font.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(s);
}
}
In your case, this should work:
SpannableString firstPart = new SpannableString("Videos ");
SpannableString lastPart = new SpannableString(" (4)");
firstPart.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getApplicationContext(), R.color.white)), 0, firstPart.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
lastPart.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getApplicationContext(), R.color.black)), 0, lastPart.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
then set your title thike this:
getSupportActionBar().setTitle(firstPart + lastPart);
or like this:
getSupportActionBar().setTitle(TextUtils.concat(firstPart, lastPart));
String text = "<font color=#cc0029>Videos</font> <font color=#ffcc00>(" + numVideos + ")</font>";
getSupportActionBar().setTitle(Html.fromHtml(text));
I want to create a SpannableString with some emotions and text like this image.
Below is the method that I have used so for but it just attach emotions with text. Please suggest me how can I create such types of view.
private SpannableStringBuilder getLikeCountString(UserFeedData feedData, Context mContext) {
SpannableStringBuilder builder = new SpannableStringBuilder();
int emotionSize = (int) mContext.getResources().getDimension(R.dimen.emotion_size);
if (feedData.getEmotionTypes() != null) {
String[] emotions = feedData.getEmotionTypes().split(",");
for (String emotion : emotions) {
SpannableString emojSpan = new SpannableString(" ");
// Getting image based on emotion id
Drawable icon = ContextCompat.getDrawable(mContext, ContentDetailFragment.getLikeEmotionResource(Integer.valueOf(emotion.trim())));
//icon.setBounds(0, 0, (icon.getIntrinsicWidth() / 2) + 5, (icon.getIntrinsicHeight() / 2) + 5);
icon.setBounds(0, 0, emotionSize, emotionSize);
ImageSpan imageSpan = new ImageSpan(icon, ImageSpan.ALIGN_BOTTOM);
emojSpan.setSpan(imageSpan, 0, emojSpan.length() - 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
builder.append(emojSpan);
}
}
if (feedData.getContentLikes() > 0) {
builder.append(feedData.getContentLikes() + " ");
}
return builder;
}
Instead of using individual ImageSpans for each emoji, use one ImageSpan with LayerDrawable inside, one emoji per layer. You can do all kinds of overlaps with LayerDrawable.
Inside android SearchView i made a TextView and added "This Site" to it then converted the view to bitmap and added span using a shape drawable.
I dont want my cursor to type before the Span and setSelection doesn't seems to work for me
final SearchView sv = new SearchView(getActivity());
final String tokenName = "This Site";
final SpannableStringBuilder sb = new SpannableStringBuilder();
TextView tv = createSearchTokenTextView(tokenName);
BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
sb.append(tokenName + " ");
sb.setSpan(new ImageSpan(bd), sb.length() - (tokenName.length() + 1), sb.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Selection.setSelection(sb, 0, sb.toString().length());
sv.setQuery(sb, false);
I used the below code to add image span.
Drawable happySmiley = ctx.getResources().getDrawable(
R.drawable.img_smile);
happySmiley.setBounds(0, 0, happySmiley.getIntrinsicWidth(),
happySmiley.getIntrinsicHeight());
Drawable sadSmiley = ctx.getResources()
.getDrawable(R.drawable.ic_launcher);
sadSmiley
.setBounds(0, 0, sadSmiley.getIntrinsicWidth(),
sadSmiley.getIntrinsicHeight());
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("Some text ");
builder.setSpan(new ImageSpan(happySmiley), builder.length()-1, builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(". More text [sad_smiley_anchor]");
builder.setSpan(new ImageSpan(sadSmiley), builder.length()
- "[sad_smiley_anchor]".length(), builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvSS.setText(builder);
o/p Some text (smiley). More text (smiley).
But I want to display the smiley above the "Some" in "Some text". Like
(Smiley) (Smiley)
Some Text. More text.
If smiley can be set as background to "Some" in "Some text" like setForegroundSpan() etc. then also problem can be short out. Please comment on this.
Thanks
I want to create a TextView that contains programming language code. like the following picture
I think that I should use html for give color and also will use string functions such as indexof,substring .. firstly, will find "for" and if i find it i will replace it with
<h3 style="color:blue;margin-left:20px;">for</h3>
But I am not sure if its the best way or useful.. Do you have any idea to do it on android?
PS: I am not making a compiler, just want to show piece of code
PS2: I am not asking for webview or textview.. Just want to learn how can I do it except my html case. Doesnt matter webview or textview or editview
Try this
public static Spannable setFourDifferentFontStyle(final Context context, String original,
String firstWord, String color1, float size,
String secondWord, String color2, float size2,
String thirdWord, String color3, float size3,
String fourthWord, String color4, float size4)
{
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord + secondWord + thirdWord + fourthWord);
spannable.setSpan(new RelativeSizeSpan(size), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color1)), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new RelativeSizeSpan(size2), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color2)), firstWord.length(),firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new RelativeSizeSpan(size3), firstWord.length() + secondWord.length(), firstWord.length() + secondWord.length() + thirdWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color3)), firstWord.length() + secondWord.length(),firstWord.length() + secondWord.length() + thirdWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new RelativeSizeSpan(size4), firstWord.length() + secondWord.length() + thirdWord.length(), original.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color4)), firstWord.length() + secondWord.length() + thirdWord.length(), original.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Thank you for all attention, I've completly found a solution that I want.
I've used codemirror library, and found a sample for android.
you can download the project from code google