Issue Formatting EditText for Currency - java

All- I have been struggling with this for a while and have looked at all the other questions about this sort of thing but I just can't figure it out: I have an edttext field that needs to be formatted for currency. I have tried the code in all of the other questions relating to this but no matter what I try it just doesn't work. Here is my code:
EditText text = (EditText)findViewById(R.id.edit_bill);
text.setRawInputType(Configuration.KEYBOARD_12KEY);
text.addTextChangedListener(new TextWatcher(){
EditText text = (EditText)findViewById(R.id.edit_bill);
DecimalFormat dec = new DecimalFormat("0.00");
public void afterTextChanged(Editable arg0) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
{
String userInput= ""+s.toString().replaceAll("[^\\d]", "");
if (userInput.length() > 0) {
Float in=Float.parseFloat(userInput);
float percen = in/100;
text.setText("$"+dec.format(percen));
text.setSelection(text.getText().length());
}
}
}
});
This code is inside of an onClick method. Does that make a difference (eg. the text will only be formatted when the user clicks the button)?. Thanks in advance!

I have used this method in the past for formatting money.
static public String customFormat(String pattern, double s ) {
DecimalFormat myFormatter = new DecimalFormat(pattern);
String stringFormatOutput = myFormatter.format(s);
return stringFormatOutput;
}
it can be used like this:
String strPrice = customFormat("$###,##0.00", 300.2568);
mTxt.setText(strPrice);
just change the "300.2568" to be your price. This probably could work just as well for floats instead of doubles.

Related

How to get the selected cursor position in EditText?

There is an EditText, what I want is when I select it ,I should get the cursor position. The maximum length of the edittext is 13.
Also in my edittext, the first three character's type is text and the other character's type is number. If the first three characters are selected, the input type of keyboard should show text. And if any other characters are selected ,then the input type should be number. How to do this?
I think this code can help you ❤ :
Class Code :
edittext = (EditText) findViewById(R.id.edtxt);
edittext.setInputType(InputType.TYPE_CLASS_TEXT);
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
if (edittext.getText().length() < 3 ) {
edittext.setInputType(InputType.TYPE_CLASS_TEXT);
}
else if (edittext.getText().length() == 3 ) {
edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
}}
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});}}
Xml Code :
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:maxLength="13"
android:id="#+id/edtxt"
android:ems="10"/>

Calculation with textWatcher android

In my project I have 7 EditText field.
User will only need to fill their own value in first two EditText.
I need a series calculation like this...
(after user input) subtract ed1-ed2 the result automatically show in ed3 field.
multiply ed3*2 = result show in ed4 field automatically.
multiply ed3*3= result will show in ed5 field.
multiply ed3*4=result will show in ed6 field.
Finally sum of ed4+ed5+ed6= result will show in ed7 field automatically.
So how to do it any one please help.
Thank you in Advance.
All EditText use a same TextWatcher. A simple solution like this.
EditText et1;
EditText et2;
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int num1 = Integer.valueOf(et1.getText().toString()); // Make sure you get a number
int num2 = Integer.valueOf(et2.getText().toString());
// do your calculation
}
#Override
public void afterTextChanged(Editable s) {
}
};
et1.addTextChangedListener(textWatcher);
et2.addTextChangedListener(textWatcher);

How to block user from inputing a specific character on a EditText

I need to block the user from inputing the . (period) character from the keyboard on a number EditText, but I need to be able to use it on the same EditText via the setText method.
I've tried using InputFilter but when I call setText the . character don't show. I've also tried setting the digits parameter in the xml but the setText doesn't work either.
Is there a way to do this?
Here's my InputFilter code:
public static InputFilter blockPeriod(){
return new InputFilter() {
#Override
public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
for (int j = i; j <i1 ; j++) {
char c = charSequence.charAt(j);
if (!allowed(c)){
return "";
}
}
return null;
}
private boolean allowed(char c){
return c != '.';
}
};
}
You can do it with your InputFilter, but you need to allow it for a while when you calling setText() method.
private static class AllowableInputFilter implements InputFilter {
private boolean mAllowDot;
public void setAllowDot(boolean toAllow) {
mAllowDot = toAllow;
}
....
private boolean allowed(char c){
return mAllowDot || c != '.';
}
}
public void forceText(String text) {
mInputFilter.setAllowDot(true);
mEditText.setText(text);
mInputFilter.setAllowDot(false);
}
mInputFilter = new AllowableInputFilter();
mInputFilter.setAllowDot(false);
mEditText.setFilters(new InputFilter[] {mInputFilter});
forceText("Okaaayy....");
try this out :
if (textedit.getText().toString().startsWith(".")){
//Do something about it
}
now entering . you can show a message or something else.also put this code under onTextChanged method(text watcher).
Hope it helps you out.
user digits property inside xml in EditText
android:digits="0123456789"
so now edittext only allow digit from 0 to 9

Changing textview on edittext ( when possible without a button)

I'm kinda new to java programming for android, so if i make stupid mistakes, i'm sorry.
So basically, what i wanna make is an app where if you type in the answer correctly, the next textview is gonna be displayed. And when the next textView is displayed, you're needing to give a answer to that textView, when the answer is given correctly. The textview changes again. And so on.
Does anybody have an idea how to do this?
If you don't undarstand what im saying, here is a example:
public class Game extends AppCompatActivity {
public static EditText editText_ans;
public static TextView textView_1;
String enteredText = editText.getText().toString();
If(enteredText = 3 && textView_1 = #string/1+2){
setText.textView_1(#string/3+4)
}
If(enteredText = 7 && textView_1 = #string/3+4){
setText.textView_1("100 - 23")
I'm really stuck and i hope that you guys wanna help me.
If you wanted to change the view without button you can use method addTextChangeListner() which will notify you when when the text hasbeen change for particular edittext.
edittext.addTextChangedListener(textWatcher);
private final TextWatcher textWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
textView.setVisibility(View.GONE);
} else{
textView.setText("You have entered : " + editText.getText());
}
}
};
int x=0; //to keep track of qustions
private List<String> mQuestionList=new ArrayList<>(); //array of question
private List<String> mAnswerList=new ArrayList<>(); //array of question answer
displayquestion.settext(mQuestionList.get(x);//displayquestion is textview
//nextquestion is the button when user click it will first check answer and than move to next question if answer is correct
nextquestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String answer=editText.getText().toString();
if(answer.equal(mAnswerList.get(x)){
x=x+1;
displayquestion.settext(mQuestionList.get(x); //answer is correct display next quesion
}else{
//wrong answer
}
}
});

Getting character count of EditText

I'm trying to get a character count of an EditText. I've looked into different properties of the EditText and TextView classes, but there doesn't seem to be a function that returns the amount of characters. I have tried to use the TextWatcher, but that is not ideal since sometimes I load a saved message into the EditText from the preferences, and TextWatcher does not count characters not typed right then.
Any help would be great!
Cheers!
Just grab the text in the EditText as a string and check its length:
int length = editText.getText().length();
EditText edittext;
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//This sets a textview to the current length
textview.setText(String.valueOf(s.length());
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(mTextEditorWatcher);
In Kotlin it's very easy
<EditText_name>.Text.length

Categories

Resources