Fixes the top and bottom compound drawables - Android Graphics

Android examples for Graphics:Drawable Operation

Description

Fixes the top and bottom 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;

    /**/* www  .j av  a 2 s.c  om*/
     * Fixes the top and bottom compound drawables
     *
     * @param tv
     */
    public static void vertical(TextView tv) {
        Drawable[] drawables = tv.getCompoundDrawables();
        Drawable top = drawables[TOP];
        Drawable bottom = drawables[BOTTOM];
        if ((top != null && bottom != null)
                || (top == null && bottom == null)) {
            return;
        }
        if (top == null) {
            top = createPlaceholder(bottom);
        }
        if (bottom == null) {
            bottom = createPlaceholder(top);
        }
        tv.setCompoundDrawables(drawables[LEFT], top, drawables[RIGHT],
                bottom);
    }

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

Related Tutorials