title: give space between text and underline with customview android
body: <p><a href="https://i.stack.imgur.com/pU9xl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pU9xl.jpg" alt="I want an underline below OPTING"></a></p>
  
  <p>I want an underline below OPTING like below, but when i create this will the help of customview, underline appears just below opting but i want some space between text and line like image</p>
  
  <p>I had created a Custom view, in which a word will searched in a string if that is found, then that corresponding text will be underlined but the only thing i want is to give some space between underline and text,</p>
  
  <p>My class is as follows,</p>
  
  <pre><code>import android.content.Context;
  import android.content.res.TypedArray;
  import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.graphics.Rect;
  import android.support.v7.widget.AppCompatTextView;
  import android.text.Layout;
  import android.util.AttributeSet;
  import android.view.Display;
  import android.view.WindowManager;
  import android.widget.TextView;
  
  
  public class UnderLine  extends AppCompatTextView {
  
  private Rect mRect;
  private Paint mPaint;
  private int mColor;
  private float density;
  private float mStrokeWidth;
  private String stringSeach;
  
  public UnderLine(Context context) {
      this(context, null, 0);
  }
  
  public UnderLine(Context context, AttributeSet attrs) {
      this(context, attrs, 0);
  }
  
  public UnderLine(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      init(context, attrs, defStyleAttr);
  }
  
  private void init(Context context, AttributeSet attributeSet, int defStyle) {
  
      density = context.getResources().getDisplayMetrics().density;
  
      TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.UnderLine, defStyle, 0);
      mColor = typedArray.getColor(R.styleable.UnderLine_underlineColorr, 0xFFFF0000);
      stringSeach = typedArray.getString(R.styleable.UnderLine_underlineTextt);
      mStrokeWidth = typedArray.getDimension(R.styleable.UnderLine_underlineWidthh, density * 2);
      typedArray.recycle();
  
      mRect = new Rect();
      mPaint = new Paint();
      mPaint.setStyle(Paint.Style.STROKE);
      mPaint.setColor(mColor); //line mColor
      mPaint.setStrokeWidth(mStrokeWidth);
  }
  
  public int getUnderLineColor() {
      return mColor;
  }
  
  public void setUnderLineColor(int mColor) {
      this.mColor = mColor;
      invalidate();
  }
  
  public float getUnderlineWidth() {
      return mStrokeWidth;
  }
  
  public void setUnderlineWidth(float mStrokeWidth) {
      this.mStrokeWidth = mStrokeWidth;
      invalidate();
  }
  
  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      setMeasuredDimension( getMeasuredWidth(), getMeasuredHeight()+110);
  }
  
  @Override
  protected void onDraw(Canvas canvas) {
      TextView parentTextView = this;
      Rect parentTextViewRect = new Rect();
  
      String targetWord = stringSeach.toLowerCase();
      int startOffsetOfClickedText = this.getText().toString().toLowerCase().indexOf(targetWord);
      int endOffsetOfClickedText = startOffsetOfClickedText + targetWord.length();
  
      // Initialize values for the computing of clickedText position
      Layout textViewLayout = parentTextView.getLayout();
  
      double startXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal((int)startOffsetOfClickedText);
      double endXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal((int)endOffsetOfClickedText);
  
      // Get the rectangle of the clicked text
      int currentLineStartOffset = textViewLayout.getLineForOffset((int)startOffsetOfClickedText);
      int currentLineEndOffset = textViewLayout.getLineForOffset((int)endOffsetOfClickedText);
      boolean keywordIsInMultiLine = currentLineStartOffset != currentLineEndOffset;
      textViewLayout.getLineBounds(currentLineStartOffset, parentTextViewRect);
  
      // Update the rectangle position to his real position on screen
      int[] parentTextViewLocation = {0,0};
      parentTextView.getLocationOnScreen(parentTextViewLocation);
  
      double parentTextViewTopAndBottomOffset = (
              //parentTextViewLocation[1] -
              parentTextView.getScrollY() +
                      parentTextView.getCompoundPaddingTop()
      );
  
      parentTextViewRect.top += parentTextViewTopAndBottomOffset;
      parentTextViewRect.bottom += parentTextViewTopAndBottomOffset;
  
      // In the case of multi line text, we have to choose what rectangle take
      if (keywordIsInMultiLine){
  
          WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
          Display display = wm.getDefaultDisplay();
  
          int screenHeight = display.getHeight();
          int dyTop = parentTextViewRect.top;
          int dyBottom = screenHeight - parentTextViewRect.bottom;
          boolean onTop = dyTop &gt; dyBottom;
  
          if (onTop){
              endXCoordinatesOfClickedText = textViewLayout.getLineRight(currentLineStartOffset);
          }
          else{
              parentTextViewRect = new Rect();
              textViewLayout.getLineBounds(currentLineEndOffset, parentTextViewRect);
              parentTextViewRect.top += parentTextViewTopAndBottomOffset;
              parentTextViewRect.bottom += parentTextViewTopAndBottomOffset;
              startXCoordinatesOfClickedText = textViewLayout.getLineLeft(currentLineEndOffset);
          }
  
      }
  
      parentTextViewRect.left += (
              parentTextViewLocation[0] +
                      startXCoordinatesOfClickedText +
                      parentTextView.getCompoundPaddingLeft() -
                      parentTextView.getScrollX()
      );
      parentTextViewRect.right = (int) (
              parentTextViewRect.left +
                      endXCoordinatesOfClickedText -
                      startXCoordinatesOfClickedText
      );
  
  
     canvas.drawLine(parentTextViewRect.left,parentTextViewRect.bottom+mStrokeWidth, parentTextViewRect.right,
              parentTextViewRect.bottom+mStrokeWidth, mPaint);
      super.onDraw(canvas);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>attrs.xml is as follows,</p>
  
  <pre><code>  &lt;declare-styleable name="UnderLine" &gt;
      &lt;attr name="underlineWidthh" format="dimension" /&gt;
      &lt;attr name="underlineColorr" format="color" /&gt;
      &lt;attr name="underlineTextt" format="string" /&gt;
  &lt;/declare-styleable&gt;
  </code></pre>
  
  <p>Sample layout as follows,</p>
  
  <pre><code>    &lt;UnderLine
      style="@style/textView"
      android:gravity="top|center"
      app:underlineColorr="@color/signup_bottom_darkWhite"
      app:underlineWidthh="2dp"
      app:underlineTextt="OPTING"
      android:text="ON FREE PARKING + DISCOUNTED RATES \n BY OPTING IN"/&gt;
  </code></pre>
  
  <p>I am a beginner with customview, and i had created this with the help of some answers on stackoverflow. Please don't suggest any other way to do this.</p>
  
  <p>Anyhelp will be greatly appreciated.</p>