Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Custom TextView to show Currency

I need to set a mask for a textview to convert its text to a specific style (Currency, my own though Like this: Rls ###,###,###,### ) but I need to be able to get it's raw text later on. What shall I do?

======================================================

say I will pass this string to the TextView: 2120000

It should be seen by user like : Rls 2,120,000

but the .getText method should return: 2120000

======================================================

the formatting code would be like this:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("Rls ###,###,###,###", symbols);
String prezzo = decimalFormat.format(Integer.parseInt(textTemp));

any help would be appreciated

like image 664
Muhammad Naderi Avatar asked Jan 24 '26 00:01

Muhammad Naderi


1 Answers

Correct way would be this: To create a custom TextView as a Java Class

public class RialTextView extends TextView {

    String rawText;

    public RialTextView(Context context) {
        super(context);

    }

    public RialTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RialTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        rawText = text.toString();
        String prezzo = text.toString();
        try {

            DecimalFormatSymbols symbols = new DecimalFormatSymbols();
            symbols.setDecimalSeparator(',');
            DecimalFormat decimalFormat = new DecimalFormat("###,###,###,###", symbols);
            prezzo = decimalFormat.format(Integer.parseInt(text.toString()));
        }catch (Exception e){}

        super.setText(prezzo, type);
    }

    @Override
    public CharSequence getText() {

        return rawText;
    }
}

and in the XML instead of <TextView tag using something like this:

<com...RialTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price"
        android:id="@+id/tv_itemgriditems_Price"
        android:layout_below="@+id/tv_itemgriditems_Remain"
        android:layout_centerHorizontal="true"
        android:textSize="15sp"
        />
like image 82
Muhammad Naderi Avatar answered Jan 26 '26 17:01

Muhammad Naderi