I am working on an android application which people will enter their bills. I have an EditText which people enter the amount.
What I want to do is that for example If a person enters "2" it should be converted automatically to "0.02". Then if he/she wants to enter 22$ he should push 2, 2, 0 and 0 buttons. It will be like that "22.00". How can I manage to do that using EditText. Can you give me some ideas guys?
Here is the related code pieces :
<EditText
android:id="@+id/amount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:inputType="number"
android:hint="@string/enter_amount"
android:singleLine="true" />
EditText payment = (EditText) findViewById(R.id.amount);
if (payment.getText().toString().length() > 0){
total_amount =Integer.parseInt(payment.getText().toString()) ;
}
You can use EditText. setText(...) to set the current text of an EditText field.
In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file. Following is the example to set the text of TextView control while declaring it in XML Layout file.
We can set editable property of EditText in XML layout but not programatically, but there is no setEditable() method! If EditText is not Enabled [ by setEnabled(false) ] it still Editable!
EditText payment = (EditText) findViewById(R.id.amount);
payment.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) {
total_amount =Integer.parseInt(s) ;
payment.setText(""+(total_amount/100));
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With