Android TextView drawable, change padding between drawable and text?

AndroidTextviewDrawablePadding

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

In XML:

android:drawablePadding = paddingValue

or

Programmatically:

TextView txt;

txt.setCompoundDrawablePadding(paddingValue)

android:drawablePadding is the easiest way to give padding to the drawable icon but you can not give specific one side padding like paddingRight or paddingLeft of the drawable icon. This gives padding to either side of the drawable.

Solution 3 - Android

  <Button
            android:id="@+id/otherapps"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/btn_background"
            android:text="@string/other_apps"
            android:layout_weight="1"
            android:fontFamily="cursive"
            android:layout_marginBottom="10dp"
            android:drawableLeft="@drawable/ic_more"
            android:paddingLeft="8dp" //for drawable padding to the left
            android:textColor="@android:color/holo_red_light" />`enter code here`

Solution 4 - Android

You can use layer-list.

Before:

enter image description here

Create xml for shape - shape_rectangle.xml :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="5dp">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@color/my_color" />
        <size
            android:width="5dp"
            android:height="5dp" />
    </shape>
</item>
</layer-list>

After:

enter image description here

In xml, there is something like android:right. You can add also top,left,bottom. This way you can give for example: padding left and right to drawable without resizing total height of textView.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1648374View Question on Stackoverflow
Solution 1 - AndroidssantosView Answer on Stackoverflow
Solution 2 - AndroidAnubhavView Answer on Stackoverflow
Solution 3 - AndroidNull Pointer ExceptionView Answer on Stackoverflow
Solution 4 - AndroiddeadfishView Answer on Stackoverflow