I am new to Java /android studio, need help with this-
I have a TextinputEditText field- accountEt (one can input number to this field)
I need to calculate and display- accountEt * ((100-3) / 100);
[Basically, if 100 entered it shows 97, if 10 entered it shows 9.7]
How to do this in android studio? > show the result (9.7) in a TextView properly.
Like-
Textfield accountEt- [10] | (edit text) Result-
[9.7] |(text view)
Any help is useful.
If you use a button to calculate the result. In your button onclick listener just save the value of text value to string and calculate the resut using the value and store it another variable. And finally set the result to your edittext
int a=Integer.parseInt(edittextname.gettext().tostring());
float result=a * ((100-3) / 100);
accountet.settext(Float.toString(result));
Get edit Text value in String.
calculate the result and store in the String Data Type.
Show on String value on the Text View.
String calculate=accountEt.getText.ToString();
String newCalcualatedValue=calculate.toInt* ((100-3) / 100);
textView.setText(newCalcualatedValue);
If you want to update the text automatically on text input, do this:
accountEt.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
String text = editText.getText().toString();
int num;
if (text.isEmpty())
num = 0;
else
num = Integer.parseInt(editText.getText().toString());
double res = num * 0.97;
result.setText(Double.toString(res));
}
});
Related
What I have:
I have an EditText which accepts decimal input and I setted the default value in the XML to 0.00 (if there is no input yet)
If the user press 1, then the value change to 1. If he needs to enter decimal value then he must press the dot . on the keyboard.
What I want:
I want to auto format the EditText in a decimal format 0.00, so if a user press 1 if becomes 0.01 instead of 1 (Like in the PAYPAL App). If he entered 100, then the value in the EditText must be formatted as 1.00.
I know this can be done by using TextWatcher but I don't know how to achieve this.
End Result Comparison:
WHAT I HAVE
1 = 1
11 = 11
222 = 222
WHAT I NEED
1 = 0.01
11 = 0.11
222 = 2.22
UPDATE:
My actual TextWatcher looks like below just to set the default value to 0.00 if the user deletes all inputs (to prevent error on calculated functions).
private class EditTextListener implements 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) {
}
#Override
public void afterTextChanged(Editable s) {
if (editText.getText().toString().equals("") || editText.length() < 1){
editText.setText("0.00");
editText.post(() -> editText.setSelection(editText.getText().length()));
} else {
BigDecimal amount = new BigDecimal(editText.getText().toString());
}
}
}
as you described you need to divide it by 100 with decimal points:
#Override
public void afterTextChanged(Editable s) {
if (editText.getText().toString().equals("") || editText.length() < 1) {
editText.setText("0.00");
editText.post(() -> editText.setSelection(editText.getText().length()));
} else {
double amount = Double.parseDouble(editText.getText().toString());
editText.setText(String.format("%.2f", (amount / 100.0));
}
}
Try use this code
edtQtdVolume.addTextChangedListener(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) {
edtQtdVolume.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[R$,.]", "");
double parsed = Double.parseDouble(cleanString);
Locale locale = new Locale("en", "US");
NumberFormat nf = NumberFormat.getNumberInstance(locale);
DecimalFormat formatter = (DecimalFormat) nf;
formatter.applyPattern("#0.00");
String formatted = formatter.format((parsed/100));
edtQtdVolume.setText(formatted);
edtQtdVolume.setSelection(formatted.length());
edtQtdVolume.addTextChangedListener(this);
lista.get(position).setQtdVolumeInformadoColeta(Double.valueOf(formatted));
}
#Override
public void afterTextChanged(Editable s) {
}
});
I'm new to android development and was trying to make a betting game.
In my app there's a field where user is required to enter his bet, now the bet should satisfy the following conditions:
-Minimum value of bet is 5
-Bet should be less than player's current point.
Now don't want the game(app) to proceed further untill the player enters a Valid bet.
How can I achiecve this? I'm struck on this problem since past 2 days.
it is not about simple if statements ?
if(editTextbetamount>5 && editTextbetamount<usersCurrentPoint){
}else{
//toasting something for example
}
Add a textChange listenerto your editText
editTextView.addTextChangeListener(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) {
// Do your logic here
if(Integer.parseInt(s.toString()) >= 5 && playersCurrentPoint >= Integer.parseInt(s.toString){
// proceed
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
I am using an EditText to allow a user to input a value that is stored in a Double.
However, by default, Doubles look like "0.0" and it's a little annoying for a user to backspace over the extra decimal if it's not used. Is there a way to force-display whole numbers to look like "0" and only show the decimal if the user actually decides to use it?
Current code:
myEditText = (EditText) view.findViewById(R.id.my_edittext);
myEditText.setText(myVariable + "");
myEditText.addTextChangedListener(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) {
}
#Override
public void afterTextChanged(Editable s) {
String temp = s.toString();
if (s.length() > 0){
if (OtherMethods.isDouble(temp)) {
myVariable = Double.parseDouble(temp);
}
else {
myVariable = 0.0;
}
}
else {
myVariable = 0.0;
}
}
});
The XML:
<EditText
android:id="#+id/my_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:hint="Input Value"
android:imeOptions="actionDone"/>
To achieve this you can use NumberFormat
EditText yourEditText = (EditText) findViewById(R.id.editTextID);
//dummy data, will have user's value
double aDouble = 4.0;
//formats to show decimal
NumberFormat formatter = new DecimalFormat("#0");
//this will show "4"
yourEditText.setText(formatter.format(aDouble));
Make sure to validate the user's input. Also, this will only modify what is displayed and not the value itself.
Parsing Double to String, then String to Int:
String stringparsed = YourDouble + "";
int intparsed = Integer.parseInt(stringparsed);
Using substring to cut the string from a startIndex to finalIndex:
String stringparsed = YourDouble + "";
String final = stringparsed.substring(0,1); //for example, if the double was 0.0, the result is 0
I have a field filled with an EditText control, it figures: 475759403048575663648495004945757590.
What can I use to choose the first 10 numbers from this EditText:
and then, insert the numbers 4757594030, in the TextView?
You can substring the string to 10 characters.
String s = somestring.substring(0,10);
So this will return the first 10 characters, and now you can put the value S in your control!
You can put a textwatcher to the edittext, then count as the edit text is being edited, when you get to the number of characters (10 in this case), you can then display those characters.
Example
TextView textView = ....
edittext.addTextChangedListener(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) { }
#Override
public void afterTextChanged(Editable textVal) {
String inputText = textVal.toString();
if (inputText.length() == 10) {
textView.setText(inputText);
}
}
});
All-
I have a TextWatcher that formats an EditText to currency format:
private String current = "";
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)){
editText$.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,.]", "");
double parsed = Double.parseDouble(cleanString);
String formated = NumberFormat.getCurrencyInstance().format((parsed/100));
current = formated;
editText$.setText(formated);
editText$.setSelection(formated.length());
editText$.addTextChangedListener(this);
}
}
This works great, the problem is that my EditText only needs whole numbers so I do not the user to be able to enter cents. So instead of 0.01 than 0.12 than 1.23 than 12.34, I want 1 than 12 than 123 than 1,234. How can I get rid of the decimal point but keep the commas? Thank you.
If you don't mind removing the period and trailing zeroes, you could do this:
mEditText.addTextChangedListener(new TextWatcher() {
private String current = "";
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(current)) {
annualIncomeEntry.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,]", "");
if (cleanString.length() > 0) {
double parsed = Double.parseDouble(cleanString);
NumberFormat formatter = NumberFormat.getCurrencyInstance();
formatter.setMaximumFractionDigits(0);
current = formatter.format(parsed);
} else {
current = cleanString;
}
annualIncomeEntry.setText(current);
annualIncomeEntry.setSelection(current.length());
annualIncomeEntry.addTextChangedListener(this);
}
}
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
This will set the number formatter's maximum fraction digits to zero, removing all trailing zeroes and the period. I also removed the division by 100 so that all entered numbers are integers.
Also make sure that your EditText's inputType is "number" or this will crash if the user tries to enter a non-numeric character.
Hexar's answer was useful but it lacked error detection when the user deleted all the numbers or moved the cursor. I built on to his answer and an answer here to form a complete solution. It may not be best practice due to setting the EditText in the onTextChanged() method but it works.
/* don't allow user to move cursor while entering price */
mEditText.setMovementMethod(null);
mEditText.addTextChangedListener(new TextWatcher() {
private String current = "";
NumberFormat formatter = NumberFormat.getCurrencyInstance();
private double parsed;
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(current)) {
/* remove listener to prevent stack overflow from infinite loop */
mEditText.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,]", "");
try {
parsed = Double.parseDouble(cleanString);
}
catch(java.lang.NumberFormatException e) {
parsed = 0;
}
formatter.setMaximumFractionDigits(0);
String formatted = formatter.format(parsed);
current = formatted;
mEditText.setText(formatted);
/* add listener back */
mEditText.addTextChangedListener(this);
/* print a toast when limit is reached... see xml below.
* this is for 6 chars */
if (start == 7) {
Toast toast = Toast.makeText(getApplicationContext(),
"Maximum Limit Reached", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
}
A quick way to ensure the user doesn't enter invalid information is to edit the xml. For my program, a limit of 6 number characters was set.
<!-- it says maxLength 8 but it's really 6 digits and a '$' and a ',' -->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number|textVisiblePassword"
android:maxLength="8"
android:digits="0123456789"
android:id="#+id/mEditText"
android:hint="Price"/>
Why don't you format the amount using currencyFormat and then take out the .00 from the String.
private static final ThreadLocal<NumberFormat> currencyFormat = new ThreadLocal<NumberFormat>() {
#Override
protected NumberFormat initialValue() {
return NumberFormat.getCurrencyInstance();
}
};
currencyFormat.get().format( < your_amount_here > )
etProductCost.addTextChangedListener(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) {
if (s.toString().length() == 1){
//first number inserted.
if (s.toString().equals(getString(R.string.currency_symbol))){
//if it is a currecy symbol
etProductCost.setText("");
}else {
etProductCost.setText(getString(R.string.currency_symbol) + s.toString());
etProductCost.setSelection(s.toString().length());
}
return;
}
//set cursor position to last in edittext
etProductCost.setSelection(s.toString().length());
}
#Override
public void afterTextChanged(Editable s) {}
});