Android Open Source - android-utils Underline Text View






From Project

Back to project page android-utils.

License

The source code is released under:

Apache License

If you think the Android project android-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.omegar.android.utils.components;
//  w  ww.j av  a 2  s  . c  o  m
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

import com.omegar.android.utils.Logger;
import com.omegar.android.utils.R;

public class UnderlineTextView extends TextView {

  private final Paint mPaint = new Paint();
  private int mUnderlineHeight = 0;
  private int mUnderlinePadding;
  private int mLineStyle;

  private static final int BASELINE = 0;
  private static final int MAX = 1;
  private static final int MIDDLE = 2;

  public UnderlineTextView(Context context) {
    this(context, null);
  }

  public UnderlineTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public UnderlineTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    init(context, attrs);
  }

  private void init(Context context, AttributeSet attrs) {
    Resources r = getResources();
    mUnderlineHeight = (int) (getTextSize() / 15);

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
        R.styleable.UnderlineTextView, 0, 0);
    mLineStyle = a
        .getInt(R.styleable.UnderlineTextView_lineStyle, BASELINE);

    int padding = (int) a.getDimension(
        R.styleable.UnderlineTextView_linePadding, 0.0f);
    mUnderlinePadding = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, padding, r.getDisplayMetrics());

    if (mUnderlineHeight <= 0) {
      mUnderlineHeight = 1;
    }
    a.recycle();
  }

  @Override
  public void setPadding(int left, int top, int right, int bottom) {
    super.setPadding(left, top, right, bottom + mUnderlineHeight);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int offset = getOffset();

    mPaint.setColor(getTextColors().getDefaultColor());
    canvas.drawRect(0, offset - mUnderlineHeight, getWidth(), offset,
        mPaint);
  }

  private int getOffset() {
    switch (mLineStyle) {
    case BASELINE:
      return getBaseline() + mUnderlinePadding;
    case MAX:
      return getHeight();
    case MIDDLE:
      return (getBaseline() + mUnderlinePadding + getHeight()) / 2;
    default:
      Logger.w("No handler for this value: " + mLineStyle);
    }

    return 0;
  }
}




Java Source Code List

com.omegar.android.utils.BaseDialog.java
com.omegar.android.utils.Logger.java
com.omegar.android.utils.PhotoHelper.java
com.omegar.android.utils.components.DividedLinearLayout.java
com.omegar.android.utils.components.NetworkImageView.java
com.omegar.android.utils.components.NonScrollableGridView.java
com.omegar.android.utils.components.NonScrollableListView.java
com.omegar.android.utils.components.ProportionalLinearLayout.java
com.omegar.android.utils.components.UnderlineTextView.java