CurrencyTextView in Android
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import java.text.NumberFormat;
import java.util.Locale;
public class CurrencyTextView extends TextView {
public CurrencyTextView(Context context) {
super(context);
init();
}
public CurrencyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CurrencyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
// set the text alignment to right to display currency symbol on the left
setGravity(android.view.Gravity.END);
}
public void setAmount(double amount) {
Locale locale = new Locale("en", "US"); // or any other locale you need
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
String formattedAmount = currencyFormatter.format(amount);
setText(formattedAmount);
}
}