Java Code Examples for android.text.Spanned#subSequence()

The following examples show how to use android.text.Spanned#subSequence() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utilities.java    From google-authenticator-android with Apache License 2.0 6 votes vote down vote up
private static Spanned removeTrailingNewlines(Spanned text) {
  int trailingNewlineCharacterCount = 0;
  for (int i = text.length() - 1; i >= 0; i--) {
    char c = text.charAt(i);
    if ((c == '\n') || (c == '\r')) {
      trailingNewlineCharacterCount++;
    } else {
      break;
    }
  }
  if (trailingNewlineCharacterCount == 0) {
    return text;
  }

  return new SpannedString(
      text.subSequence(0, text.length() - trailingNewlineCharacterCount));
}
 
Example 2
Source File: NumberPicker.java    From screenstandby with GNU General Public License v2.0 6 votes vote down vote up
public CharSequence filter(CharSequence source, int start, int end,
        Spanned dest, int dstart, int dend) {
    if (mDisplayedValues == null) {
        return mNumberInputFilter.filter(source, start, end, dest, dstart, dend);
    }
    CharSequence filtered = String.valueOf(source.subSequence(start, end));
    String result = String.valueOf(dest.subSequence(0, dstart))
            + filtered
            + dest.subSequence(dend, dest.length());
    String str = String.valueOf(result).toLowerCase();
    for (String val : mDisplayedValues) {
        val = val.toLowerCase();
        if (val.startsWith(str)) {
            return filtered;
        }
    }
    return "";
}
 
Example 3
Source File: TaskListActivity.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(@NonNull View widget) {
    // toggle span (this is a mere visual change)
    span.setDone(!span.isDone());
    // request visual update
    widget.invalidate();

    // it must be a TextView
    final TextView textView = (TextView) widget;
    // it must be spanned
    final Spanned spanned = (Spanned) textView.getText();

    // actual text of the span (this can be used along with the  `span`)
    final CharSequence task = spanned.subSequence(
            spanned.getSpanStart(this),
            spanned.getSpanEnd(this)
    );

    Debug.i("task done: %s, '%s'", span.isDone(), task);
}
 
Example 4
Source File: ItemPicker.java    From libcommon with Apache License 2.0 6 votes vote down vote up
@Override
public CharSequence filter(final CharSequence source, final int start, final int end,
              final Spanned dest, final int dstart, final int dend) {
          if (mDisplayedValues == null) {
              return mNumberInputFilter.filter(source, start, end, dest, dstart, dend);
          }
          final CharSequence filtered = String.valueOf(source.subSequence(start, end));
          final String result = String.valueOf(dest.subSequence(0, dstart))
                  + filtered
                  + dest.subSequence(dend, dest.length());
          final String str = String.valueOf(result).toLowerCase(Locale.US);
          for (String val : mDisplayedValues) {
              val = val.toLowerCase(Locale.US);
              if (val.startsWith(str)) {
                  return filtered;
              }
          }
          return "";
      }
 
Example 5
Source File: HighlightingEditor.java    From writeily-pro with MIT License 6 votes vote down vote up
private String createIndentForNextLine(Spanned dest, int dend, int istart) {
    if (istart > -1) {
        int iend;

        for (iend = ++istart;
             iend < dend;
             ++iend) {
            char c = dest.charAt(iend);

            if (c != ' ' &&
                    c != '\t') {
                break;
            }
        }

        return dest.subSequence(istart, iend) + addBulletPointIfNeeded(dest.charAt(iend));
    } else {
        return "";
    }
}
 
Example 6
Source File: FileUtil.java    From android-file-chooser with Apache License 2.0 6 votes vote down vote up
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    Matcher matcher = pattern.matcher(source);
    if (!matcher.matches()) {
        return source instanceof SpannableStringBuilder ? dest.subSequence(dstart, dend) : "";
    }

    int keep = maxLength - (dest.length() - (dend - dstart));
    if (keep <= 0) {
        return "";
    } else if (keep >= end - start) {
        return null; // keep original
    } else {
        keep += start;
        if (Character.isHighSurrogate(source.charAt(keep - 1))) {
            --keep;
            if (keep == start) {
                return "";
            }
        }
        return source.subSequence(start, keep).toString();
    }
}
 
Example 7
Source File: InputFilterMinMax.java    From geopackage-mapcache-android with MIT License 6 votes vote down vote up
@Override
public CharSequence filter(CharSequence source, int start, int end,
		Spanned dest, int dstart, int dend) {
	try {
		String value = dest.subSequence(0, dstart).toString()
				+ source.subSequence(start, end)
				+ dest.subSequence(dend, dest.length());
		if (value.isEmpty()) {
			return null;
		}
		int input = Integer.parseInt(value);
		if (isInRange(min, max, input)) {
			return null;
		}
	} catch (NumberFormatException nfe) {
	}
	return dest.subSequence(dstart, dend);
}
 
Example 8
Source File: NumberPicker.java    From financisto with GNU General Public License v2.0 6 votes vote down vote up
public CharSequence filter(CharSequence source, int start, int end,
        Spanned dest, int dstart, int dend) {
    if (mDisplayedValues == null) {
        return mNumberInputFilter.filter(source, start, end, dest, dstart, dend);
    }
    CharSequence filtered = String.valueOf(source.subSequence(start, end));
    String result = String.valueOf(dest.subSequence(0, dstart))
            + filtered
            + dest.subSequence(dend, dest.length());
    String str = String.valueOf(result).toLowerCase();
    for (String val : mDisplayedValues) {
        val = val.toLowerCase();
        if (val.startsWith(str)) {
            return filtered;
        }
    }
    return "";
}
 
Example 9
Source File: InputFilterDecimalMinMax.java    From geopackage-mapcache-android with MIT License 6 votes vote down vote up
@Override
public CharSequence filter(CharSequence source, int start, int end,
		Spanned dest, int dstart, int dend) {
	try {
		String value = dest.subSequence(0, dstart).toString()
				+ source.subSequence(start, end)
				+ dest.subSequence(dend, dest.length());
		if (value.isEmpty() || value.equals("-")) {
			return null;
		}
		double input = Double.parseDouble(value);
		if (isInRange(min, max, input)) {
			return null;
		}
	} catch (NumberFormatException nfe) {
	}
	return dest.subSequence(dstart, dend);
}
 
Example 10
Source File: WordTokenizer.java    From Spyglass with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure the the character before the explicit character is a word-breaking character.
 * Note that the function only uses the input string to determine which explicit character was
 * typed. It uses the complete contents of the {@link EditText} to determine if there is a
 * word-breaking character before the explicit character, as the input string may start with an
 * explicit character, i.e. with an input string of "@John Doe" and an {@link EditText} containing
 * the string "Hello @John Doe", this should return true.
 *
 * @param text   the {@link Spanned} to check for a word-breaking character before the explicit character
 * @param cursor position of the cursor in text
 *
 * @return true if there is a space before the explicit character, false otherwise
 */
protected boolean hasWordBreakingCharBeforeExplicitChar(final @NonNull Spanned text, final int cursor) {
    CharSequence beforeCursor = text.subSequence(0, cursor);
    // Get the explicit character closest before the cursor and make sure it
    // has a word-breaking character in front of it
    int i = cursor - 1;
    while (i >= 0 && i < beforeCursor.length()) {
        char c = beforeCursor.charAt(i);
        if (isExplicitChar(c)) {
            return i == 0 || isWordBreakingChar(beforeCursor.charAt(i - 1));
        }
        i--;
    }
    return false;
}
 
Example 11
Source File: NumberPicker.java    From screenstandby with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CharSequence filter(CharSequence source, int start, int end,
        Spanned dest, int dstart, int dend) {

    CharSequence filtered = super.filter(source, start, end, dest, dstart, dend);
    if (filtered == null) {
        filtered = source.subSequence(start, end);
    }

    String result = String.valueOf(dest.subSequence(0, dstart))
            + filtered
            + dest.subSequence(dend, dest.length());

    if ("".equals(result)) {
        return result;
    }
    int val = getSelectedPos(result);

    /* Ensure the user can't type in a value greater
     * than the max allowed. We have to allow less than min
     * as the user might want to delete some numbers
     * and then type a new number.
     */
    if (val > mEnd) {
        return "";
    } else {
        return filtered;
    }
}
 
Example 12
Source File: InputFilterDecimal.java    From mage-android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public CharSequence filter(CharSequence source, int start, int end,
                           Spanned dest, int dstart, int dend) {

    String value = dest.subSequence(0, dstart).toString()
            + source.subSequence(start, end)
            + dest.subSequence(dend, dest.length());
    if (value.isEmpty() || value.equals("-")) {
        return null;
    }
    double input;
    try {
        input = Double.parseDouble(value);
    } catch (NumberFormatException nfe) {
        return dest.subSequence(dstart, dend);
    }
    if (min != null && min > input
            || max != null && max < input) {
        return dest.subSequence(dstart, dend);
    }

    if (precision != null) {
        int decimalIndex = value.indexOf(".");
        if (decimalIndex > -1 && value.length() - decimalIndex - 1 > precision) {
            return dest.subSequence(dstart, dend);
        }
    }

    return null;
}
 
Example 13
Source File: DonateDialogFragment.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
private void handleValueInputFiltering() {
  imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
  editTextFilter = new InputFilter() {
    final int maxDigitsBeforeDecimalPoint = 6;
    final int maxDigitsAfterDecimalPoint = 2;

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
        int dend) {
      StringBuilder builder = new StringBuilder(dest);
      builder.replace(dstart, dend, source.subSequence(start, end)
          .toString());
      if (!builder.toString()
          .matches("(([1-9]{1})([0-9]{0,"
              + (maxDigitsBeforeDecimalPoint - 1)
              + "})?)?(\\.[0-9]{0,"
              + maxDigitsAfterDecimalPoint
              + "})?"

          )) {
        if (source.length() == 0) return dest.subSequence(dstart, dend);
        return "";
      }

      return null;
    }
  };
}
 
Example 14
Source File: ItemPicker.java    From libcommon with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence filter(final CharSequence source, final int start, final int end,
        final Spanned dest, final int dstart, final int dend) {

    CharSequence filtered = super.filter(source, start, end, dest, dstart, dend);
    if (filtered == null) {
        filtered = source.subSequence(start, end);
    }

    final String result = String.valueOf(dest.subSequence(0, dstart))
            + filtered
            + dest.subSequence(dend, dest.length());

    if ("".equals(result)) {
        return result;
    }
    final int val = getSelectedPos(result);

    /* Ensure the user can't type in a value greater
     * than the max allowed. We have to allow less than min
     * as the user might want to delete some numbers
     * and then type a new number.
     */
    if (val > mMaxValue) {
        return "";
    } else {
        return filtered;
    }
}
 
Example 15
Source File: NumberPicker.java    From financisto with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CharSequence filter(CharSequence source, int start, int end,
        Spanned dest, int dstart, int dend) {

    CharSequence filtered = super.filter(source, start, end, dest, dstart, dend);
    if (filtered == null) {
        filtered = source.subSequence(start, end);
    }

    String result = String.valueOf(dest.subSequence(0, dstart))
            + filtered
            + dest.subSequence(dend, dest.length());

    if ("".equals(result)) {
        return result;
    }
    int val = getSelectedPos(result);

    /* Ensure the user can't type in a value greater
     * than the max allowed. We have to allow less than min
     * as the user might want to delete some numbers
     * and then type a new number.
     */
    if (val > mEnd) {
        return "";
    } else {
        return filtered;
    }
}
 
Example 16
Source File: WordTokenizer.java    From Spyglass with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean isValidMention(final @NonNull Spanned text, final int start, final int end) {
    // Get the token
    CharSequence token = text.subSequence(start, end);

    // Null or empty string is not a valid mention
    if (TextUtils.isEmpty(token)) {
        return false;
    }

    // Handle explicit mentions first, then implicit mentions
    final int threshold = mConfig.THRESHOLD;
    boolean multipleWords = containsWordBreakingChar(token);
    boolean containsExplicitChar = containsExplicitChar(token);

    if (!multipleWords && containsExplicitChar) {

        // If it is one word and has an explicit char, the explicit char must be the first char
        if (!isExplicitChar(token.charAt(0))) {
            return false;
        }

        // Ensure that the character before the explicit character is a word-breaking character
        // Note: Checks the explicit character closest in front of the cursor
        if (!hasWordBreakingCharBeforeExplicitChar(text, end)) {
            return false;
        }

        // Return true if string is just an explicit character
        if (token.length() == 1) {
            return true;
        }

        // If input has length greater than one, the second character must be a letter or digit
        // Return true if and only if second character is a letter or digit, i.e. "@d"
        return Character.isLetterOrDigit(token.charAt(1));

    } else if (token.length() >= threshold) {

        // Change behavior depending on if keywords is one or more words
        if (!multipleWords) {
            // One word, no explicit characters
            // input is only one word, i.e. "u41"
            return onlyLettersOrDigits(token, threshold, 0);
        } else if (containsExplicitChar) {
            // Multiple words, has explicit character
            // Must have a space, the explicit character, then a letter or digit
            return hasWordBreakingCharBeforeExplicitChar(text, end)
                    && isExplicitChar(token.charAt(0))
                    && Character.isLetterOrDigit(token.charAt(1));
        } else {
            // Multiple words, no explicit character
            // Either the first or last couple of characters must be letters/digits
            boolean firstCharactersValid = onlyLettersOrDigits(token, threshold, 0);
            boolean lastCharactersValid = onlyLettersOrDigits(token, threshold, token.length() - threshold);
            return firstCharactersValid || lastCharactersValid;
        }
    }

    return false;
}
 
Example 17
Source File: EditInputFilterPrice.java    From Android with MIT License 4 votes vote down vote up
@Override
public CharSequence filter(CharSequence src, int start, int end,
                           Spanned dest, int dstart, int dend) {
    String oldtext =  dest.toString();
    System.out.println(oldtext);
    if ("".equals(src.toString())) {
        return null;
    }
    Matcher m = p.matcher(src);
    if(oldtext.contains(".")){
        if(!m.matches()){
            return null;
        }
    }else{
        if(!m.matches() && !src.equals(".") ){
            return null;
        }
    }
    if(!src.toString().equals("") && RegularUtil.matches(oldtext, RegularUtil.VERIFICATION_AMOUT)) {
        if((oldtext+src.toString()).equals(".")){
            return "";
        }
        StringBuffer oldStr = new StringBuffer(oldtext);
        oldStr.insert(dstart,src + "");
        double dold = Double.parseDouble(oldStr.toString());
        if(dold > maxValue){
            return dest.subSequence(dstart, dend);
        }else if(dold == maxValue){
            if(src.toString().equals(".")){
                return dest.subSequence(dstart, dend);
            }
        }
    }
    if(oldtext.contains(".")){
        int index = oldtext.indexOf(".");
        int len = oldtext.length() - 1 - index;
        if(index < dstart){
            len ++;
        }

        if(len > pontintLength){
            CharSequence newText = dest.subSequence(dstart, dend);
            return newText;
        }
    }
    return dest.subSequence(dstart, dend) +src.toString();
}
 
Example 18
Source File: ComponentDigitCtrlFilter.java    From Android with MIT License 4 votes vote down vote up
/**
 *  source    The new input string
 *  start    New start index to the string that is input, average is 0
 *  end    The new input string at the end of the subscript, general source in length - 1
 *  dest    Before the input text box
 *  dstart    The original contents starting coordinates, general is 0
 *  dend    The original contents coordinates, generally for the dest length - 1
 */

@Override
public CharSequence filter(CharSequence src, int start, int end,
                           Spanned dest, int dstart, int dend) {
    String oldtext =  dest.toString();
    System.out.println(oldtext);
    if ("".equals(src.toString())) {
        return null;
    }
    Matcher m = p.matcher(src);
    if(oldtext.contains(".")){
        if(!m.matches()){
            return null;
        }
    }else{
        if(!m.matches() && !src.equals(".") ){
            return null;
        }
    }
    if(!src.toString().equals("")){
        if((oldtext+src.toString()).equals(".")){
            return "";
        }
        StringBuffer oldStr = new StringBuffer(oldtext);
        oldStr.insert(dstart,src + "");
        double dold = Double.parseDouble(oldStr.toString());
        if(dold > maxValue){
            return dest.subSequence(dstart, dend);
        }else if(dold == maxValue){
            if(src.toString().equals(".")){
                return dest.subSequence(dstart, dend);
            }
        }
    }
    if(oldtext.contains(".")){
        int index = oldtext.indexOf(".");
        int len = oldtext.length()-1 + end - index;

        if(len > pontintLength){
            CharSequence newText = dest.subSequence(dstart, dend);
            return newText;
        }
    }
    return dest.subSequence(dstart, dend) +src.toString();
}