]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Refactor adding bold spans.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 863e20b81bffcaaefa20935baef6ac467af99715..8123c9d6db037e58c0d175bc0c7347f9e9e00f78 100644 (file)
@@ -1582,6 +1582,15 @@ public class DictionaryActivity extends ActionBarActivity {
             }
         }
 
+        private void addBoldSpans(String token, String col1Text, Spannable col1Spannable) {
+            int startPos = 0;
+            while ((startPos = col1Text.indexOf(token, startPos)) != -1) {
+                col1Spannable.setSpan(new StyleSpan(Typeface.BOLD), startPos, startPos
+                                      + token.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
+                startPos += token.length();
+            }
+        }
+
         private TableLayout getView(final int position, PairEntry.Row row, ViewGroup parent,
                                     TableLayout result) {
             final Context context = parent.getContext();
@@ -1662,16 +1671,12 @@ public class DictionaryActivity extends ActionBarActivity {
                 final Spannable col2Spannable = new SpannableString(col2Text);
 
                 // Bold the token instances in col1.
-                final Set<String> toBold = toHighlight != null ? this.toHighlight : Collections
-                                           .singleton(row.getTokenRow(true).getToken());
-                for (final String token : toBold) {
-                    int startPos = 0;
-                    while ((startPos = col1Text.indexOf(token, startPos)) != -1) {
-                        col1Spannable.setSpan(new StyleSpan(Typeface.BOLD), startPos, startPos
-                                              + token.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
-                        startPos += token.length();
+                if (toHighlight != null) {
+                    for (final String token : toHighlight) {
+                        addBoldSpans(token, col1Text, col1Spannable);
                     }
-                }
+                } else
+                    addBoldSpans(row.getTokenRow(true).getToken(), col1Text, col1Spannable);
 
                 createTokenLinkSpans(col1, col1Spannable, col1Text);
                 createTokenLinkSpans(col2, col2Spannable, col2Text);
@@ -1726,7 +1731,6 @@ public class DictionaryActivity extends ActionBarActivity {
             // Make it so we can long-click on these token rows, too:
             final Spannable textSpannable = new SpannableString(text);
             createTokenLinkSpans(textView, textSpannable, text);
-            textView.setText(textSpannable);
 
             if (!htmlEntries.isEmpty()) {
                 final ClickableSpan clickableSpan = new ClickableSpan() {
@@ -1734,7 +1738,7 @@ public class DictionaryActivity extends ActionBarActivity {
                     public void onClick(View widget) {
                     }
                 };
-                ((Spannable) textView.getText()).setSpan(clickableSpan, 0, text.length(),
+                textSpannable.setSpan(clickableSpan, 0, text.length(),
                         Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                 textView.setOnClickListener(new OnClickListener() {
                     @Override
@@ -1743,6 +1747,7 @@ public class DictionaryActivity extends ActionBarActivity {
                     }
                 });
             }
+            textView.setText(textSpannable);
             return textView;
         }
 
@@ -1772,7 +1777,7 @@ public class DictionaryActivity extends ActionBarActivity {
         textView.setMovementMethod(LinkMovementMethod.getInstance());
         final Matcher matcher = CHAR_DASH.matcher(text);
         while (matcher.find()) {
-            spannable.setSpan(new NonLinkClickableSpan(textColorFg), matcher.start(),
+            spannable.setSpan(new NonLinkClickableSpan(), matcher.start(),
                               matcher.end(),
                               Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
         }