0

I have created a custom textview class and I am using BackgroundColorSpan to apply color in the background. How can I add blank space before and after each line. I really appreciate any help.

final String test_str1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";


public class CustomTextView extends TextView {
    public CustomTextView(Context context) {
        super(context);
        setFont();
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFont();
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFont();
    }

    private void setFont() {
        Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/TEXT.ttf");
        setTypeface(font, Typeface.NORMAL);

        Spannable myspan = new SpannableString(getText());
        myspan.setSpan(new BackgroundColorSpan(0xFF757593), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        txtview.setText(myspan);
    }
}
naXa
  • 26,677
  • 15
  • 154
  • 213
jason
  • 3,728
  • 10
  • 43
  • 111

2 Answers2

0

One does not simply append or prepend a string with a whitespace in Java. In the first instance you should look for a library that does it for you.

I found Apache Commons Lang to be a good one for string manipulations. It has class StringUtils with the following methods:

public static String appendIfMissing(String str, CharSequence suffix, CharSequence... suffixes)
Appends the suffix to the end of the string if the string does not already end with any the suffixes.

public static String prependIfMissing(String str, CharSequence prefix, CharSequence... prefixes) Prepends the prefix to the start of the string if the string does not already start with any of the prefixes.

Both operations on String are null safe.

Linking the library to your project is easy. If you use Gradle just add this line to dependencies

dependencies {
    ...
    compile 'org.apache.commons:commons-lang3:3.4'
}
Community
  • 1
  • 1
naXa
  • 26,677
  • 15
  • 154
  • 213
0

Another option is to use JDK. String.format() can be used to left/right pad a given string.

public static String padRight(String s, int n) {
     return String.format("%1$-" + n + "s", s);
}

public static String padLeft(String s, int n) {
    return String.format("%1$" + n + "s", s);
}

public static String pad(String s, int n) {
    return padRight(padLeft(s, n), n);
}

// Usage example
String myString = getText().toString();
Spannable myspan = new SpannableString(pad(myString, 1));
myspan.setSpan(new BackgroundColorSpan(0xFF757593), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtview.setText(myspan);

References:

  1. Source of methods used in this answer;
  2. Format String Syntax | Java docs;
  3. SpannableString | Android docs;
  4. CharSequence | Android docs.
Community
  • 1
  • 1
naXa
  • 26,677
  • 15
  • 154
  • 213