Exorcist
Exorcist

Reputation: 252

Setting text dynamically of EditText

I am unable to figure out how to set text dynamically of an EditText. A fellow user at stackoverflow suggested me to use onTextChanged() but I don't how that either.So I read on it for an hour and when tried to implement it,Errors Errors Errors! So,I am asking this question again.

I have two EditTexts et1 and et2. Both are of type NumberDecimal.et2 is unfocusable and unclickable.The user can only input value in et1.What I want is that as soon as user enters a number in et1,that number should pe displayed in et2.So,if the user has to enter 10,he will first enter 1 and that 1 should also be displayed in et2.Now to complete the number,he will enter 0 and then the number 10 should be displayed in et2.So,et2 should be updated character by character depending upon the entry made in et1.

This is how I read from et1 :

et1=(EditText) findViewById(R.id.et1);
et2=(EditText) findViewById(R.id.et2);
String read=et1.getText().toString();
if(read.length()!=0)
{
float a=Float.parseFloat(read);
}

Now if I use setText() on et2,that wont be dynamic and furthermore I will have to make use of an button to show the text in et2 and it will show whole String which was entered in et1 until the button was clicked.

Using Listener on et1 is an good idea but I don't know its implementation with onTextChanged since I am a newbie.

Please help.

Thanking you in anticipation.

Upvotes: 0

Views: 648

Answers (2)

Pavandroid
Pavandroid

Reputation: 1586

Use the below code

final TextView et2 = new TextView(this);
        TextView et1 = new TextView(this){
            @Override
            protected void onTextChanged(CharSequence text, int start,
                    int before, int after) {
                // TODO Auto-generated method stub
                super.onTextChanged(text, start, before, after);
                et2.setText(this.getText());
            }
        };

now add these et1,et2 to any layout.

Upvotes: 1

John
John

Reputation: 3797

I had to do the same thing you are trying to do, I used a KeyListener. Just update the EditText everytime a key is pressed.

Check it out:

http://developer.android.com/reference/android/text/method/KeyListener.html

Upvotes: 0

Related Questions