Wednesday 16 October 2013

Android: How to underline a string/test if underlined

/** See also {@link ViewUtils#isUnderlinedTextView(TextView)} */
    static public SpannableString underlineString(String str) {
        SpannableString spanString = new SpannableString(str);
        spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
        return spanString;
    }
    
    /** See also {@link ViewUtils#underlineString(String)} */
    static public boolean isUnderlinedTextView(TextView textView) {
        boolean isUnderlined;
        
        if( !(textView.getText() instanceof SpannedString) ) {
            throw new IllegalArgumentException(textView.getText() + " must be instalce of SpannedString: " + textView);
        }
        
        SpannedString spannedString = (SpannedString)textView.getText();
        UnderlineSpan[] anyUnderlineSpans = spannedString.getSpans(0, textView.getText().length(), UnderlineSpan.class);
        isUnderlined = anyUnderlineSpans.length>0;
        
        return isUnderlined;
    }

See also SpannedString Javadoc

No comments:

Post a Comment