2

How can padding be added to the bottom of the textview programmatically? I noticed that there is some padding above the text but not below. What can be done in order to add some black padding at the bottom just like at the top? It would make the text view a lot more legible (especially for letters that extend below the baseline i.e. g and y).

enter image description here

TextView tv1 = v.findViewById(R.id.textView1);
String myString = " " + getString(R.string.magnify) + " ";
Spannable spanna = new SpannableString(myString);
spanna.setSpan(new BackgroundColorSpan(Color.BLACK), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanna.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.yellow)), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv2.setText(spanna);
wbk727
  • 8,017
  • 12
  • 61
  • 125

3 Answers3

-1

With this custom ReplacementSpan, text is drawn "vertically center"

final SpannableString spannableString = new SpannableString(myString);
stringBuilder.append(spannableString);
stringBuilder.setSpan(new TagSpan(Color.BLACK, Color.MAGENTA), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
testTextView.setText(stringBuilder, TextView.BufferType.SPANNABLE);

Class TagSpan

public class TagSpan extends ReplacementSpan {
    private RectF mRect;
    private int mBackgroundColor;
    private int mForegroundColor;

    public TagSpan(int backgroundColor, int foregroundColor) {
        this.mRect = new RectF();
        this.mBackgroundColor = backgroundColor;
        this.mForegroundColor = foregroundColor;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        // Background
        mRect.set(x, top, x + paint.measureText(text, start, end), bottom);
        paint.setColor(mBackgroundColor);
        canvas.drawRect(mRect, paint);

        // Text
        paint.setColor(mForegroundColor);
        int xPos = Math.round(x);
        int yPos = (int) (0 + ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)));
        canvas.drawText(text, start, end, xPos, yPos, paint);
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        return Math.round(paint.measureText(text, start, end));
    }

}

enter image description here

Not exactly answer to "bottom padding" request but solve part of your problem I think.

(Original post)

smora
  • 717
  • 5
  • 18
  • ok I got it, in fact you dont want to add padding to your textView, but to your span content, this is the black aera that you want to increase. – smora Aug 20 '17 at 15:08
  • Yes! That's it. – wbk727 Aug 20 '17 at 15:09
  • take a look at this https://stackoverflow.com/questions/27198155/adding-a-padding-margin-to-a-spannable – smora Aug 20 '17 at 15:09
  • or this one https://stackoverflow.com/questions/13727343/android-paddings-for-spannable – smora Aug 20 '17 at 15:17
  • It not adds bottom padding, but the text is vertically center. So I think it solve part of your problem. I try to add bottom padding, but it means modify span height and I had no success in trying to. – smora Aug 20 '17 at 15:59
-2

You can use: yourTextView.setPadding(0, 10, 0, 0); Android - How to set padding for TextView in ListView or ExpandableListView

Or increase the TextviewHeight and centre the text.

-2

Set RelativeLayout.LayoutParams for Textview

TextView tv1 = v.findViewById(R.id.textView1);
String myString = " " + getString(R.string.magnify) + " ";
Spannable spanna = new SpannableString(myString);
spanna.setSpan(new BackgroundColorSpan(Color.BLACK), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanna.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.yellow)), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv1.setText(spanna);

RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, tv1.getId());
        tv1.setLayoutParams(params);
redAllocator
  • 725
  • 6
  • 12