WonderCsabo
WonderCsabo

Reputation: 12207

EditText error not cleared

Let's take the following reduced test case:

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@android:id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me!"
        android:id="@android:id/button1"/>

</LinearLayout>

MainActivity.xml:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView text1 = (TextView) findViewById(R.id.text1);
        final TextView text2 = (TextView) findViewById(R.id.text2);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    text1.setError("error");
                    text2.setError("error");
                }
            });
        }
    }

I have the following problem: the the documentation of TextView.setErrorText() says the error message and icon is cleared when any key events cause changes to the TextView. But in this example, only the second widget is cleared, the first is not when changed.

Upvotes: 0

Views: 863

Answers (1)

Drew
Drew

Reputation: 3334

The documentation is very clear on it:

public void setError (CharSequence error)

"Sets the right-hand compound drawable of the TextView to the "error" icon and sets an error message that will be displayed in a popup when the TextView has focus. "

One of your EditText receives the focus when your app is executed. The other is unfocused, therefore, the error text is not shown. That's the answer to your question.

And by the way, it is really not a good practice to mix namespaces: android.R.* (@android:) and R. (@id/*). This could result in hardly trackable bugs in not-so-distant future of your app.

Upvotes: 2

Related Questions