Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText - How to set default text in android edittext

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()) ;
            }
like image 277
user2870 Avatar asked Jul 30 '13 12:07

user2870


People also ask

How do I change the default text in EditText?

You can use EditText. setText(...) to set the current text of an EditText field.

Can you set text in EditText?

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.

How do you make EditText editable?

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!


1 Answers

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));
        }
    });
like image 135
Nizam Avatar answered Sep 28 '22 06:09

Nizam