5

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

j2emanue
  • 60,549
  • 65
  • 286
  • 456

2 Answers2

16

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);
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Thank you! This did it. I am surprised I have to set the intrinsic width and height even though the preferred size is also set within the image-xml. One would think it is not required, but it seems it is. Doing what you suggested solved my issue (I suppose it was using the viewport width and height instead of the intrinsic ones). – Igor Mar 14 '16 at 01:40
  • 1
    For people with api > 21, use ContextCompat.getDrawable(context, resId); for compatibility issues. – Hibbem Aug 16 '16 at 13:55
0

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

Community
  • 1
  • 1
vuhung3990
  • 6,353
  • 1
  • 45
  • 44