Skip to content

Instantly share code, notes, and snippets.

@MensObscura
Last active April 6, 2016 09:09
Show Gist options
  • Save MensObscura/3d4065a31063d2ee61b2994565104a85 to your computer and use it in GitHub Desktop.
Save MensObscura/3d4065a31063d2ee61b2994565104a85 to your computer and use it in GitHub Desktop.
to diplay some price with " ppD°° " 'pp' are the price in integer and 'D' the type of money like "€", and '°°' the decimals
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.text.DecimalFormat;
import butterknife.Bind;
import butterknife.ButterKnife;
public class ExponentView extends RelativeLayout {
private static final String TAG = ExponentView.class.getSimpleName();
private static final String decimalPattern = "###0.00";
private static final String regularExpression = ",";
@Bind(R.id.tv_base)
protected TextView mLabel;
@Bind(R.id.tv_exponent)
protected TextView mExponent;
private int mTextSize;
private ColorStateList mTextColor;
public ExponentView(Context context) {
super(context);
}
public ExponentView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public ExponentView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr);
}
private void init(AttributeSet attrs) {
init(attrs, 0);
}
private void init(AttributeSet attrs, int defStyleAttr) {
final View rootView = inflate(getContext(), R.layout.custom_exponent_layout, this);
ButterKnife.bind(rootView);
mTextSize = -1;
mTextColor = null;
final TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ExponentView, defStyleAttr, 0);
try {
mTextSize = typedArray.getDimensionPixelSize(R.styleable.ExponentView_textSize, mTextSize);
mTextColor = typedArray.getColorStateList(R.styleable.ExponentView_textColor);
} finally {
typedArray.recycle();
}
if (mTextSize > 0) {
float labelSize = (mTextSize / Resources.getSystem().getDisplayMetrics().density);
float exponentSize = (float) ((mTextSize * 0.75) / Resources.getSystem().getDisplayMetrics().density);
mLabel.setTextSize(labelSize);
mExponent.setTextSize(exponentSize);
}
if (mTextColor != null) {
mLabel.setTextColor(mTextColor);
mExponent.setTextColor(mTextColor);
}
}
public void setText(final double number, final String exponentPrefix) {
final String stringNumber = String.valueOf(new DecimalFormat(decimalPattern).format(number));
final String[] splittedNumber = stringNumber.split(regularExpression);
mLabel.setText(splittedNumber[0]);
if (splittedNumber.length > 1) {
mExponent.setText(String.format("%s%s", exponentPrefix, splittedNumber[1]));
}
}
public void setLabelText(final String text) {
mLabel.setText(text);
}
public void setExponentText(final String text) {
mExponent.setText(text);
}
}
@MensObscura
Copy link
Author

Do not forget to add :

<resources>

    <declare-styleable name="ExponentView">
        <attr name="textSize" format="dimension" />
        <attr name="textColor" format="color" />
    </declare-styleable>


</resources>

in attrs.xml

@MensObscura
Copy link
Author

Example of usage :

XML :

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

     <com.your.package.ExponentView
                android:id="@+id/tv_product_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:textSize="16sp"
                app:textColor="@color/color_pink" />

</LinearLayout>

Java :

mExponentViewProductPrice.setText(10.50, "€"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment