Last active
April 6, 2016 09:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of usage :
XML :
Java :