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

The following examples show how to use android.widget.TextView#getCompoundDrawables() . 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: DynamicHelper.java    From json2view with 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 2
Source File: StatusDataSetter.java    From Simpler with 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 3
Source File: Workspace.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 4
Source File: DrawableProvider.java    From MVPArms with 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 5
Source File: CustomMatchers.java    From friendspell with 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 6
Source File: CustomMatchers.java    From friendspell with 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 7
Source File: FixedTabIndicator.java    From DropDownMenu with 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 8
Source File: ResCompat.java    From NoHttp with 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 9
Source File: CreateFromQMSLayerDialog.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 10
Source File: TSnackbar.java    From TSnackBar with 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 11
Source File: BindingAdapters.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: ResCompat.java    From NoHttp with 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 13
Source File: RightDrawableOnTouchListener.java    From ShoppingList with MIT License 5 votes vote down vote up
/**
 * @param keyword
 */
public RightDrawableOnTouchListener(TextView view) {
    super();
    final Drawable[] drawables = view.getCompoundDrawables();
    if (drawables != null && drawables.length == 4)
        this.drawable = drawables[2];
}
 
Example 14
Source File: DrawableProvider.java    From AndroidBase with 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 15
Source File: ResCompat.java    From NoHttp with 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 16
Source File: ViewUtil.java    From friendspell with 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 17
Source File: Util.java    From UETool with 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 18
Source File: DataReductionSiteBreakdownView.java    From 365browser with Apache License 2.0 5 votes vote down vote up
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 19
Source File: SNavigationDrawer.java    From Newslly with 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 20
Source File: FolderIcon.java    From LB-Launcher with 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;
}