Ashutosh Ashutosh - 3 years ago 171
Android Question

EditText get value to pass Textview

I am working on an activity in which I have taken edittext and textview. I have to get integer value from edittext and set it on textview.

I have to take 10-digit number(mobile no) on edittext and as soon as the edittext column gets 10-digits,its automatically sets 10-digit number on textview within the same activity.

I have implemented in this manner


  1. Getting value on edittext using

    Integer.parseInt(edittext.getText().toString());


    in a String str.

  2. Applying condition to it
    (str.length == 10)
    and getting it on textview.



As the above process is not working so try to help me friends.

I am working on this code:

public class MobileNo extends Activity {

EditText et;
TextView tv;
int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

et = (EditText) findViewById(R.id.edit);
tv = (TextView) findViewById(R.id.text1);

et.getText().toString();

et.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 textVal) {
// TODO Auto-generated method stub
Editable inputNo = textVal;
if (inputNo.length() == 10) {
tv.setText(inputNo);
}

}
});

}
}

Answer Source

I would try:

            @Override
            public void afterTextChanged(Editable textVal) {
                String inputNo = textVal.toString();
                if (inputNo.length() == 10) {
                    tv.setText(inputNo);
                }

            }

If your phone number is formatted like "(800) 549-2992" or "800-549-2992" and you only wants the numbers then you'd have to do the following:

            @Override
            public void afterTextChanged(Editable textVal) {
                // takes out all non-digits from string
                String inputNo = textVal.toString().replaceAll("[^\\d]","");
                if (inputNo.length() == 10) {
                    tv.setText(inputNo);
                }

            } 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download