Example usage for android.widget TextView getCompoundDrawablePadding

List of usage examples for android.widget TextView getCompoundDrawablePadding

Introduction

In this page you can find the example usage for android.widget TextView getCompoundDrawablePadding.

Prototype

public int getCompoundDrawablePadding() 

Source Link

Document

Returns the padding between the compound drawables and the text.

Usage

From source file:Main.java

private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;//ww w  .  j  a va  2 s.  c om

    switch (gravity) {
    case Gravity.END:
        ratio = -1;
    case Gravity.START:
        total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth()
                + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
        canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
        break;
    case Gravity.BOTTOM:
        ratio = -1;
    case Gravity.TOP:
        Paint.FontMetrics fontMetrics = view.getPaint().getFontMetrics();
        total = fontMetrics.descent - fontMetrics.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}

From source file:Main.java

private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;/*  w  ww.  j  a va  2s . c  om*/

    switch (gravity) {
    case Gravity.RIGHT:
        ratio = -1;
    case Gravity.LEFT:
        total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth()
                + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
        canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
        break;
    case Gravity.BOTTOM:
        ratio = -1;
    case Gravity.TOP:
        Paint.FontMetrics fontMetrics0 = view.getPaint().getFontMetrics();
        total = fontMetrics0.descent - fontMetrics0.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}

From source file:org.telegram.ui.ProfileActivity.java

private static int getSideDrawablesSize(TextView textView) {
    Drawable[] drawable = textView.getCompoundDrawables();
    int size = 0;
    if (drawable[0] != null) {
        size += drawable[0].getIntrinsicWidth() + textView.getCompoundDrawablePadding();
    }//from  ww w. ja  v  a2  s  .c om
    if (drawable[2] != null) {
        size += drawable[2].getIntrinsicWidth() + textView.getCompoundDrawablePadding();
    }
    return size;
}