Java Code Examples for android.widget.TextView#getCompoundDrawablesRelative()

The following examples show how to use android.widget.TextView#getCompoundDrawablesRelative() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: Hauk   File: ShareLinkLayoutManager.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Calculates the real displayed with of a {@link TextView}. The measurement of text view
 * (button) width is unreliable when inflating views, as it does not appear to take into account
 * drawables or padding properly. This method calculates the drawn width manually instead,
 * giving a reliable width that prevents character wrapping.
 *
 * @param view The text view to calculate the width for.
 * @return A width in pixels.
 */
private static int calculateRealWidth(TextView view) {
    // Calculate the padding of the view.
    int realWidth = view.getCompoundDrawablePadding() + view.getTotalPaddingStart() + view.getTotalPaddingEnd();

    // Add the width of the contained text.
    String text = view.getTransformationMethod().getTransformation(view.getText(), view).toString();
    realWidth += view.getPaint().measureText(text);

    // Add the widths of any drawables on the button.
    for (Drawable drawable : view.getCompoundDrawablesRelative()) {
        if (drawable != null) realWidth += drawable.getIntrinsicWidth();
    }

    // Return the result.
    return realWidth;
}
 
Example 2
Source Project: aircon   File: DrawableTintSetter.java    License: MIT License 5 votes vote down vote up
private void setAttr(final TextView view, final Integer color) {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
		if (color != null) {
			view.setCompoundDrawableTintList(ColorStateList.valueOf(color));
		}
	}
	else {
		Drawable[] drawables = view.getCompoundDrawablesRelative();
		for (Drawable drawable : drawables) {
			if (drawable != null && color != null) {
				DrawableCompat.setTint(drawable, color);
			}
		}
	}
}
 
Example 3
/**
 * @see android.widget.TextView#getCompoundDrawablesRelative()
 */
public static Drawable[] getCompoundDrawablesRelative(TextView textView) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return textView.getCompoundDrawablesRelative();
    } else {
        return textView.getCompoundDrawables();
    }
}
 
Example 4
public static Matcher<View> hasCompoundDrawableRelative(final boolean start,
                                                        final boolean top,
                                                        final boolean end,
                                                        final boolean bottom) {
    return new TypeSafeMatcher<View>() {
        @Override
        protected boolean matchesSafely(View view) {
            if (view instanceof TextView) {
                TextView textView = (TextView) view;
                Drawable[] drawables = textView.getCompoundDrawablesRelative();
                boolean hasStart = drawables[0] != null;
                boolean hastTop = drawables[1] != null;
                boolean hasEnd = drawables[2] != null;
                boolean hasBottom = drawables[3] != null;
                return start == hasStart &&
                        top == hastTop &&
                        end == hasEnd &&
                        bottom == hasBottom;

            }
            return false;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("CompoundDrawables relative not matched");
        }
    };

}
 
Example 5
protected void supportVectorDrawable(Context context, TextView view, AttributeSet attrs) {
  Drawable[] drawablesCompat = MdVectorDrawableCompat.getFromAttribute(context, attrs,
      R.attr.drawableLeftCompat, R.attr.drawableTopCompat, R.attr.drawableRightCompat, R.attr.drawableBottomCompat,
      R.attr.drawableStartCompat, R.attr.drawableEndCompat);
  boolean shouldInitiate = false;
  for (Drawable drawable : drawablesCompat) {
    if (drawable != null) {
      shouldInitiate = true;
      break;
    }
  }
  if (shouldInitiate) {
    Drawable[] drawables = view.getCompoundDrawables();
    Drawable[] drawablesRelative;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
      drawablesRelative = view.getCompoundDrawablesRelative();
    } else {
      drawablesRelative = drawables;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      view.setCompoundDrawablesRelativeWithIntrinsicBounds(
          getDrawable(drawablesCompat[4] != null ? drawablesCompat[4] : drawablesCompat[0], drawablesRelative[0], drawables[0]),
          getDrawable(drawablesCompat[1], drawablesRelative[1], drawables[1]),
          getDrawable(drawablesCompat[5] != null ? drawablesCompat[5] : drawablesCompat[2], drawablesRelative[2], drawables[2]),
          getDrawable(drawablesCompat[3], drawablesRelative[3], drawables[3])
      );
    } else {
      view.setCompoundDrawablesWithIntrinsicBounds(
          getDrawable(drawablesCompat[4] != null ? drawablesCompat[4] : drawablesCompat[0], drawablesRelative[0], drawables[0]),
          getDrawable(drawablesCompat[1], drawablesRelative[1], drawables[1]),
          getDrawable(drawablesCompat[5] != null ? drawablesCompat[5] : drawablesCompat[2], drawablesRelative[2], drawables[2]),
          getDrawable(drawablesCompat[3], drawablesRelative[3], drawables[3])
      );
    }
  }
}
 
Example 6
public static void changeColorForDrawable(Context context, TextView textView, int color, PorterDuff.Mode mode) {
    if (textView == null) { return; }
    Drawable[] compoundDrawablesRelative = textView.getCompoundDrawablesRelative();
    Drawable[] newDrawablesRelative = changeColorForDrawable(context, color, mode, compoundDrawablesRelative);
    textView.setCompoundDrawablesRelative(newDrawablesRelative[0], newDrawablesRelative[1], newDrawablesRelative[2], newDrawablesRelative[3]);
}