Fixes the left and right compound drawables - Android Graphics

Android examples for Graphics:Drawable Operation

Description

Fixes the left and right compound drawables

Demo Code


//package com.java2s;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

public class Main {
    private static final int LEFT = 0;
    private static final int TOP = 1;
    private static final int RIGHT = 2;
    private static final int BOTTOM = 3;

    public static void horizontal(TextView tv) {
        Drawable[] drawables = tv.getCompoundDrawables();
        Drawable left = drawables[LEFT];
        Drawable right = drawables[RIGHT];
        if ((left != null && right != null)
                || (left == null && right == null)) {
            return;
        }/*w  w  w . j  a  v a  2  s  .  c o m*/
        if (right == null) {
            right = createPlaceholder(left);
        }
        if (left == null) {
            left = createPlaceholder(right);
        }
        tv.setCompoundDrawables(left, drawables[TOP], right,
                drawables[BOTTOM]);
    }

    private static Drawable createPlaceholder(Drawable original) {
        Drawable placeholder = new ColorDrawable();
        placeholder.setBounds(original.getBounds());
        return placeholder;
    }
}

Related Tutorials