How to add icon inside EditText view in Android ?

前端 未结 6 1463
渐次进展
渐次进展 2020-12-07 07:57

I want to add a \"search\" icon to appear inside an EditText in the left edge? such as search box in Facebook Android app?

6条回答
  •  醉梦人生
    2020-12-07 08:37

    You can set drawableLeft in the XML as suggested by marcos, but you might also want to set it programmatically - for example in response to an event. To do this use the method setCompoundDrawablesWithIntrincisBounds(int, int, int, int):

    EditText editText = findViewById(R.id.myEditText);
    
    // Set drawables for left, top, right, and bottom - send 0 for nothing
    editTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myDrawable, 0, 0, 0);
    

提交回复
热议问题