j2emanue
j2emanue

Reputation: 62519

EditText setError with icon not working

Using Kitkat but also tried on jellybean and im not able to get a custom error icon to appear.
Im using these two overloaded methods for setError(...) from Android doc Im just doing very simple code:

    import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity implements OnCheckedChangeListener {

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

    setContentView(R.layout.report_issue);

    RadioGroup rg = (RadioGroup) findViewById(R.id.myradio_group);
    rg.setOnCheckedChangeListener(this);

}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

    if (checkedId == R.id.radio_two) {
        EditText et = (EditText) findViewById(R.id.et_city);
        // rb.setError("i got a single error");
        et.setError("my error",
                getResources().getDrawable(R.drawable.ic_launcher));

  //as you can see from above im setting a image for the error thru code
  et.requestFocus();
    }
}

}

heres my mylayout.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mycontainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<RadioGroup
    android:id="@+id/myradio_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="radio1" />

    <RadioButton
        android:id="@+id/radio_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="radio2" />
</RadioGroup>

<EditText
    android:id="@+id/et_city"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="enter city" />

</LinearLayout>

Here is a screenshot of what im seeing on jellybean and kitkat:

error icon not showing when custom

here is a image where its working fine when i remove the custom image icon and only call :et.setError("my error"); enter image description here

Upvotes: 5

Views: 4688

Answers (2)

vuhung3990
vuhung3990

Reputation: 6819

Android OS bug with some devices running Jelly Bean/4.2.1 - TextView.setError(CharSequence error) Missing icon

i found this and it working like charm, hope this work with you too

Upvotes: 0

j2emanue
j2emanue

Reputation: 62519

I actually got it to work. I had to first set the bounds on the drawable something like this:

Drawable d= getResources().getDrawable(R.drawable.ic_launcher);
            d.setBounds(0, 0, 
                    d.getIntrinsicWidth(), d.getIntrinsicHeight());

            et.setError("my error",d);

Upvotes: 16

Related Questions