List of usage examples for android.text SpannableString SpannableString
public SpannableString(CharSequence source)
From source file:com.google.zxing.client.android.result.supplement.SupplementalInfoRetriever.java
final void append(String itemID, String source, String[] newTexts, String linkURL) throws InterruptedException { final TextView textView = textViewRef.get(); if (textView == null) { throw new InterruptedException(); }//from w w w. java 2 s . c o m StringBuilder newTextCombined = new StringBuilder(); if (source != null) { newTextCombined.append(source).append(" : "); } int linkStart = newTextCombined.length(); boolean first = true; for (String newText : newTexts) { if (first) { newTextCombined.append(newText); first = false; } else { newTextCombined.append(" ["); newTextCombined.append(newText); newTextCombined.append(']'); } } int linkEnd = newTextCombined.length(); String newText = newTextCombined.toString(); final Spannable content = new SpannableString(newText + "\n\n"); if (linkURL != null) { content.setSpan(new URLSpan(linkURL), linkStart, linkEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } handler.post(new Runnable() { public void run() { textView.append(content); textView.setMovementMethod(LinkMovementMethod.getInstance()); } }); // Add the text to the history. historyManager.addHistoryItemDetails(itemID, newText); }
From source file:com.yahala.ui.GroupCreateActivity.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (fragmentView == null) { searching = false;/* w ww.j a v a 2 s .com*/ searchWas = false; fragmentView = inflater.inflate(R.layout.group_create_layout, container, false); emptyTextView = (TextView) fragmentView.findViewById(R.id.searchEmptyView); emptyTextView.setText(LocaleController.getString("NoContacts", R.string.NoContacts)); userSelectEditText = (EditText) fragmentView.findViewById(R.id.bubble_input_text); userSelectEditText.setHint(LocaleController.getString("SendMessageTo", R.string.SendMessageTo)); if (Build.VERSION.SDK_INT >= 11) { userSelectEditText.setTextIsSelectable(false); } userSelectEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { if (!ignoreChange) { beforeChangeIndex = userSelectEditText.getSelectionStart(); changeString = new SpannableString(charSequence); } } @Override public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { } @Override public void afterTextChanged(Editable editable) { if (!ignoreChange) { boolean search = false; int afterChangeIndex = userSelectEditText.getSelectionEnd(); if (editable.toString().length() < changeString.toString().length()) { String deletedString = ""; try { deletedString = changeString.toString().substring(afterChangeIndex, beforeChangeIndex); } catch (Exception e) { FileLog.e("tmessages", e); } if (deletedString.length() > 0) { if (searching && searchWas) { search = true; } Spannable span = userSelectEditText.getText(); for (int a = 0; a < allSpans.size(); a++) { XImageSpan sp = allSpans.get(a); if (span.getSpanStart(sp) == -1) { allSpans.remove(sp); selectedContacts.remove(sp.jid); } } if (parentActivity != null) { ActionBar actionBar = parentActivity.getSupportActionBar(); actionBar.setSubtitle(String.format("%d/200 %s", selectedContacts.size(), LocaleController.getString("Members", R.string.Members))); } listView.invalidateViews(); } else { search = true; } } else { search = true; } if (search) { String text = userSelectEditText.getText().toString().replace("<", ""); if (text.length() != 0) { searchDialogs(text); searching = true; searchWas = true; emptyTextView.setText(LocaleController.getString("NoResult", R.string.NoResult)); listViewAdapter.notifyDataSetChanged(); } else { searchResult = null; searchResultNames = null; searching = false; searchWas = false; emptyTextView .setText(LocaleController.getString("NoContacts", R.string.NoContacts)); listViewAdapter.notifyDataSetChanged(); } } } } }); listView = (PinnedHeaderListView) fragmentView.findViewById(R.id.listView); listView.setEmptyView(emptyTextView); listView.setVerticalScrollBarEnabled(false); listView.setAdapter(listViewAdapter = new ListAdapter(parentActivity)); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { TLRPC.User user; int section = listViewAdapter.getSectionForPosition(i); int row = listViewAdapter.getPositionInSectionForPosition(i); if (searching && searchWas) { user = searchResult.get(row); } else { ArrayList<TLRPC.User> arr = com.yahala.xmpp.ContactsController .getInstance().usersSectionsDict.get( com.yahala.xmpp.ContactsController.getInstance().sortedUsersSectionsArray .get(section)); user = com.yahala.xmpp.ContactsController.getInstance().friendsDict.get(arr.get(row).jid); listView.invalidateViews(); } if (selectedContacts.containsKey(user.jid)) { XImageSpan span = selectedContacts.get(user.jid); selectedContacts.remove(user.jid); SpannableStringBuilder text = new SpannableStringBuilder(userSelectEditText.getText()); text.delete(text.getSpanStart(span), text.getSpanEnd(span)); allSpans.remove(span); ignoreChange = true; userSelectEditText.setText(text); userSelectEditText.setSelection(text.length()); ignoreChange = false; } else { if (selectedContacts.size() == 200) { return; } ignoreChange = true; XImageSpan span = createAndPutChipForUser(user); span.jid = user.jid; ignoreChange = false; } if (parentActivity != null) { ActionBar actionBar = parentActivity.getSupportActionBar(); actionBar.setSubtitle(String.format("%d/200 %s", selectedContacts.size(), LocaleController.getString("Members", R.string.Members))); } if (searching || searchWas) { searching = false; searchWas = false; emptyTextView.setText(LocaleController.getString("NoContacts", R.string.NoContacts)); ignoreChange = true; SpannableStringBuilder ssb = new SpannableStringBuilder(""); for (ImageSpan sp : allSpans) { ssb.append("<<"); ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE); } userSelectEditText.setText(ssb); userSelectEditText.setSelection(ssb.length()); ignoreChange = false; listViewAdapter.notifyDataSetChanged(); } else { listView.invalidateViews(); } } }); } else { ViewGroup parent = (ViewGroup) fragmentView.getParent(); if (parent != null) { parent.removeView(fragmentView); } } return fragmentView; }
From source file:com.joyepay.qrcode.result.supplement.SupplementalInfoRetriever.java
final void append(String itemID, String source, String[] newTexts, String linkURL) throws InterruptedException { final TextView textView = textViewRef.get(); if (textView == null) { throw new InterruptedException(); }//ww w . ja v a2 s .co m StringBuilder newTextCombined = new StringBuilder(); if (source != null) { newTextCombined.append(source).append(" : "); } int linkStart = newTextCombined.length(); boolean first = true; for (String newText : newTexts) { if (first) { newTextCombined.append(newText); first = false; } else { newTextCombined.append(" ["); newTextCombined.append(newText); newTextCombined.append(']'); } } int linkEnd = newTextCombined.length(); String newText = newTextCombined.toString(); final Spannable content = new SpannableString(newText + "\n\n"); if (linkURL != null) { content.setSpan(new URLSpan(linkURL), linkStart, linkEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } handler.post(new Runnable() { public void run() { textView.append(content); textView.setMovementMethod(LinkMovementMethod.getInstance()); } }); // Add the text to the history. // historyManager.addHistoryItemDetails(itemID, newText); }
From source file:org.lytsing.android.weibo.ui.ComposeActivity.java
private void initGridView() { mGVFaceAdapter = new GridViewFaceAdapter(this); mGridView = (GridView) findViewById(R.id.tweet_pub_faces); mGridView.setAdapter(mGVFaceAdapter); mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // ?/*from w w w.j a v a 2 s . c o m*/ SpannableString ss = new SpannableString(view.getTag().toString()); Drawable d = getResources().getDrawable((int) mGVFaceAdapter.getItemId(position)); d.setBounds(0, 0, 35, 35);//? ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM); ss.setSpan(span, 0, view.getTag().toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // ? mEdit.getText().insert(mEdit.getSelectionStart(), ss); } }); }
From source file:com.android.settings.applications.ClearDefaultsPreference.java
public boolean updateUI(PreferenceViewHolder view) { boolean hasBindAppWidgetPermission = mAppWidgetManager .hasBindAppWidgetPermission(mAppEntry.info.packageName); TextView autoLaunchView = (TextView) view.findViewById(R.id.auto_launch); boolean autoLaunchEnabled = AppUtils.hasPreferredActivities(mPm, mPackageName) || isDefaultBrowser(mPackageName) || AppUtils.hasUsbDefaults(mUsbManager, mPackageName); if (!autoLaunchEnabled && !hasBindAppWidgetPermission) { resetLaunchDefaultsUi(autoLaunchView); } else {//from w ww.j a va 2s . com boolean useBullets = hasBindAppWidgetPermission && autoLaunchEnabled; if (hasBindAppWidgetPermission) { autoLaunchView.setText(R.string.auto_launch_label_generic); } else { autoLaunchView.setText(R.string.auto_launch_label); } Context context = getContext(); CharSequence text = null; int bulletIndent = context.getResources() .getDimensionPixelSize(R.dimen.installed_app_details_bullet_offset); if (autoLaunchEnabled) { CharSequence autoLaunchEnableText = context.getText(R.string.auto_launch_enable_text); SpannableString s = new SpannableString(autoLaunchEnableText); if (useBullets) { s.setSpan(new BulletSpan(bulletIndent), 0, autoLaunchEnableText.length(), 0); } text = (text == null) ? TextUtils.concat(s, "\n") : TextUtils.concat(text, "\n", s, "\n"); } if (hasBindAppWidgetPermission) { CharSequence alwaysAllowBindAppWidgetsText = context .getText(R.string.always_allow_bind_appwidgets_text); SpannableString s = new SpannableString(alwaysAllowBindAppWidgetsText); if (useBullets) { s.setSpan(new BulletSpan(bulletIndent), 0, alwaysAllowBindAppWidgetsText.length(), 0); } text = (text == null) ? TextUtils.concat(s, "\n") : TextUtils.concat(text, "\n", s, "\n"); } autoLaunchView.setText(text); mActivitiesButton.setEnabled(true); } return true; }
From source file:li.barter.AbstractBarterLiActivity.java
protected final void setActionBarTitle(final String title) { final SpannableString s = new SpannableString(title); s.setSpan(new TypefacedSpan(this, TypefaceCache.ALEGREYA_BLACK_ITALIC), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // Update the action bar title with the TypefaceSpan instance final ActionBar actionBar = getActionBar(); actionBar.setTitle(s);//from ww w . j ava 2 s . c om }
From source file:com.coderdojo.libretalk.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); appName = mTitle = getTitle();/* w w w. jav a 2s. c o m*/ getActionBar().setTitle(appName); // Read the User data from the database datasource = new DBManager(this); datasource.open(); userList = datasource.getAllUsers(); mFriendsListArray = new ArrayList<SpannableString>(); mMessageListArray = new ArrayList<SpannableString>(); for (int i = 0; i < userList.size(); i++) { mFriendsListArray.add(new SpannableString(userList.get(i).getProfile().getName())); } mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); friendlist = getResources().getString(R.string.friendlist); // set a custom shadow that overlays the main content when the drawer opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); // set up the drawer's list view with items and click listener mDrawerList .setAdapter(new ArrayAdapter<SpannableString>(this, R.layout.drawer_list_item, mFriendsListArray)); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); // enable ActionBar app icon to behave as action to toggle nav drawer getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); // ActionBarDrawerToggle ties together the the proper interactions // between the sliding drawer and the action bar app icon mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */ mDrawerLayout, /* DrawerLayout object */ R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ R.string.drawer_open, /* "open drawer" description for accessibility */ R.string.drawer_close /* "close drawer" description for accessibility */ ) { public void onDrawerClosed(View view) { getActionBar().setTitle(friendlist); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(appName); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { setTitle(friendlist); selectItem(0); } //XXX NETWORKING CODE BEGIN //[!] TODO: This is configured to use Android's localhost. Change the host value to whatever our server's address is. Random randomGenerator = new Random(); long tempuserhash = randomGenerator.nextLong(); String host = getString(R.string.ipaddress); Log.v("Libretalk", host); String user = getString(R.string.rabbituser); Log.v("Libretalk", user); String password = getString(R.string.rabbitpassword); Log.v("Libretalk", password); this.connection = new LibretalkConnection(host, user, password, tempuserhash); final ILibretalkMessageEventHandler eventHandler = new ILibretalkMessageEventHandler() { @Override public void onMessageReceived(final LibretalkMessageData message) { try { //printMsg(message.getSenderTag() + ": " + message.getData()); printMsg(message); } catch (Exception ex) { showErrDialog("Fatal Error! - " + ex.getClass().getSimpleName(), "An error has occurred while attempting to proccess a message from " + message.getSenderTag() + ".\n " + ex.getMessage(), ":'("); } } @Override public void onDisconnect(final Exception ex) { showErrDialog("Disconnected! - " + ex.getClass().getSimpleName(), "A network error has occurred and you have been disconnected! " + "Please check you are connected to the internet.", ":("); } }; this.receiver = new LibretalkMessageReceiver(this.connection, eventHandler); this.sender = new LibretalkMessageSender(this.connection); try { this.connection.connect(); this.receiver.startConsuming(); } catch (LibretalkNetworkException ex) { this.showErrDialog("Connection failure - " + ex.getClass().getSimpleName(), "Unfortunately, an error has occurred and we were unable to connect you to" + "the Libretalk Network, please check you are connected to the internet.", ":("); ex.printStackTrace(); return; } //XXX NETWORKING CODE END }
From source file:com.google.android.gm.ay.java
public static void a(final Context context, final String s, final String s2, final long n, final int n2) { final List<String> b = com.google.android.gm.persistence.b.DD().b(context, true); int size;//from ww w . ja va2s . co m if (b != null && b.size() > 0) { size = b.size(); } else { size = 1; } final String quantityString = context.getResources().getQuantityString(n2, size); final ab ab = new ab(context); if (size > 1) { final String string = context.getResources().getString(2131297166, new Object[] { s }); final SpannableString spannableString = new SpannableString( (CharSequence) String.format(quantityString, string)); final int index = spannableString.toString().indexOf(string); spannableString.setSpan((Object) new TextAppearanceSpan(context, 2131231210), index, index + string.length(), 0); ab.b((CharSequence) spannableString); ab.e((CharSequence) spannableString); } else { ab.b(quantityString); ab.e((CharSequence) quantityString); } final int hashCode = Arrays.hashCode(new Object[] { n, s2 }); final Account x = x(context, s); final Folder h = h(context, s, "^^out"); Intent a; if (h == null || s == null) { E.f(ay.mW, "Null account or folder. account: %s folder: %s", x, h); a = null; } else { a = ag.a(context, h.azZ.aPj, x); } final NotificationManager notificationManager = (NotificationManager) context .getSystemService("notification"); ab.h(17301624); ab.a(System.currentTimeMillis()); ab.c(s2); ab.a(PendingIntent.getActivity(context, -1, a, 0)); ArrayList<Integer> list = ay.aZj.get(s); if (list == null) { list = new ArrayList<Integer>(); } list.add(hashCode); ay.aZj.put(s, list); notificationManager.notify(hashCode, ab.build()); }
From source file:org.sirimangalo.meditationplus.AdapterChat.java
@Override public View getView(final int position, View convertView, ViewGroup parent) { View rowView;/*from w ww .j a v a 2 s. co m*/ if (convertView == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); rowView = inflater.inflate(R.layout.list_item_chat, parent, false); } else { rowView = convertView; } JSONObject p = values.get(position); try { int then = Integer.parseInt(p.getString("time")); long nowL = System.currentTimeMillis() / 1000; int now = (int) nowL; int ela = now - then; int day = 60 * 60 * 24; ela = ela > day ? day : ela; int intColor = 255 - Math.round((float) ela * 255 / day); intColor = intColor > 100 ? intColor : 100; String hexTransparency = Integer.toHexString(intColor); hexTransparency = hexTransparency.length() > 1 ? hexTransparency : "0" + hexTransparency; String hexColor = "#" + hexTransparency + "000000"; int transparency = Color.parseColor(hexColor); TextView time = (TextView) rowView.findViewById(R.id.time); if (time != null) { String ts = null; ts = Utils.time2Ago(then); time.setText(ts); time.setTextColor(transparency); } TextView mess = (TextView) rowView.findViewById(R.id.message); if (mess != null) { final String username = p.getString("username"); String messageString = p.getString("message"); messageString = Html.fromHtml(messageString).toString(); SpannableString messageSpan = Utils.replaceSmilies(context, messageString, intColor); if (messageString.contains("@" + loggedUser)) { int index = messageString.indexOf("@" + loggedUser); messageSpan.setSpan(new StyleSpan(Typeface.BOLD), index, index + loggedUser.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); messageSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#" + hexTransparency + "990000")), index, index + loggedUser.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } for (String user : users) { if (!user.equals(loggedUser) && messageString.contains("@" + user)) { int index = messageString.indexOf("@" + user); messageSpan = Utils.createProfileSpan(context, index, index + user.length() + 1, user, messageSpan); messageSpan.setSpan( new ForegroundColorSpan(Color.parseColor("#" + hexTransparency + "009900")), index, index + user.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } Spannable userSpan = Utils.createProfileSpan(context, 0, username.length(), username, new SpannableString(username + ": ")); if (p.getString("me").equals("true")) userSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#" + hexTransparency + "0000FF")), 0, userSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); else userSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#" + hexTransparency + "000000")), 0, userSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); CharSequence full = TextUtils.concat(userSpan, messageSpan); mess.setTextColor(transparency); mess.setText(full); mess.setMovementMethod(LinkMovementMethod.getInstance()); Linkify.addLinks(mess, Linkify.ALL); mess.setTag(p.getString("cid")); } } catch (JSONException e) { e.printStackTrace(); } return rowView; }
From source file:im.vector.adapters.VectorSearchMessagesListAdapter.java
@Override public View getView(int position, View convertView2, ViewGroup parent) { View convertView = super.getView(position, convertView2, parent); try {//from www. ja va2 s . com MessageRow row = getItem(position); Event event = row.getEvent(); // some items are always hidden convertView.findViewById(R.id.messagesAdapter_avatars_list).setVisibility(View.GONE); convertView.findViewById(R.id.messagesAdapter_message_separator).setVisibility(View.GONE); convertView.findViewById(R.id.messagesAdapter_action_image).setVisibility(View.GONE); convertView.findViewById(R.id.messagesAdapter_top_margin_when_no_room_name) .setVisibility(mDisplayRoomName ? View.GONE : View.VISIBLE); convertView.findViewById(R.id.messagesAdapter_message_header).setVisibility(View.GONE); Room room = mSession.getDataHandler().getStore().getRoom(event.roomId); RoomState roomState = row.getRoomState(); if (null == roomState) { roomState = room.getLiveState(); } // refresh the avatar ImageView avatarView = convertView.findViewById(R.id.messagesAdapter_roundAvatar) .findViewById(R.id.avatar_img); mHelper.loadMemberAvatar(avatarView, row); // display the sender TextView senderTextView = convertView.findViewById(R.id.messagesAdapter_sender); if (senderTextView != null) { senderTextView .setText(VectorMessagesAdapterHelper.getUserDisplayName(event.getSender(), roomState)); } // display the body TextView bodyTextView = convertView.findViewById(R.id.messagesAdapter_body); // set the message text EventDisplay display = new RiotEventDisplay(mContext, event, (null != room) ? room.getLiveState() : null); CharSequence text = display.getTextualDisplay(); if (null == text) { text = ""; } try { CharSequence strBuilder = mHelper.highlightPattern(new SpannableString(text), mPattern, mBackgroundColorSpan, false); bodyTextView.setText(strBuilder); mHelper.applyLinkMovementMethod(bodyTextView); } catch (Exception e) { // an exception might be triggered with HTML content // Indeed, the formatting can fail because of the single line display. // in this case, the formatting is ignored. bodyTextView.setText(text.toString()); } // display timestamp TextView timeTextView = convertView.findViewById(R.id.messagesAdapter_timestamp); timeTextView.setText(AdapterUtils.tsToString(mContext, event.getOriginServerTs(), true)); // display the room name View roomNameLayout = convertView.findViewById(R.id.messagesAdapter_message_room_name_layout); roomNameLayout.setVisibility(mDisplayRoomName ? View.VISIBLE : View.GONE); if (mDisplayRoomName) { TextView roomTextView = convertView.findViewById(R.id.messagesAdapter_message_room_name_textview); roomTextView.setText(VectorUtils.getRoomDisplayName(mContext, mSession, room)); } // display the day View dayLayout = convertView.findViewById(R.id.messagesAdapter_search_message_day_separator); // day separator String headerMessage = headerMessage(position); if (!TextUtils.isEmpty(headerMessage)) { dayLayout.setVisibility(View.VISIBLE); TextView headerText = convertView.findViewById(R.id.messagesAdapter_message_header_text); headerText.setText(headerMessage); dayLayout.findViewById(R.id.messagesAdapter_message_header_top_margin).setVisibility(View.GONE); dayLayout.findViewById(R.id.messagesAdapter_message_header_bottom_margin).setVisibility(View.GONE); } else { dayLayout.setVisibility(View.GONE); } // message separator is only displayed when a message is not the last message in a day section convertView.findViewById(R.id.messagesAdapter_search_separator_line) .setVisibility(!TextUtils.isEmpty(headerMessage(position + 1)) ? View.GONE : View.VISIBLE); final int fPosition = position; convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (null != mVectorMessagesAdapterEventsListener) { mVectorMessagesAdapterEventsListener.onContentClick(fPosition); } } }); convertView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if (null != mVectorMessagesAdapterEventsListener) { return mVectorMessagesAdapterEventsListener.onContentLongClick(fPosition); } return false; } }); } catch (Throwable t) { Log.e(LOG_TAG, "## getView() failed " + t.getMessage()); } return convertView; }