Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class HorizontalBarViewGroup extends ViewGroup {
float mValue = 0f;
int mFillWidth = 0;
int mEmptyWidth = 0;
int mTextWidth = 0;
String mText = "";
int mEmptyColor;
int mFillColor;
int mEmptyTextColor;
int mFillTextColor;
int mTextSize = 15;
int mTextPadding = mTextSize;
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
View mFillChild;
View mEmptyChild;
TextView mTextChild;
public HorizontalBarViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public HorizontalBarViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public HorizontalBarViewGroup(Context context) {
super(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public HorizontalBarViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.HorizontalBarViewGroup,
0, 0
);
String fontPath = null;
try {
mEmptyColor = a.getColor(R.styleable.HorizontalBarViewGroup_vg_emptyColor, Color.GRAY);
mFillColor = a.getColor(R.styleable.HorizontalBarViewGroup_vg_fillColor, Color.BLUE);
mEmptyTextColor = a.getColor(R.styleable.HorizontalBarViewGroup_vg_emptyTextColor, Color.BLACK);
mFillTextColor = a.getColor(R.styleable.HorizontalBarViewGroup_vg_fillTextColor, Color.WHITE);
mTextSize = a.getDimensionPixelSize(R.styleable.HorizontalBarViewGroup_vg_textSize, mTextSize);
mText = a.getString(R.styleable.HorizontalBarViewGroup_vg_text);
mTextPadding = a.getDimensionPixelSize(R.styleable.HorizontalBarViewGroup_vg_textPadding, mTextPadding);
mValue = a.getFloat(R.styleable.HorizontalBarViewGroup_vg_fillPercentage, mValue);
fontPath = a.getString(R.styleable.HorizontalBarViewGroup_vg_fontPath);
if (mText == null) {
mText = "";
}
p.setTextSize(mTextSize);
mTextWidth = (int) p.measureText(mText);
} finally {
// release the TypedArray so that it can be reused.
a.recycle();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int totalWidth = MeasureSpec.getSize(widthMeasureSpec);
int totalHeight = heightMeasureSpec;
if (mFillChild == null) {
mFillChild = getChildAt(0);
mFillChild.setBackgroundColor(mFillColor);
}
if (mEmptyChild == null) {
mEmptyChild = getChildAt(1);
mEmptyChild.setBackgroundColor(mEmptyColor);
}
if (mTextChild == null) {
mTextChild = (TextView) getChildAt(2);
}
//This is the bar fill
mFillWidth = (int) (totalWidth * (mValue / 100f));
if (mFillChild != null) {
mFillChild.measure(
MeasureSpec.makeMeasureSpec(mFillWidth, MeasureSpec.EXACTLY),
totalHeight);
}
mEmptyWidth = totalWidth - mFillWidth;
if (mEmptyChild != null) {
mEmptyChild.measure(
MeasureSpec.makeMeasureSpec(mEmptyWidth, MeasureSpec.EXACTLY),
totalHeight);
}
if (mTextChild != null) {
mTextChild.setText(mText);
mTextChild.measure(widthMeasureSpec,
heightMeasureSpec);
}
setMeasuredDimension(totalWidth, totalHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int prevChildRight = 0;
int prevChildBottom = 0;
mFillChild.layout(prevChildRight, prevChildBottom, prevChildRight + mFillChild.getMeasuredWidth(), prevChildBottom + mFillChild.getMeasuredHeight());
prevChildRight += mFillChild.getMeasuredWidth();
mEmptyChild.layout(prevChildRight, prevChildBottom, prevChildRight + mEmptyChild.getMeasuredWidth(), prevChildBottom + mEmptyChild.getMeasuredHeight());
int textTop = getMeasuredHeight() / 2 - mTextChild.getMeasuredHeight() / 2;
int textWidth = mTextChild.getMeasuredWidth();
if (mFillWidth > (textWidth + mTextPadding * 2)) {
mTextChild.setTextColor(mFillTextColor);
int left = mFillWidth - (textWidth + mTextPadding);
mTextChild.layout(left,
textTop,
left + mTextChild.getMeasuredWidth(),
textTop + mTextChild.getMeasuredHeight());
} else {
mTextChild.setTextColor(mEmptyTextColor);
int left = mFillWidth + mTextPadding;
mTextChild.layout(left,
textTop,
left + mTextChild.getMeasuredWidth(),
textTop + mTextChild.getMeasuredHeight());
}
}
public void setValue(float value, String strFormat) {
mValue = value;
if (mValue > 100) {
mValue = 0;
}
mText = String.format(strFormat, value);
mTextWidth = (int) p.measureText(mText);
requestLayout();
}
public float getPercentage() {
return mValue;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HorizontalBarViewGroup">
<attr name="vg_emptyColor" format="color"/>
<attr name="vg_fillColor" format="color"/>
<attr name="vg_emptyTextColor" format="color"/>
<attr name="vg_fillTextColor" format="color"/>
<attr name="vg_textSize" format="dimension"/>
<attr name="vg_text" format="string"/>
<attr name="vg_textPadding" format="dimension"/>
<attr name="vg_fillPercentage" format="float"/>
<attr name="vg_fontPath" format="string"/>
</declare-styleable>
</resources>
<com.example.alimuzaffar.perfmatters.customviews.HorizontalBarViewGroup
android:id="@+id/custom"
android:background="#80FF0000"
android:layout_width="wrap_content"
app:vg_text="0.00 Filled"
app:vg_fillPercentage="0"
android:layout_height="35dp">
<com.example.alimuzaffar.perfmatters.customviews.MV
android:background="#00FF00"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.example.alimuzaffar.perfmatters.customviews.MV
android:background="#0080FF"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.example.alimuzaffar.perfmatters.customviews.MTV
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.example.alimuzaffar.perfmatters.customviews.HorizontalBarViewGroup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment