I would like to build a custom textview that can show money like this:
In the end I want any user to be able to call it like this:
<com.util.MyCustomViews.MoneyTextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="24.92"
android:textColor="#727272"
android:textSize="18dp" />
Two problems I have is that if I use html to achieve tis its not quite the exact height UX team wants the cents raised by. They only want the cents raised half way as you can see in the photo. so calling <p>This text contains <sup>superscript</sup>45</p>
raises it too high. i need to to put able to raise it myself. Im not sure how to begin but im assuming I'll over ride setText in a custom textView like this:
class MoneyTextView extends AppCompatTextView {
public MoneyTextView(Context context) {
super(context);
}
public MoneyTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MoneyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setText(CharSequence text, BufferType type) {
Spannable s = getSomeCustomSpannableSuperScriptString(getContext(), text);
super.setText(s, BufferType.SPANNABLE);
}
}
UPDATE: I Tried the following code but it did not work:
public class MoneyTextView extends AppCompatTextView {
static final String SEPERATOR = ".";
static final double HEIGHT_IN_EM = 0.5;
static final int MOGO_BASELINE_SHIFT = 10;
public MoneyTextView(Context context) {
super(context);
}
public MoneyTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MoneyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(Html.fromHtml("<b>$27</b><sup style=\"vertical-align:top,line-height:"+HEIGHT_IN_EM+"em\">.45</sup>"), BufferType.SPANNABLE);
}
and used it with this constant: static final double HEIGHT_IN_EM = 0.5;
but this is the output i get no matter the height i choose, on a nexus api 19 emulator:
it wont push down the ".45".
the layout file itself if necessary looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplicationtest.MainActivity">
<com.example.myapplicationtest.MoneyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:text="666.33"
android:textSize="22dp"/>
</LinearLayout>