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

The following examples show how to use android.widget.TextView#setCompoundDrawablesRelative() . 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 check out the related API usage on the sidebar.
Example 1
private void updateStatus(CharSequence text, @AttrRes int attr, Drawable icon) {
    if (mViewHolder != null) {
        CardView statusCard = (CardView) ((ViewGroup) mViewHolder.itemView).getChildAt(0);
        TextView status = statusCard.findViewById(android.R.id.text1);

        if (icon != null) {
            icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
            status.setCompoundDrawablesRelative(icon, null, null, null);
        } else {
            status.setCompoundDrawablesRelative(null, null, null, null);
        }
        status.setText(text);

        int[] attrs = {attr};
        TypedArray a = getContext().obtainStyledAttributes(attrs);
        int color = a.getColor(0, 0);
        a.recycle();

        statusCard.setCardBackgroundColor(color);
    }
}
 
Example 2
Source Project: Noyze   File: PackageChooserPreference.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup container) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item_intent_check, container, false);
    }

    ActivityInfo ai = mInfos.get(position);
    TextView title = (TextView) convertView;
    title.setText(ai.label);
    int iconSize = Resources.getSystem().getDimensionPixelSize(android.R.dimen.app_icon_size);
    ai.icon.setBounds(0, 0, iconSize, iconSize);
    title.setCompoundDrawablesRelative(ai.icon, null, null, null);

    if (title instanceof Checkable) {
        Checkable checker = (Checkable) title;
        checker.setChecked(appPackages.contains(ai.componentName.getPackageName()));
    }

    return convertView;
}
 
Example 3
Source Project: Noyze   File: PackageChooserPreference.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup container) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item_intent_check, container, false);
    }

    ActivityInfo ai = mInfos.get(position);
    TextView title = (TextView) convertView;
    title.setText(ai.label);
    int iconSize = Resources.getSystem().getDimensionPixelSize(android.R.dimen.app_icon_size);
    ai.icon.setBounds(0, 0, iconSize, iconSize);
    title.setCompoundDrawablesRelative(ai.icon, null, null, null);

    if (title instanceof Checkable) {
        Checkable checker = (Checkable) title;
        checker.setChecked(appPackages.contains(ai.componentName.getPackageName()));
    }

    return convertView;
}
 
Example 4
/**
 * @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
 *      Drawable)
 */
public static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top,
        Drawable end, Drawable bottom) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
        // On JB MR1, due to a platform bug, setCompoundDrawablesRelative() is a no-op if the
        // view has ever been measured. As a workaround, use setCompoundDrawables() directly.
        // See: http://crbug.com/368196 and http://crbug.com/361709
        boolean isRtl = isLayoutRtl(textView);
        textView.setCompoundDrawables(isRtl ? end : start, top, isRtl ? start : end, bottom);
    } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
        textView.setCompoundDrawablesRelative(start, top, end, bottom);
    } else {
        textView.setCompoundDrawables(start, top, end, bottom);
    }
}
 
Example 5
Source Project: science-journal   File: NoteViewHolder.java    License: Apache License 2.0 5 votes vote down vote up
public static void loadSnapshotsIntoList(
    ViewGroup valuesList, Label label, AppAccount appAccount) {
  Context context = valuesList.getContext();
  SnapshotLabelValue snapshotLabelValue = label.getSnapshotLabelValue();

  valuesList.setVisibility(View.VISIBLE);
  // Make sure it has the correct number of views, re-using as many as possible.
  int childCount = valuesList.getChildCount();
  int snapshotsCount = snapshotLabelValue.getSnapshotsCount();
  if (childCount < snapshotsCount) {
    for (int i = 0; i < snapshotsCount - childCount; i++) {
      LayoutInflater.from(context).inflate(R.layout.snapshot_value_details, valuesList);
    }
  } else if (childCount > snapshotsCount) {
    valuesList.removeViews(0, childCount - snapshotsCount);
  }

  SensorAppearanceProvider sensorAppearanceProvider =
      AppSingleton.getInstance(context).getSensorAppearanceProvider(appAccount);

  String valueFormat = context.getResources().getString(R.string.data_with_units);
  for (int i = 0; i < snapshotsCount; i++) {
    GoosciSnapshotValue.SnapshotLabelValue.SensorSnapshot snapshot =
        snapshotLabelValue.getSnapshots(i);
    ViewGroup snapshotLayout = (ViewGroup) valuesList.getChildAt(i);

    GoosciSensorAppearance.BasicSensorAppearance appearance =
        snapshot.getSensor().getRememberedAppearance();
    TextView sensorName = (TextView) snapshotLayout.findViewById(R.id.sensor_name);
    sensorName.setCompoundDrawablesRelative(null, null, null, null);
    sensorName.setText(appearance.getName());
    String value =
        BuiltInSensorAppearance.formatValue(
            snapshot.getValue(), appearance.getPointsAfterDecimal());
    ((TextView) snapshotLayout.findViewById(R.id.sensor_value))
        .setText(String.format(valueFormat, value, appearance.getUnits()));

    loadLargeDrawable(appearance, sensorAppearanceProvider, snapshotLayout, snapshot.getValue());
  }
}
 
Example 6
/**
 * @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
 *      Drawable)
 */
public static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top,
        Drawable end, Drawable bottom) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
        // On JB MR1, due to a platform bug, setCompoundDrawablesRelative() is a no-op if the
        // view has ever been measured. As a workaround, use setCompoundDrawables() directly.
        // See: http://crbug.com/368196 and http://crbug.com/361709
        boolean isRtl = isLayoutRtl(textView);
        textView.setCompoundDrawables(isRtl ? end : start, top, isRtl ? start : end, bottom);
    } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
        textView.setCompoundDrawablesRelative(start, top, end, bottom);
    } else {
        textView.setCompoundDrawables(start, top, end, bottom);
    }
}
 
Example 7
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]);
}