title: centering an image in a compoundTextView, moves the text down
body: <p>I have an image with size 6x10 dp</p>
  
  <p>I want to add it dynamically to a textView.</p>
  
  <p>I have 3 text view and according to some state I add it as a compoundDrawable.</p>
  
  <pre><code>TextViewCompat.setCompoundDrawablesRelative(targetTextView, null, null, drawable, null);
  </code></pre>
  
  <p>I want the image to be almost centered vertically to the text view + 1 sp length lower than the center. So in each text view I added</p>
  
  <pre><code>      android:gravity="center_vertical"
  </code></pre>
  
  <p>so:</p>
  
  <p>
  </p>
  
  <p>I have tried to </p>
  
  <p>1) create a canvas with height 8 x 10 and center it to the text view and then the drawers bound to (0,1sp, 10, 8-1sp)</p>
  
  <p>but it seems the text view moved downwards as well relative to its position before adding the compund imageView.</p>
  
  <p><a href="https://i.stack.imgur.com/uxw7Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uxw7Y.png" alt="enter image description here"></a></p>
  
  <p>2) create a canvas with heightof the textview and center it to the text view and then the drawers bound to (0,(textView height - 6) /2, 10, ...)</p>
  
  <p><a href="https://i.stack.imgur.com/bSeG0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bSeG0.png" alt="enter image description here"></a></p>
  
  <p>but then waiting for the observerTree make the code combersome.</p>
  
  <p>Any other way to do this?</p>
  
  <p>here is my code for the (1) option:</p>
  
  <pre><code>  private void tintAndAddPadding(
  
  ...
  
  
        @DrawableRes int drawableId, @ColorRes int colorId) {
      Drawable drawable =
          Preconditions.checkNotNull(AppCompatResources.getDrawable(getContext(), drawableId));
      Drawable drawableWithTint = DrawableCompat.wrap(drawable);
      DrawableCompat.setTint(
          drawableWithTint.mutate(), getContext().getResources().getColor(colorId));
      // Bitmap dimensions sets the canvas dimensions
      Bitmap bitmap =
          Bitmap.createBitmap(
              getContext()
                  .getResources()
                  .getDimensionPixelSize(R.dimen.account_menu_collapse_expand_width),
              getContext()
                  .getResources()
                  .getDimensionPixelSize(R.dimen.account_menu_collapse_expand_canvas_height),
              Bitmap.Config.ARGB_8888);
  
      Canvas canvas = new Canvas(bitmap);
  
      // Drawable bounds should be all 4 coordinates on the canvas to draw on.
      // This implies the drawable width and height.
      drawableWithTint.setBounds(
          0,
          getContext()
              .getResources()
              .getDimensionPixelSize(R.dimen.account_menu_collapse_expand_top_padding)
          , canvas.getWidth(),
          canvas.getHeight() -
              getContext()
                  .getResources()
                  .getDimensionPixelSize(R.dimen.account_menu_collapse_expand_top_padding));
      drawableWithTint.draw(canvas);
  
     TextViewCompat.setCompoundDrawablesRelative(targetTextView, null, null, drawableWithTint, null);
      // Padding between text and drawable
      targetTextView.setCompoundDrawablePadding(
          getContext()
              .getResources()
              .getDimensionPixelSize(R.dimen.account_menu_collapse_expand_left_padding))
    }
  </code></pre>
  
  <ul>
  <li>I know i can use relativeLayout, but my team encourages me to use compoundText nevertheless.</li>
  </ul>