Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 427
j2emanue Avatar asked Jun 16 '14 20:06

j2emanue


People also ask

How do I add an icon to EditText?

2- First thing we need to do is to look for icons that we can use them for the android edittext. Right click on res folder → New → Vector Asset . Adjust the size from (24dp x 24dp) to (18dp x 18dp), choose the icon that you want by clicking on the android icon, click “Next” button and then click “Finish”.

How do you center text in EditText?

Simpler Solution to align text in EditText is to add android:gravity="center" in layout xml. There is no need to add Java code.


1 Answers

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);
like image 126
j2emanue Avatar answered Oct 30 '22 22:10

j2emanue