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

The following examples show how to use android.widget.TextView#getCompoundDrawables() . 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: Simpler   File: StatusDataSetter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onDestroySuccess(long sid, long aid, TextView tvAttitude) {
    AttitudeContainer.sHeartContainer.remove(sid);
    Drawable[] compoundDrawables = tvAttitude.getCompoundDrawables();
    if (compoundDrawables[0] != null) {
        Drawable drawable = mActivity.getResources().getDrawable(R.drawable.ic_like);
        // 必须设置图片大小,否则不显示
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvAttitude.setCompoundDrawables(drawable, null, null, null);
    } else {
        tvAttitude.setTextColor(ContextCompat.getColor(mActivity, R.color.retweeted_count_text_color));
    }
    String countStr = tvAttitude.getText().toString();
    if (TextUtils.isDigitsOnly(countStr)) {
        int count = Integer.parseInt(countStr) - 1;
        tvAttitude.setText(NumberFormatter.formatWBCount(count, 60000));
    }
}
 
Example 2
Source Project: json2view   File: DynamicHelper.java    License: MIT License 6 votes vote down vote up
/**
 * apply compound property in textView
 * position 0:left, 1:top, 2:right, 3:bottom
 * - REF : drawable to load as compoundDrawable
 * - BASE64 : decode as base64 and set as CompoundDrawable
 */
public static void applyCompoundDrawable(View view, DynamicProperty property, int position) {
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
        Drawable[] d = textView.getCompoundDrawables();
        switch (property.type) {
            case REF: {
                try {
                    d[position] = view.getContext().getResources().getDrawable(getDrawableId(view.getContext(), property.getValueString()));
                } catch (Exception e) {}
            }
            break;
            case BASE64: {
                d[position] = property.getValueBitmapDrawable();
            }
            break;
            case DRAWABLE: {
                d[position] = property.getValueGradientDrawable();
            }
            break;
        }
        textView.setCompoundDrawablesWithIntrinsicBounds(d[0], d[1], d[2], d[3]);
    }
}
 
Example 3
@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);
    }
    textView.setCompoundDrawablesWithIntrinsicBounds(
            drawables[0],
            top,
            drawables[2],
            drawables[3]);
}
 
Example 4
Source Project: Newslly   File: SNavigationDrawer.java    License: MIT License 5 votes vote down vote up
private void setTextViewDrawableColor(TextView textView, int color) {
    for (Drawable drawable : textView.getCompoundDrawables()) {
        if (drawable != null) {
            drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(), color), PorterDuff.Mode.SRC_IN));
        }
    }
}
 
Example 5
private Drawable getStartCompoundDrawable(TextView textView) {
    Drawable[] drawables = textView.getCompoundDrawables();
    // Start drawable can be in the left or right index based on if the layout is rtl.
    if (drawables[0] != null) {
        return drawables[0];
    }
    return drawables[2];
}
 
Example 6
Source Project: UETool   File: Util.java    License: MIT License 5 votes vote down vote up
private static List<Pair<String, Bitmap>> getTextViewDrawableBitmap(TextView textView) {
    List<Pair<String, Bitmap>> bitmaps = new ArrayList<>();
    try {
        Drawable[] drawables = textView.getCompoundDrawables();
        bitmaps.add(new Pair<>("DrawableLeft", getDrawableBitmap(drawables[0])));
        bitmaps.add(new Pair<>("DrawableTop", getDrawableBitmap(drawables[1])));
        bitmaps.add(new Pair<>("DrawableRight", getDrawableBitmap(drawables[2])));
        bitmaps.add(new Pair<>("DrawableBottom", getDrawableBitmap(drawables[3])));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmaps;
}
 
Example 7
Source Project: friendspell   File: ViewUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void setDrawable(TextView textView, Drawable drawable, int position) {
  Drawable[] drawables = textView.getCompoundDrawables();
  textView.setCompoundDrawablesWithIntrinsicBounds(
      (position == 0) ? drawable : drawables[0],
      (position == 1) ? drawable : drawables[1],
      (position == 2) ? drawable : drawables[2],
      (position == 3) ? drawable : drawables[3]);
}
 
Example 8
Source Project: NoHttp   File: ResCompat.java    License: Apache License 2.0 5 votes vote down vote up
public static void setLeftDrawable(TextView textView, Drawable leftDrawable) {
    setDrawableBounds(leftDrawable);
    Drawable top = textView.getCompoundDrawables()[1];
    Drawable right = textView.getCompoundDrawables()[2];
    Drawable bottom = textView.getCompoundDrawables()[3];
    textView.setCompoundDrawables(leftDrawable, top, right, bottom);
}
 
Example 9
Source Project: AndroidBase   File: DrawableProvider.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 将radiobutton的drawable动态的缩放
 *
 * @param percent
 * @param rb
 * @return
 */
public static Drawable getScaleDrawableForRadioButton(float percent, TextView rb) {
    Drawable[] compoundDrawables = rb.getCompoundDrawables();
    Drawable drawable = null;
    for (Drawable d : compoundDrawables) {
        if (d != null) {
            drawable = d;
        }
    }
    drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * percent + 0.5f), (int) (drawable.getIntrinsicHeight() * percent + 0.5f));
    return drawable;
}
 
Example 10
/**
 * @param keyword
 */
public RightDrawableOnTouchListener(TextView view) {
    super();
    final Drawable[] drawables = view.getCompoundDrawables();
    if (drawables != null && drawables.length == 4)
        this.drawable = drawables[2];
}
 
Example 11
Source Project: NoHttp   File: ResCompat.java    License: Apache License 2.0 5 votes vote down vote up
public static void setBottomDrawable(TextView textView, Drawable bottomDrawable) {
    setDrawableBounds(bottomDrawable);
    Drawable left = textView.getCompoundDrawables()[0];
    Drawable top = textView.getCompoundDrawables()[1];
    Drawable right = textView.getCompoundDrawables()[2];
    textView.setCompoundDrawables(left, top, right, bottomDrawable);
}
 
Example 12
/**
 * Returns the drawable for the given text view.
 */
public static Drawable getTextViewIcon(TextView tv) {
    final Drawable[] drawables = tv.getCompoundDrawables();
    for (int i = 0; i < drawables.length; i++) {
        if (drawables[i] != null) {
            return drawables[i];
        }
    }
    return null;
}
 
Example 13
Source Project: TSnackBar   File: TSnackbar.java    License: Apache License 2.0 5 votes vote down vote up
public TSnackbar setIconRight(@DrawableRes int drawableRes, float sizeDp) {
    final TextView tv = mView.getMessageView();
    Drawable drawable = ContextCompat.getDrawable(mContext, drawableRes);
    if (drawable != null) {
        drawable = fitDrawable(drawable, (int) convertDpToPixel(sizeDp, mContext));
    } else {
        throw new IllegalArgumentException("resource_id is not a valid drawable!");
    }
    final Drawable[] compoundDrawables = tv.getCompoundDrawables();
    tv.setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], drawable, compoundDrawables[3]);
    return this;
}
 
Example 14
private void setIcon(TextView view, String path) {
    Drawable icon = Drawable.createFromPath(path);
    Drawable checkbox = view.getCompoundDrawables()[2];

    if (icon != null)
        icon.setBounds(0, 0, checkbox.getIntrinsicWidth(), checkbox.getIntrinsicHeight());

    view.setCompoundDrawables(icon, null, checkbox, null);
}
 
Example 15
Source Project: NoHttp   File: ResCompat.java    License: Apache License 2.0 5 votes vote down vote up
public static void setTopDrawable(TextView textView, Drawable topDrawable) {
    setDrawableBounds(topDrawable);
    Drawable left = textView.getCompoundDrawables()[0];
    Drawable right = textView.getCompoundDrawables()[2];
    Drawable bottom = textView.getCompoundDrawables()[3];
    textView.setCompoundDrawables(left, topDrawable, right, bottom);
}
 
Example 16
Source Project: DropDownMenu   File: FixedTabIndicator.java    License: Apache License 2.0 5 votes vote down vote up
private void switchTab(int pos) {
    TextView tv = getChildAtCurPos(pos);

    Drawable drawable = tv.getCompoundDrawables()[2];
    int level = drawable.getLevel();

    if (mOnItemClickListener != null) {
        mOnItemClickListener.onItemClick(tv, pos, level == 1);
    }

    if (mLastIndicatorPosition == pos) {
        // 点击同一个条目时
        tv.setTextColor(level == 0 ? mTabSelectedColor : mTabDefaultColor);
        drawable.setLevel(1 - level);

        return;
    }

    mCurrentIndicatorPosition = pos;
    resetPos(mLastIndicatorPosition);

    //highLightPos(pos);
    tv.setTextColor(mTabSelectedColor);
    tv.getCompoundDrawables()[2].setLevel(1);

    mLastIndicatorPosition = pos;
}
 
Example 17
Source Project: friendspell   File: CustomMatchers.java    License: Apache License 2.0 5 votes vote down vote up
public static Matcher<View> withCompoundDrawable(final int position) {
  return new BoundedMatcher<View, TextView>(TextView.class) {
    @Override public void describeTo(Description description) {
      description.appendText(
          "has compound drawable resource at position " + position);
    }
    @Override public boolean matchesSafely(TextView textView) {
      Drawable drawables[] = textView.getCompoundDrawables();
      return (drawables[position] != null);
    }
  };
}
 
Example 18
Source Project: friendspell   File: CustomMatchers.java    License: Apache License 2.0 5 votes vote down vote up
public static Matcher<View> withoutCompoundDrawable(final int position) {
  return new BoundedMatcher<View, TextView>(TextView.class) {
    @Override public void describeTo(Description description) {
      description.appendText(
          "does not have compound drawable at position " + position);
    }
    @Override public boolean matchesSafely(TextView textView) {
      Drawable drawables[] = textView.getCompoundDrawables();
      return (drawables[position] == null);
    }
  };
}
 
Example 19
Source Project: MVPArms   File: DrawableProvider.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 将 TextView/RadioButton 中设置的 drawable 动态的缩放
 *
 * @param percent
 * @param tv
 * @return
 */
public static Drawable getScaleDrawableForRadioButton(float percent, TextView tv) {
    Drawable[] compoundDrawables = tv.getCompoundDrawables();
    Drawable drawable = null;
    for (Drawable d : compoundDrawables) {
        if (d != null) {
            drawable = d;
        }
    }
    return getScaleDrawable(percent, drawable);
}
 
Example 20
Source Project: LB-Launcher   File: FolderIcon.java    License: Apache License 2.0 4 votes vote down vote up
private Drawable getTopDrawable(TextView v) {
    Drawable d = v.getCompoundDrawables()[1];
    return (d instanceof PreloadIconDrawable) ? ((PreloadIconDrawable) d).mIcon : d;
}