Example usage for android.widget TextView getCompoundDrawables

List of usage examples for android.widget TextView getCompoundDrawables

Introduction

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

Prototype

@NonNull
public Drawable[] getCompoundDrawables() 

Source Link

Document

Returns drawables for the left, top, right, and bottom borders.

Usage

From source file:Main.java

public static void addBadge(TextView tv, Drawable d) {
    Drawable[] dws = tv.getCompoundDrawables();
    dws[2] = d;/*  w  w w . j  a v a 2s. c  om*/
    tv.setCompoundDrawablesWithIntrinsicBounds(dws[0], dws[1], dws[2], dws[3]);
}

From source file:Main.java

public static void startAnimation(TextView textView) {
    Drawable[] drawables = textView.getCompoundDrawables();
    for (Drawable drawable : drawables) {
        if (drawable != null && drawable instanceof Animatable) {
            ((Animatable) drawable).start();
        }/*from  ww w  . java 2  s. c  o  m*/
    }
}

From source file:Main.java

/**
 * Fixes the top and bottom compound drawables
 *
 * @param tv//from   ww w  .j av  a  2 s  .c o  m
 */
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);
}

From source file:Main.java

/**
 * Fixes the left and right compound drawables
 *
 * @param tv/*w w w. ja  v a  2 s  .c  om*/
 */
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;
    }
    if (right == null) {
        right = createPlaceholder(left);

        // create a placeholder drawable of the same size on the right
    }
    if (left == null) {
        left = createPlaceholder(right);
    }
    tv.setCompoundDrawables(left, drawables[TOP], right, drawables[BOTTOM]);
}

From source file:Main.java

static void preDraw(TextView view, Canvas canvas) {
    Drawable[] drawables = view.getCompoundDrawables();
    if (drawables[0] != null) {
        view.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
        onCenterDraw(view, canvas, drawables[0], Gravity.START);
    } else if (drawables[1] != null) {
        view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        onCenterDraw(view, canvas, drawables[1], Gravity.TOP);
    } else if (drawables[2] != null) {
        view.setGravity(Gravity.CENTER_VERTICAL | Gravity.END);
        onCenterDraw(view, canvas, drawables[2], Gravity.END);
    } else if (drawables[3] != null) {
        view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        onCenterDraw(view, canvas, drawables[3], Gravity.BOTTOM);
    }//  w w  w  .j  av a2s .  c om
}

From source file:Main.java

public static void preDraw(TextView view, Canvas canvas) {
    Drawable[] drawables = view.getCompoundDrawables();
    if (drawables != null) {
        if (drawables[0] != null) {
            view.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            onCenterDraw(view, canvas, drawables[0], Gravity.LEFT);
        } else if (drawables[1] != null) {
            view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
            onCenterDraw(view, canvas, drawables[1], Gravity.TOP);
        } else if (drawables[2] != null) {
            view.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
            onCenterDraw(view, canvas, drawables[2], Gravity.RIGHT);
        } else if (drawables[3] != null) {
            view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            onCenterDraw(view, canvas, drawables[3], Gravity.BOTTOM);
        }/* www.j  a  va  2s.  co m*/
    }
}

From source file:me.selinali.tribbble.utils.ViewUtils.java

public static void tintDrawable(TextView textView, int position) {
    DrawableCompat.setTint(textView.getCompoundDrawables()[position], color(R.color.textNormal));
}

From source file:com.doctoror.fuckoffmusicplayer.presentation.util.BindingAdapters.java

@BindingAdapter({ "drawableTop", "tintAttr" })
public static void setDrawableTopTintedFromAttr(@NonNull final TextView textView, @Nullable Drawable top,
        @AttrRes final int tintAttr) {
    final Drawable[] drawables = textView.getCompoundDrawables();
    if (top != null) {
        top = DrawableUtils.getTintedDrawableFromAttrTint(textView.getContext(), top, tintAttr);
    }//ww w.  j a  va 2 s.  c  o m
    textView.setCompoundDrawablesWithIntrinsicBounds(drawables[0], top, drawables[2], drawables[3]);
}

From source file:android.support.design.testutils.TestUtilsMatchers.java

/**
 * Returns a matcher that matches TextViews whose start drawable is filled with the specified
 * fill color.//from  w ww  .j  a v  a 2 s  .  c om
 */
public static Matcher withStartDrawableFilledWith(final @ColorInt int fillColor,
        final int allowedComponentVariance) {
    return new BoundedMatcher<View, TextView>(TextView.class) {
        private String failedCheckDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText(failedCheckDescription);
        }

        @Override
        public boolean matchesSafely(final TextView view) {
            final Drawable[] compoundDrawables = view.getCompoundDrawables();
            final boolean isRtl = (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL);
            final Drawable startDrawable = isRtl ? compoundDrawables[2] : compoundDrawables[0];
            if (startDrawable == null) {
                failedCheckDescription = "no start drawable";
                return false;
            }
            try {
                final Rect bounds = startDrawable.getBounds();
                TestUtils.assertAllPixelsOfColor("", startDrawable, bounds.width(), bounds.height(), true,
                        fillColor, allowedComponentVariance, true);
            } catch (Throwable t) {
                failedCheckDescription = t.getMessage();
                return false;
            }
            return true;
        }
    };
}

From source file:Main.java

/**
 * Make a textview to a collapsible textview with the indicator on the right of the textview
 * /*from w w w.j av  a2 s .  co  m*/
 * @param tv , {@link TextView} to be converted
 * @param upDrawableResId , drawable resource id to be used as up indicator
 * @param downDrawableResId , drawable resource id to be used as down indicator
 * @param lineTreshold , no of line to be displayed for the collapsed state
 */
public static void makeCollapsible(final TextView tv, int upDrawableResId, int downDrawableResId,
        final int lineTreshold) {

    final Drawable[] drawables = tv.getCompoundDrawables();
    final Drawable up = tv.getContext().getResources().getDrawable(upDrawableResId);
    final Drawable down = tv.getContext().getResources().getDrawable(downDrawableResId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down, drawables[3]);
        tv.setEllipsize(TruncateAt.END);
        tv.setMaxLines(lineTreshold);
        tv.setTag(true);
        tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v instanceof TextView) {
                    TextView tv = (TextView) v;
                    boolean snippet = (Boolean) tv.getTag();
                    if (snippet) {
                        // show everything
                        snippet = false;
                        tv.setMaxLines(Integer.MAX_VALUE);
                        tv.setEllipsize(null);
                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], up,
                                drawables[3]);
                    } else {
                        // show snippet
                        snippet = true;
                        tv.setMaxLines(lineTreshold);
                        tv.setEllipsize(TruncateAt.END);
                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down,
                                drawables[3]);
                    }
                    tv.setTag(snippet);
                }

            }
        });
    } else {
        tv.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {

                ViewTreeObserver vto = tv.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                    @SuppressWarnings("deprecation")
                    @SuppressLint("NewApi")
                    @Override
                    public void onGlobalLayout() {

                        tv.setEllipsize(TruncateAt.END);
                        int line = tv.getLineCount();
                        tv.setMaxLines(lineTreshold);
                        if (line <= lineTreshold) {
                            tv.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View arg0) {
                                    // empty listener
                                    // Log.d("line count", "count: "+
                                    // tv.getLineCount());
                                }
                            });
                            if (tv.getLayout() != null) {
                                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                                    tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                                } else {
                                    tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                                }
                            }
                            return;
                        }

                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down,
                                drawables[3]);
                        tv.setTag(true);
                        tv.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                if (v instanceof TextView) {
                                    TextView tv = (TextView) v;
                                    boolean snippet = (Boolean) tv.getTag();
                                    if (snippet) {
                                        snippet = false;
                                        // show everything
                                        tv.setMaxLines(Integer.MAX_VALUE);
                                        tv.setEllipsize(null);
                                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1],
                                                up, drawables[3]);
                                    } else {
                                        snippet = true;
                                        // show snippet
                                        tv.setMaxLines(lineTreshold);
                                        tv.setEllipsize(TruncateAt.END);
                                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1],
                                                down, drawables[3]);
                                    }
                                    tv.setTag(snippet);
                                }

                            }
                        });

                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                            tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        } else {
                            tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }
                    }

                });
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

        });
    }

}