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

The following examples show how to use android.widget.TextView#getCompoundDrawablesRelative() . 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 check out the related API usage on the sidebar.
Example 1
Source File: ShareLinkLayoutManager.java    From Hauk with 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 File: DrawableTintSetter.java    From aircon with 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
Source File: ApiCompatibilityUtils.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @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
Source File: CustomMatchers.java    From ribot-app-android with Apache License 2.0 5 votes vote down vote up
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
Source File: MdLayoutInflaterFactory.java    From android-md-core with Apache License 2.0 5 votes vote down vote up
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
Source File: DrawableColorHelper.java    From adamant-android with GNU General Public License v3.0 4 votes vote down vote up
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]);
}