List of usage examples for android.text.style ClickableSpan ClickableSpan
ClickableSpan
From source file:org.sirimangalo.meditationplus.ActivityMain.java
private void populateOnline(JSONArray onlines) { if (onlines.length() == 0) { onlineList.setVisibility(View.GONE); return;/* w ww . j a va 2s . c o m*/ } onlineList.setVisibility(View.VISIBLE); ArrayList<JSONObject> onlineArray = new ArrayList<JSONObject>(); ArrayList<String> onlineNamesArray = new ArrayList<String>(); // collect into array for (int i = 0; i < onlines.length(); i++) { try { JSONObject a = onlines.getJSONObject(i); onlineArray.add(a); onlineNamesArray.add(a.getString("username")); } catch (JSONException e) { e.printStackTrace(); } } String text = getString(R.string.online) + " "; // add spans int pos = text.length(); // start after "Online: " text += TextUtils.join(", ", onlineNamesArray); Spannable span = new SpannableString(text); span.setSpan(new StyleSpan(Typeface.BOLD), 0, pos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // bold the "Online: " Drawable android = context.getResources().getDrawable(R.drawable.android); android.setBounds(0, 0, 48, 32); for (JSONObject oneOnA : onlineArray) { try { final String oneOn = oneOnA.getString("username"); int end = pos + oneOn.length(); boolean isMed = false; for (int j = 0; j < jsonList.length(); j++) { JSONObject user = jsonList.getJSONObject(j); String username = user.getString("username"); if (username.equals(oneOn)) isMed = true; } if (oneOnA.getString("source").equals("android")) { ImageSpan image = new ImageSpan(android, ImageSpan.ALIGN_BASELINE); span.setSpan(image, pos - 1, pos, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } ClickableSpan clickable = new ClickableSpan() { @Override public void onClick(View widget) { showProfile(oneOn); } }; span.setSpan(clickable, pos, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); span.setSpan(new UnderlineSpan() { public void updateDrawState(TextPaint tp) { tp.setUnderlineText(false); } }, pos, end, 0); span.setSpan(new ForegroundColorSpan(isMed ? 0xFF009900 : 0xFFFF9900), pos, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); pos += oneOn.length() + 2; } catch (JSONException e) { e.printStackTrace(); } } onlineList.setText(span); onlineList.setMovementMethod(LinkMovementMethod.getInstance()); }
From source file:ru.valle.btc.MainActivity.java
private CharSequence getPrivateKeyTypeLabel(final KeyPair keyPair) { int typeWithCompression = keyPair.privateKey.type == BTCUtils.PrivateKeyInfo.TYPE_BRAIN_WALLET && keyPair.privateKey.isPublicKeyCompressed ? keyPair.privateKey.type + 1 : keyPair.privateKey.type; CharSequence keyType = getResources().getTextArray(R.array.private_keys_types)[typeWithCompression]; SpannableString keyTypeLabel = new SpannableString(getString(R.string.private_key_type, keyType)); int keyTypeStart = keyTypeLabel.toString().indexOf(keyType.toString()); keyTypeLabel.setSpan(new StyleSpan(Typeface.BOLD), keyTypeStart, keyTypeStart + keyType.length(), SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); if (keyPair.privateKey.type == BTCUtils.PrivateKeyInfo.TYPE_BRAIN_WALLET) { String compressionStrToSpan = keyType.toString().substring(keyType.toString().indexOf(',') + 2); int start = keyTypeLabel.toString().indexOf(compressionStrToSpan); if (start >= 0) { ClickableSpan switchPublicKeyCompressionSpan = new ClickableSpan() { @Override/*from www .j a va2s . com*/ public void onClick(View widget) { cancelAllRunningTasks(); switchingCompressionTypeTask = new AsyncTask<Void, Void, KeyPair>() { @Override protected KeyPair doInBackground(Void... params) { return new KeyPair(new BTCUtils.PrivateKeyInfo(keyPair.privateKey.type, keyPair.privateKey.privateKeyEncoded, keyPair.privateKey.privateKeyDecoded, !keyPair.privateKey.isPublicKeyCompressed)); } @Override protected void onPostExecute(KeyPair keyPair) { switchingCompressionTypeTask = null; onKeyPairModify(false, keyPair); } }; switchingCompressionTypeTask.execute(); } }; keyTypeLabel.setSpan(switchPublicKeyCompressionSpan, start, start + compressionStrToSpan.length(), SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); } } return keyTypeLabel; }
From source file:ru.valle.btc.MainActivity.java
private void showSpendPanelForKeyPair(KeyPair keyPair) { if (keyPair != null && keyPair.privateKey.privateKeyDecoded == null) { keyPair = null;/*from ww w . j a v a 2 s . c om*/ } if (keyPair != null && !TextUtils.isEmpty(keyPair.address)) { currentKeyPair = keyPair; final String address = keyPair.address; String descStr = getString(R.string.raw_tx_description_header, address); SpannableStringBuilder builder = new SpannableStringBuilder(descStr); int spanBegin = descStr.indexOf(address); if (spanBegin >= 0) { ForegroundColorSpan addressColorSpan = new ForegroundColorSpan( getColor(MainActivity.this, R.color.dark_orange)); builder.setSpan(addressColorSpan, spanBegin, spanBegin + address.length(), SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); } rawTxDescriptionHeaderView.setText(builder); String wutLink = getString(R.string.raw_tx_description_wut_link); String jsonLink = getString(R.string.raw_tx_description_json_link); builder = new SpannableStringBuilder(getString(R.string.raw_tx_description, wutLink, jsonLink)); spanBegin = builder.toString().indexOf(wutLink); ClickableSpan urlSpan = new ClickableSpan() { @Override public void onClick(View widget) { SpannableStringBuilder builder = new SpannableStringBuilder( getText(R.string.raw_tx_description_wut)); setUrlSpanForAddress("blockexplorer.com", address, builder); setUrlSpanForAddress("blockchain.info", address, builder); TextView messageView = new TextView(MainActivity.this); messageView.setText(builder); messageView.setMovementMethod(LinkMovementMethod.getInstance()); int padding = (int) (16 * (getResources().getDisplayMetrics().densityDpi / 160f)); messageView.setPadding(padding, padding, padding, padding); new AlertDialog.Builder(MainActivity.this).setView(messageView) .setPositiveButton(android.R.string.ok, null).show(); } }; builder.setSpan(urlSpan, spanBegin, spanBegin + wutLink.length(), SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); spanBegin = builder.toString().indexOf(jsonLink); urlSpan = new URLSpan("http://blockchain.info/unspent?active=" + address); builder.setSpan(urlSpan, spanBegin, spanBegin + jsonLink.length(), SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); rawTxDescriptionView.setText(builder); rawTxDescriptionView.setMovementMethod(LinkMovementMethod.getInstance()); onUnspentOutputsInfoChanged(); } sendLayout.setVisibility(keyPair != null ? View.VISIBLE : View.GONE); enterPrivateKeyAck.setVisibility(keyPair == null ? View.VISIBLE : View.GONE); }
From source file:cgeo.geocaching.CacheDetailActivity.java
static void appendClickableList(final SpannableStringBuilder builder, final View view, final Integer listId) { final int start = builder.length(); builder.append(DataStore.getList(listId).getTitle()); builder.setSpan(new ClickableSpan() { @Override// ww w.ja va 2 s .c o m public void onClick(final View widget) { Settings.setLastDisplayedList(listId); CacheListActivity.startActivityOffline(view.getContext()); } }, start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); }
From source file:org.tvbrowser.tvbrowser.TvBrowser.java
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) { int start = strBuilder.getSpanStart(span); int end = strBuilder.getSpanEnd(span); int flags = strBuilder.getSpanFlags(span); ClickableSpan clickable = new ClickableSpan() { public void onClick(View view) { if (!mLoadingPlugin) { mLoadingPlugin = true;/*from w ww .j a v a 2s. c om*/ String url = span.getURL(); if (url.startsWith("http://play.google.com/store/apps/details?id=")) { try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url.replace("http://play.google.com/store/apps", "market:/")))); } catch (android.content.ActivityNotFoundException anfe) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); } mLoadingPlugin = false; } else if (url.startsWith("plugin://") || url.startsWith("plugins://")) { final File path = IOUtils.getDownloadDirectory(getApplicationContext()); if (!path.isDirectory()) { path.mkdirs(); } if (url.startsWith("plugin://")) { url = url.replace("plugin://", "http://"); } else if (url.startsWith("plugins://")) { url = url.replace("plugins://", "https://"); } String name = url.substring(url.lastIndexOf("/") + 1); mCurrentDownloadPlugin = new File(path, name); if (mCurrentDownloadPlugin.isFile()) { mCurrentDownloadPlugin.delete(); } final String downloadUrl = url; handler.post(new Runnable() { @Override public void run() { AsyncTask<String, Void, Boolean> async = new AsyncTask<String, Void, Boolean>() { private ProgressDialog mProgress; private File mPluginFile; protected void onPreExecute() { mProgress = new ProgressDialog(TvBrowser.this); mProgress.setMessage(getString(R.string.plugin_info_donwload).replace("{0}", mCurrentDownloadPlugin.getName())); mProgress.show(); }; @Override protected Boolean doInBackground(String... params) { mPluginFile = new File(params[0]); return IOUtils.saveUrl(params[0], params[1], 15000); } protected void onPostExecute(Boolean result) { mProgress.dismiss(); if (result) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(mPluginFile), "application/vnd.android.package-archive"); TvBrowser.this.startActivityForResult(intent, INSTALL_PLUGIN); } mLoadingPlugin = false; }; }; async.execute(mCurrentDownloadPlugin.toString(), downloadUrl); } }); } else { mLoadingPlugin = false; } } } }; strBuilder.setSpan(clickable, start, end, flags); strBuilder.removeSpan(span); }