Android TextView drawable, change padding between drawable and text?

user1648374 picture user1648374 · Oct 14, 2013 · Viewed 41k times · Source

I am creating a TextView with a drawable underneath, in a GridLayout.

I want to bring the drawable to the middle of the TextView; I tried with setCompoundDrawablePadding(-75) and it only changes the position of the text.

Current code:

    TextView secondItem = new TextView(this);
    GridLayout.LayoutParams second = new GridLayout.LayoutParams(row2, col1);
    second.width = halfOfScreenWidth;
    second.height = (int) (quarterScreenWidth * 1.5);
    secondItem.setLayoutParams(second);
    secondItem.setBackgroundResource(R.color.MenuSecond);
    secondItem.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, R.drawable.ic_action_new);
    secondItem.setText("Text");
    secondItem.setCompoundDrawablePadding(-180);
    secondItem.setGravity(Gravity.CENTER);
    secondItem.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse);
    gridLayout.addView(secondItem, second);

How can I set the text and drawable to the middle of the TextView?

Answer

ssantos picture ssantos · Oct 14, 2013

You'll need to combine drawablePadding and padding to get the desired result.