Skip to content

Instantly share code, notes, and snippets.

@john1jan
Last active June 12, 2019 12:08
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save john1jan/a82912fb355771e565bea1720439c5dc to your computer and use it in GitHub Desktop.
RupeeTextView . Which Prefixes ₹ symbol and adds commas separated amount value
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import com.billion.grow.logger.Log;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
/**
* Created by john on 7/7/16.
*/
public class RupeeTextView extends TextView {
Context context;
public RupeeTextView(Context context) {
super(context);
this.context = context;
}
public RupeeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public RupeeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
}
@Override
public void setText(CharSequence text, BufferType type) {
String formatedString = null;
try {
// The comma in the format specifier does the trick
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
formatter.applyPattern("#,##,##,##,###");
formatedString = String.format("₹" + formatter.format(Double.parseDouble(text.toString()))); // adds rupee symbol and thousand seperater
} catch (NumberFormatException e) {
Log.d("Rupee TextView NumberFormatException");
e.printStackTrace();
}
super.setText(formatedString, type);
}
}
@saurabh-dk
Copy link

Thanks. Still works in API 27.

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