Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate image size when using setCompoundDrawables for EditText

When I am add icon like below:

etComment = (EditText) findViewById(R.id.et_comment);
Drawable img = getResources().getDrawable( R.drawable.warning );
etComment.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );

enter image description hereenter image description here

The icon resizes EditText. How can I calculate img size and put it into EditText without EditText resize?

Thanks!


FunkTheMonk
Use setCompounDrawables() instead of setCompoundDrawablesWithIntrinsicBounds() - you'll have to set the bounds of the drawables manually.

I don't understand how to calculate Bounds manually. I have got height and width of EditText:

etComment = (EditText) findViewById(R.id.et_comment);
Drawable img = getResources().getDrawable( R.drawable.warning );
int size = etComment.getHeight();
img.setBounds(0, 0, size, size);
etComment.setCompoundDrawables( img, null, null, null );

but I have different results in different screen sizes. How I can calculate correct size and padding of icon? Could you please help me?

like image 330
IgorOK Avatar asked Nov 20 '11 00:11

IgorOK


Video Answer


1 Answers

I think you can use different size of pics for different screens and use getMinimumWidth to set Bounds.But I did not try it before , may be it is not appropriate for .9 patch.

When you use setCompoundDrawables , you need code like :

Drawable img;
Resources res = getResources();
img = res.getDrawable(R.drawable.btn_img);
//You need to setBounds before setCompoundDrawables , or it couldn't display
img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
btn.setCompoundDrawables(img_off, null, null, null); 
like image 71
Wangchao0721 Avatar answered Sep 28 '22 01:09

Wangchao0721