List of usage examples for android.text SpannableString SpannableString
public SpannableString(CharSequence source)
From source file:com.saulmm.cui.OrderDialogFragment.java
@BindingAdapter("app:spanOffset") public static void setItemSpan(View v, int spanOffset) { final String itemText = ((TextView) v).getText().toString(); final SpannableString sString = new SpannableString(itemText); sString.setSpan(new RelativeSizeSpan(1.65f), itemText.length() - spanOffset, itemText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); ((TextView) v).setText(sString);/*from w w w .j av a 2 s . c om*/ }
From source file:com.ubergeek42.WeechatAndroid.ChatLinesAdapter.java
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder;//www . java2 s .c o m // If we don't have the view, or we were using a filteredView, inflate a new one if (convertView == null) { convertView = inflater.inflate(R.layout.chatview_line, null); holder = new ViewHolder(); holder.timestamp = (TextView) convertView.findViewById(R.id.chatline_timestamp); holder.prefix = (TextView) convertView.findViewById(R.id.chatline_prefix); holder.message = (TextView) convertView.findViewById(R.id.chatline_message); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // Change the font sizes holder.timestamp.setTextSize(textSize); holder.prefix.setTextSize(textSize); holder.message.setTextSize(textSize); BufferLine chatLine = (BufferLine) getItem(position); // Render the timestamp if (enableTimestamp) { holder.timestamp.setText(timestampFormat.format(chatLine.getTimestamp())); holder.timestamp.setPadding(holder.timestamp.getPaddingLeft(), holder.timestamp.getPaddingTop(), 5, holder.timestamp.getPaddingBottom()); } else { holder.timestamp.setText(""); holder.timestamp.setPadding(holder.timestamp.getPaddingLeft(), holder.timestamp.getPaddingTop(), 0, holder.timestamp.getPaddingBottom()); } // Recalculate the prefix width based on the size of one character(fixed width font) if (prefixWidth == 0) { holder.prefix.setMinimumWidth(0); StringBuilder sb = new StringBuilder(); for (int i = 0; i < maxPrefix; i++) { sb.append("m"); } holder.prefix.setText(sb.toString()); holder.prefix.measure(convertView.getWidth(), convertView.getHeight()); prefixWidth = holder.prefix.getMeasuredWidth(); } // Render the prefix if (chatLine.getHighlight()) { String prefixStr = chatLine.getPrefix(); Spannable highlightText = new SpannableString(prefixStr); highlightText.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, prefixStr.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); highlightText.setSpan(new BackgroundColorSpan(Color.MAGENTA), 0, prefixStr.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); holder.prefix.setText(highlightText); } else { if (enableColor) { holder.prefix.setText(Html.fromHtml(chatLine.getPrefixHTML()), TextView.BufferType.SPANNABLE); } else { holder.prefix.setText(chatLine.getPrefix()); } } if (prefix_align.equals("right")) { holder.prefix.setGravity(Gravity.RIGHT); holder.prefix.setMinimumWidth(prefixWidth); } else if (prefix_align.equals("left")) { holder.prefix.setGravity(Gravity.LEFT); holder.prefix.setMinimumWidth(prefixWidth); } else { holder.prefix.setGravity(Gravity.LEFT); holder.prefix.setMinimumWidth(0); } // Render the message if (enableColor) { holder.message.setText(Html.fromHtml(chatLine.getMessageHTML()), TextView.BufferType.SPANNABLE); } else { holder.message.setText(chatLine.getMessage()); } return convertView; }
From source file:com.github.dfa.diaspora_android.activity.PodSelectionActivity.java
private void showPodConfirmationDialog(final String selectedPod) { // Make a clickable link final SpannableString dialogMessage = new SpannableString(getString(R.string.confirm_pod, selectedPod)); Linkify.addLinks(dialogMessage, Linkify.ALL); // Check if online if (!Helpers.isOnline(PodSelectionActivity.this)) { Snackbar.make(listPods, R.string.no_internet, Snackbar.LENGTH_LONG).show(); return;//from w w w . j a v a 2s . c o m } // Show dialog new AlertDialog.Builder(PodSelectionActivity.this).setTitle(getString(R.string.confirmation)) .setMessage(dialogMessage) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { onPodSelectionConfirmed(selectedPod); } }).setNegativeButton(android.R.string.no, null).show(); }
From source file:org.awesomeapp.messenger.ui.ConversationListItem.java
/** public void bind(ConversationViewHolder holder, Cursor cursor, String underLineText, boolean scrolling) { bind(holder, cursor, underLineText, true, scrolling); }//from www .ja v a 2 s.c o m */ public void bind(ConversationViewHolder holder, long contactId, long providerId, long accountId, String address, String nickname, int contactType, String message, long messageDate, int presence, String underLineText, boolean showChatMsg, boolean scrolling) { //applyStyleColors(holder); if (nickname == null) { nickname = address.split("@")[0].split("\\.")[0]; } else { nickname = nickname.split("@")[0].split("\\.")[0]; } if (Imps.Contacts.TYPE_GROUP == contactType) { String groupCountString = getGroupCount(getContext().getContentResolver(), contactId); nickname += groupCountString; } if (!TextUtils.isEmpty(underLineText)) { // highlight/underline the word being searched String lowercase = nickname.toLowerCase(); int start = lowercase.indexOf(underLineText.toLowerCase()); if (start >= 0) { int end = start + underLineText.length(); SpannableString str = new SpannableString(nickname); str.setSpan(new UnderlineSpan(), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE); holder.mLine1.setText(str); } else holder.mLine1.setText(nickname); } else holder.mLine1.setText(nickname); holder.mStatusIcon.setVisibility(View.GONE); if (holder.mAvatar != null) { if (Imps.Contacts.TYPE_GROUP == contactType) { holder.mAvatar.setVisibility(View.VISIBLE); if (AVATAR_DEFAULT_GROUP == null) AVATAR_DEFAULT_GROUP = new RoundedAvatarDrawable( BitmapFactory.decodeResource(getResources(), R.drawable.group_chat)); holder.mAvatar.setImageDrawable(AVATAR_DEFAULT_GROUP); } // else if (cursor.getColumnIndex(Imps.Contacts.AVATAR_DATA)!=-1) else { // holder.mAvatar.setVisibility(View.GONE); Drawable avatar = null; try { avatar = DatabaseUtils.getAvatarFromAddress(this.getContext().getContentResolver(), address, ImApp.SMALL_AVATAR_WIDTH, ImApp.SMALL_AVATAR_HEIGHT); // avatar = DatabaseUtils.getAvatarFromCursor(cursor, COLUMN_AVATAR_DATA, ImApp.SMALL_AVATAR_WIDTH, ImApp.SMALL_AVATAR_HEIGHT); } catch (Exception e) { //problem decoding avatar Log.e(ImApp.LOG_TAG, "error decoding avatar", e); } try { if (avatar != null) { //if (avatar instanceof RoundedAvatarDrawable) // setAvatarBorder(presence,(RoundedAvatarDrawable)avatar); holder.mAvatar.setImageDrawable(avatar); } else { // int color = getAvatarBorder(presence); int padding = 24; LetterAvatar lavatar = new LetterAvatar(getContext(), nickname, padding); holder.mAvatar.setImageDrawable(lavatar); } holder.mAvatar.setVisibility(View.VISIBLE); } catch (OutOfMemoryError ome) { //this seems to happen now and then even on tiny images; let's catch it and just not set an avatar } } } if (showChatMsg && message != null) { if (holder.mLine2 != null) { if (SecureMediaStore.isVfsUri(message)) { FileInfo fInfo = SystemServices.getFileInfoFromURI(getContext(), Uri.parse(message)); if (fInfo.type == null || fInfo.type.startsWith("image")) { if (holder.mMediaThumb != null) { holder.mMediaThumb.setVisibility(View.VISIBLE); if (fInfo.type != null && fInfo.type.equals("image/png")) { holder.mMediaThumb.setScaleType(ImageView.ScaleType.FIT_CENTER); } else { holder.mMediaThumb.setScaleType(ImageView.ScaleType.CENTER_CROP); } setThumbnail(getContext().getContentResolver(), holder, Uri.parse(message)); holder.mLine2.setVisibility(View.GONE); } } else { holder.mLine2.setText(""); } } else if ((!TextUtils.isEmpty(message)) && message.startsWith("/")) { String cmd = message.toString().substring(1); if (cmd.startsWith("sticker")) { String[] cmds = cmd.split(":"); String mimeTypeSticker = "image/png"; Uri mediaUri = Uri.parse("asset://" + cmds[1]); setThumbnail(getContext().getContentResolver(), holder, mediaUri); holder.mLine2.setVisibility(View.GONE); holder.mMediaThumb.setScaleType(ImageView.ScaleType.FIT_CENTER); } } else if ((!TextUtils.isEmpty(message)) && message.startsWith(":")) { String[] cmds = message.split(":"); try { String[] stickerParts = cmds[1].split("-"); String stickerPath = "stickers/" + stickerParts[0].toLowerCase() + "/" + stickerParts[1].toLowerCase() + ".png"; //make sure sticker exists AssetFileDescriptor afd = getContext().getAssets().openFd(stickerPath); afd.getLength(); afd.close(); //now setup the new URI for loading local sticker asset Uri mediaUri = Uri.parse("asset://localhost/" + stickerPath); setThumbnail(getContext().getContentResolver(), holder, mediaUri); holder.mLine2.setVisibility(View.GONE); holder.mMediaThumb.setScaleType(ImageView.ScaleType.FIT_CENTER); } catch (Exception e) { } } else { if (holder.mMediaThumb != null) holder.mMediaThumb.setVisibility(View.GONE); holder.mLine2.setVisibility(View.VISIBLE); try { holder.mLine2.setText(android.text.Html.fromHtml(message).toString()); } catch (RuntimeException re) { } } } if (messageDate != -1) { Date dateLast = new Date(messageDate); holder.mStatusText.setText(sPrettyTime.format(dateLast)); } else { holder.mStatusText.setText(""); } } else if (holder.mLine2 != null) { holder.mLine2.setText(address); if (holder.mMediaThumb != null) holder.mMediaThumb.setVisibility(View.GONE); } holder.mLine1.setVisibility(View.VISIBLE); if (providerId != -1) getEncryptionState(providerId, accountId, address, holder); }
From source file:org.transdroid.core.gui.navigation.NavigationHelper.java
/** * Converts a string into a {@link Spannable} that displays the string in the Roboto Condensed font * @param string A plain text {@link String} * @return A {@link Spannable} that can be applied to supporting views (such as the action bar title) so that the input string will be displayed * using the Roboto Condensed font (if the OS has this) *//* ww w .j a va 2 s.c om*/ public static SpannableString buildCondensedFontString(String string) { if (string == null) { return null; } SpannableString s = new SpannableString(string); s.setSpan(new TypefaceSpan("sans-serif-condensed"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return s; }
From source file:org.kontalk.ui.ComposeMessage.java
static CharSequence applyUpdatingStyle(CharSequence text) { // we call toString() to strip any existing span SpannableString status = new SpannableString(text.toString()); status.setSpan(sUpdatingTextSpan, 0, status.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return status; }
From source file:it.gulch.linuxday.android.fragments.EventDetailsFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_event_details, container, false); holder = new ViewHolder(); holder.inflater = inflater;/* ww w .ja va2 s .co m*/ ((TextView) view.findViewById(R.id.title)).setText(event.getTitle()); TextView textView = (TextView) view.findViewById(R.id.subtitle); String text = event.getSubtitle(); if (TextUtils.isEmpty(text)) { textView.setVisibility(View.GONE); } else { textView.setText(text); } MovementMethod linkMovementMethod = LinkMovementMethod.getInstance(); // Set the persons summary text first; replace it with the clickable text when the loader completes holder.personsTextView = (TextView) view.findViewById(R.id.persons); String personsSummary = ""; if (CollectionUtils.isEmpty(event.getPeople())) { holder.personsTextView.setVisibility(View.GONE); } else { personsSummary = StringUtils.join(event.getPeople(), ", "); holder.personsTextView.setText(personsSummary); holder.personsTextView.setMovementMethod(linkMovementMethod); holder.personsTextView.setVisibility(View.VISIBLE); } ((TextView) view.findViewById(R.id.track)).setText(event.getTrack().getTitle()); Date startTime = event.getStartDate(); Date endTime = event.getEndDate(); text = String.format("%1$s, %2$s %3$s", event.getTrack().getDay().getName(), (startTime != null) ? TIME_DATE_FORMAT.format(startTime) : "?", (endTime != null) ? TIME_DATE_FORMAT.format(endTime) : "?"); ((TextView) view.findViewById(R.id.time)).setText(text); final String roomName = event.getTrack().getRoom().getName(); TextView roomTextView = (TextView) view.findViewById(R.id.room); Spannable roomText = new SpannableString(String.format("%1$s", roomName)); // final int roomImageResId = getResources() // .getIdentifier(StringUtils.roomNameToResourceName(roomName), "drawable", // getActivity().getPackageName()); // // If the room image exists, make the room text clickable to display it // if(roomImageResId != 0) { // roomText.setSpan(new UnderlineSpan(), 0, roomText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // roomTextView.setOnClickListener(new View.OnClickListener() // { // // @Override // public void onClick(View view) // { // RoomImageDialogFragment.newInstance(roomName, roomImageResId).show(getFragmentManager()); // } // }); // roomTextView.setFocusable(true); // } roomTextView.setText(roomText); textView = (TextView) view.findViewById(R.id.abstract_text); text = event.getEventAbstract(); if (TextUtils.isEmpty(text)) { textView.setVisibility(View.GONE); } else { String strippedText = StringUtils.stripEnd(Html.fromHtml(text).toString(), " \n"); textView.setText(strippedText); textView.setMovementMethod(linkMovementMethod); } textView = (TextView) view.findViewById(R.id.description); text = event.getDescription(); if (TextUtils.isEmpty(text)) { textView.setVisibility(View.GONE); } else { String strippedText = StringUtils.stripEnd(Html.fromHtml(text).toString(), " \n"); textView.setText(strippedText); textView.setMovementMethod(linkMovementMethod); } holder.linksContainer = (ViewGroup) view.findViewById(R.id.links_container); return view; }
From source file:com.tasomaniac.android.widget.IntegrationPreference.java
private SpannableString getErrorString(CharSequence originalString) { SpannableString errorSpan = new SpannableString(originalString); ForegroundColorSpan colorSpan = new ForegroundColorSpan( ContextCompat.getColor(getContext(), R.color.error_color)); errorSpan.setSpan(colorSpan, 0, originalString.length(), 0); return errorSpan; }
From source file:com.ugedal.weeklyschedule.MainActivity.java
@SuppressWarnings("StatementWithEmptyBody") @Override/*from w w w. j a v a 2s .c om*/ public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); int group_id = item.getGroupId(); if (group_id == R.id.trinn_chooser) { // Save new state, and update the fragment SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.current_grade_key), id); editor.apply(); TextView text = (TextView) findViewById(R.id.textView); text.setText(item.getTitle()); getSupportActionBar().setTitle(item.getTitle()); mListFragment.myList.clear(); mListFragment.adapter.notifyDataSetChanged(); swipeContainer.setRefreshing(true); mListFragment.refreshContent(); } if (id == R.id.nav_about) { AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); alertDialog.setTitle(getString(R.string.about)); final SpannableString s = new SpannableString(getText(R.string.about_message)); Linkify.addLinks(s, Linkify.WEB_URLS); alertDialog.setMessage(s); alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, getString(android.R.string.cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.show(); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; }
From source file:io.relayr.tellmewhen.gcm.GcmIntentService.java
private InboxStyle prepareBigNotificationDetails(Spannable spanTitle) { NotificationCompat.InboxStyle result = new InboxStyle(); result.setBigContentTitle(spanTitle); for (Map.Entry<Pair<SensorType, String>, Float> entry : pushedRules.entrySet()) { SensorType sensorType = entry.getKey().first; String ruleName = entry.getKey().second; if (ruleName.length() <= 20) { int empty = 20 - ruleName.length(); for (int i = 0; i < empty; i++) { ruleName += " "; }//www.j a v a2s.c o m } else { ruleName = entry.getKey().second.substring(0, 20); } String notificationText = getString(R.string.push_notification_value) + " " + SensorUtil.scaleToUiData(sensorType, entry.getValue()) + sensorType.getUnit(); String all = ruleName + " (" + notificationText + ")"; Spannable spanNotif = new SpannableString(all); spanNotif.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, ruleName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); result.addLine(spanNotif); } result.setSummaryText(getString(R.string.push_notification_summary)); return result; }