AJW
AJW

Reputation: 1649

What is most efficient way to monitor an EditText line for user input?

I have an EditText set up for user input. As soon as the user types any character I want that to trigger a floating label to rise above the EditText line. What is best method to see when user has typed in the first character on the EditText line? TextWatcher? Others?

partial Activity file:

...
private ListenerEditText cListenerEditText;

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

    cListenerEditText = (ListenerEditText) findViewById(R.id.CEditText);
    cTextInputLayout = (TextInputLayout) findViewById(R.id.ToDo_text_input_layout);

    cListenerEditText.requestFocus();
    cListenerEditText.setHint("To Do");

partial layout xml file:

...
<android.support.design.widget.TextInputLayout
    android:id="@+id/ToDo_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  >

<com.example.jdw.fourthscreen.ListenerEditText
    android:id="@+id/CEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text|textCapSentences|textNoSuggestions"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFFFFF"
    android:textColorHighlight="#30D5C8"
    android:singleLine="true"
    android:maxLength="51"
    android:imeOptions="actionNext|flagNoExtractUi"
    android:layout_marginBottom="3dp"
    android:nextFocusDown="@+id/DEditText" >

</com.example.jdw.fourthscreen.ListenerEditText>
</android.support.design.widget.TextInputLayout>

Upvotes: 1

Views: 467

Answers (2)

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

To monitor user typing event in EditText you can use TextWatcher.

You can use afterTextChanged() method to check every typed character.

Below is code for adding TextWatcher to EditText.

cListenerEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void afterTextChanged(Editable s) {
        Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString());
    }
});

Upvotes: 1

Derek Fung
Derek Fung

Reputation: 8211

To monitor and/or control text input, we usually use TextWatcher or InputFilter.

For your case, I believe TextWatcher is the right way to do.

Upvotes: 2

Related Questions