David
David

Reputation: 37536

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?

Upvotes: 8

Views: 3595

Answers (2)

Omar Abdan
Omar Abdan

Reputation: 1975

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);
}

Upvotes: 1

ShibbyUK
ShibbyUK

Reputation: 1521

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);

Upvotes: 2

Related Questions