use of android.text.SpannableStringBuilder in project android_frameworks_base by ResurrectionRemix.
the class TextView method onSaveInstanceState.
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
// Save state if we are forced to
final boolean freezesText = getFreezesText();
boolean hasSelection = false;
int start = -1;
int end = -1;
if (mText != null) {
start = getSelectionStart();
end = getSelectionEnd();
if (start >= 0 || end >= 0) {
// Or save state if there is a selection
hasSelection = true;
}
}
if (freezesText || hasSelection) {
SavedState ss = new SavedState(superState);
if (freezesText) {
if (mText instanceof Spanned) {
final Spannable sp = new SpannableStringBuilder(mText);
if (mEditor != null) {
removeMisspelledSpans(sp);
sp.removeSpan(mEditor.mSuggestionRangeSpan);
}
ss.text = sp;
} else {
ss.text = mText.toString();
}
}
if (hasSelection) {
// XXX Should also save the current scroll position!
ss.selStart = start;
ss.selEnd = end;
}
if (isFocused() && start >= 0 && end >= 0) {
ss.frozenWithFocus = true;
}
ss.error = getError();
if (mEditor != null) {
ss.editorState = mEditor.saveInstanceState();
}
return ss;
}
return superState;
}
use of android.text.SpannableStringBuilder in project android_frameworks_base by ResurrectionRemix.
the class RestrictedLockUtils method setMenuItemAsDisabledByAdmin.
/**
* Set the menu item as disabled by admin by adding a restricted padlock at the end of the
* text and set the click listener which will send an intent to show the admin support details
* dialog. If the admin is null, remove the padlock and disabled color span. When the admin is
* null, we also set the OnMenuItemClickListener to null, so if you want to set a custom
* OnMenuItemClickListener, set it after calling this method.
*/
public static void setMenuItemAsDisabledByAdmin(final Context context, final MenuItem item, final EnforcedAdmin admin) {
SpannableStringBuilder sb = new SpannableStringBuilder(item.getTitle());
removeExistingRestrictedSpans(sb);
if (admin != null) {
final int disabledColor = context.getColor(R.color.disabled_text_color);
sb.setSpan(new ForegroundColorSpan(disabledColor), 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ImageSpan image = new RestrictedLockImageSpan(context);
sb.append(" ", image, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
sendShowAdminSupportDetailsIntent(context, admin);
return true;
}
});
} else {
item.setOnMenuItemClickListener(null);
}
item.setTitle(sb);
}
use of android.text.SpannableStringBuilder in project Tusky by Vavassor.
the class LinkHelper method setClickableText.
public static void setClickableText(TextView view, Spanned content, @Nullable Status.Mention[] mentions, boolean useCustomTabs, final LinkListener listener) {
SpannableStringBuilder builder = new SpannableStringBuilder(content);
URLSpan[] urlSpans = content.getSpans(0, content.length(), URLSpan.class);
for (URLSpan span : urlSpans) {
int start = builder.getSpanStart(span);
int end = builder.getSpanEnd(span);
int flags = builder.getSpanFlags(span);
CharSequence text = builder.subSequence(start, end);
if (text.charAt(0) == '#') {
final String tag = text.subSequence(1, text.length()).toString();
ClickableSpan newSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
listener.onViewTag(tag);
}
};
builder.removeSpan(span);
builder.setSpan(newSpan, start, end, flags);
} else if (text.charAt(0) == '@' && mentions != null) {
final String accountUsername = text.subSequence(1, text.length()).toString();
String id = null;
for (Status.Mention mention : mentions) {
if (mention.localUsername.equals(accountUsername)) {
id = mention.id;
}
}
if (id != null) {
final String accountId = id;
ClickableSpan newSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
listener.onViewAccount(accountId);
}
};
builder.removeSpan(span);
builder.setSpan(newSpan, start, end, flags);
}
} else if (useCustomTabs) {
ClickableSpan newSpan = new CustomTabURLSpan(span.getURL());
builder.removeSpan(span);
builder.setSpan(newSpan, start, end, flags);
}
}
view.setText(builder);
view.setLinksClickable(true);
view.setMovementMethod(LinkMovementMethod.getInstance());
}
use of android.text.SpannableStringBuilder in project android_frameworks_base by ResurrectionRemix.
the class NotificationBuilderTest method subst.
private static CharSequence subst(CharSequence in, char ch, CharSequence sub) {
int i = 0;
SpannableStringBuilder edit = new SpannableStringBuilder(in);
while (i < edit.length()) {
if (edit.charAt(i) == ch) {
edit.replace(i, i + 1, sub);
i += sub.length();
} else {
i++;
}
}
return edit;
}
use of android.text.SpannableStringBuilder in project android_frameworks_base by ResurrectionRemix.
the class ViewPropertyAlphaActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_properties);
getWindow().getDecorView().postDelayed(new Runnable() {
@Override
public void run() {
startAnim(R.id.button);
startAnim(R.id.textview);
startAnim(R.id.spantext);
startAnim(R.id.edittext);
startAnim(R.id.selectedtext);
startAnim(R.id.textviewbackground);
startAnim(R.id.layout);
startAnim(R.id.imageview);
startAnim(myViewAlphaDefault);
startAnim(myViewAlphaHandled);
EditText selectedText = (EditText) findViewById(R.id.selectedtext);
selectedText.setSelection(3, 8);
}
}, 2000);
Button invalidator = (Button) findViewById(R.id.invalidateButton);
invalidator.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
findViewById(R.id.textview).invalidate();
findViewById(R.id.spantext).invalidate();
}
});
TextView textView = (TextView) findViewById(R.id.spantext);
if (textView != null) {
SpannableStringBuilder text = new SpannableStringBuilder("Now this is a short text message with spans");
text.setSpan(new BackgroundColorSpan(Color.RED), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new SuggestionSpan(this, new String[] { "longer" }, 3), 11, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new UnderlineSpan(), 17, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ImageSpan(this, R.drawable.icon), 21, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(text);
}
LinearLayout container = (LinearLayout) findViewById(R.id.container);
myViewAlphaDefault = new MyView(this, false);
myViewAlphaDefault.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
container.addView(myViewAlphaDefault);
myViewAlphaHandled = new MyView(this, true);
myViewAlphaHandled.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
container.addView(myViewAlphaHandled);
}
Aggregations