Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set drawableLeft visibility programmatically

I'm adding/removing drawableLeft programmatically by:

((TextView)view).setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_icon, 0, 0, 0); 
((TextView)view).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); 

But because I'm using it in a list,

I need the option to remove the drawableLeft for the unselected rows without alignment issues.

What I want:

  AAA
* BBB
  CCC  

What I'm getting:

AAA
* BBB
CCC  

I can work around it by adding transparent icon,

but can I control this drawableLeft visibility programmatically?

like image 266
David Avatar asked Oct 16 '13 10:10

David


2 Answers

I'm not sure exactly which type of view you're applying the drawable to, but you could simply set the left padding to match the width of the drawable when it's not present.

View.setPadding(left, 0, 0, 0);
like image 192
ShibbyUK Avatar answered Oct 20 '22 13:10

ShibbyUK


if you are on API 23 or above you can use android:drawableTint="@android:color/transparent"

else you can use color filter. for example, hide the left drawable

Drawable[] drawables = tvLabel.getCompoundDrawables();
if (drawables[0] != null) { 
  drawables[0].mutate().setColorFilter(ContextCompat.getColor(getContext(),android.R.color.transparent), PorterDuff.Mode.MULTIPLY);
}
like image 23
Omar Abdan Avatar answered Oct 20 '22 14:10

Omar Abdan