Search code examples
javaandroidtextviewgettexteditview

editText.getText().toString returns empty


I'm new to Android and with the help of this site I've put together this code :

java:

public class Activity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    String input1 = ((EditText) findViewById(R.id.editText)).getText().toString().trim();
    TextView TextView = (TextView) findViewById(R.id.textView);

    if (input1.length() == 0) {
        TextView.setText("empty");
    } 
      else {
        TextView.setText(input1);
    }

}


}

xml:

 <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:hint="text" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="28dp"
    android:layout_marginStart="28dp"
    android:layout_marginTop="20dp"
    android:id="@+id/textView" />

But the textView shows empty when I type , tried a lot of things but nothing worked .

What I'm trying to do is get the value of editText and store it in a variable so I can work with it . I used a textView to see if it works but returns empty . I want the value to be updated real time , just like Android default calculator app .

EDIT

For my purpose I had to put a textWatcher.

public class Activity extends AppCompatActivity {


TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    EditText editText = (EditText) findViewById(R.id.editText);
    textView = (TextView) findViewById(R.id.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) {
            textView.setText(s);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

}

}

Solution

  • As pointed out by @Selvin, a Listener for text changes in the EditText would help.

            TextWatcher textWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if (charSequence.length() == 0) {
                        TextView.setText("empty");
                    }
                    else {
                        TextView.setText(charSequence);
                    }
                }
    
                @Override
                public void afterTextChanged(Editable editable) {
    
                }
            };
            EditText editText = (EditText) findViewById(R.id.editText);
            editText.addTextChangedListener(textWatcher);
    

    But please remove the Listener (textView.removeTextChangedListener(textWatcher);) when you're done, as there's a potential for a memory leak here.