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

The following examples show how to use android.widget.TextView#getContext() . 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: AccountMoneyDescriptorModel.java    From px-android with MIT License 6 votes vote down vote up
@Override
public void updateLeftSpannable(@NonNull final SpannableStringBuilder spannableStringBuilder,
    @NonNull final TextView textView) {

    final Context context = textView.getContext();

    if (showAmount) {
        updateInstallment(spannableStringBuilder, context, textView);
        spannableStringBuilder.append(TextUtil.SPACE);
    }

    if (accountMoneyMetadata.displayInfo != null) {
        sliderTitle = accountMoneyMetadata.displayInfo.sliderTitle;
        if (TextUtil.isEmpty(sliderTitle)) {
            spannableStringBuilder.append(TextUtil.SPACE);
        } else {
            final AmountLabeledFormatter amountLabeledFormatter =
                new AmountLabeledFormatter(spannableStringBuilder, context)
                    .withTextColor(ContextCompat.getColor(context, R.color.ui_meli_grey));
            amountLabeledFormatter.apply(sliderTitle);
        }
    }
}
 
Example 2
Source File: BindingAdapters.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
@BindingAdapter("date")
public static void setInteger(TextView textView, Date receivedAt) {
    if (receivedAt == null || receivedAt.getTime() <= 0) {
        textView.setVisibility(View.GONE);
    } else {
        Context context = textView.getContext();
        Calendar specifiedDate = Calendar.getInstance();
        specifiedDate.setTime(receivedAt);
        Calendar today = Calendar.getInstance();
        textView.setVisibility(View.VISIBLE);

        long diff = today.getTimeInMillis() - specifiedDate.getTimeInMillis();

        if (sameDay(today, specifiedDate) || diff < SIX_HOURS) {
            textView.setText(DateUtils.formatDateTime(context, receivedAt.getTime(), DateUtils.FORMAT_SHOW_TIME));
        } else if (sameYear(today, specifiedDate) || diff < THREE_MONTH) {
            textView.setText(DateUtils.formatDateTime(context, receivedAt.getTime(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL));
        } else {
            textView.setText(DateUtils.formatDateTime(context, receivedAt.getTime(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY | DateUtils.FORMAT_ABBREV_ALL));
        }
    }
}
 
Example 3
Source File: BindingAdapters.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
@BindingAdapter("android:text")
public static void setText(final TextView textView, final FullEmail.From from) {
    if (from instanceof FullEmail.NamedFrom) {
        final FullEmail.NamedFrom named = (FullEmail.NamedFrom) from;
        textView.setText(named.getName());
    } else if (from instanceof FullEmail.DraftFrom) {
        final Context context = textView.getContext();
        final SpannableString spannable = new SpannableString(context.getString(R.string.draft));
        spannable.setSpan(
                new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimary)),
                0,
                spannable.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        textView.setText(spannable);
    }
}
 
Example 4
Source File: BadgeStyle.java    From MaterialDrawer-Xamarin with Apache License 2.0 6 votes vote down vote up
public void style(TextView badgeTextView, ColorStateList colorStateList) {
    Context ctx = badgeTextView.getContext();
    //set background for badge
    if (mBadgeBackground == null) {
        UIUtils.setBackground(badgeTextView, new BadgeDrawableBuilder(this).build(ctx));
    } else {
        UIUtils.setBackground(badgeTextView, mBadgeBackground);
    }

    //set the badge text color
    if (mTextColor != null) {
        ColorHolder.applyToOr(mTextColor, badgeTextView, null);
    } else if (colorStateList != null) {
        badgeTextView.setTextColor(colorStateList);
    }

    //set the padding
    int paddingLeftRight = mPaddingLeftRight.asPixel(ctx);
    int paddingTopBottom = mPaddingTopBottom.asPixel(ctx);
    badgeTextView.setPadding(paddingLeftRight, paddingTopBottom, paddingLeftRight, paddingTopBottom);

    //set the min width
    badgeTextView.setMinWidth(mMinWidth.asPixel(ctx));
}
 
Example 5
Source File: DiscussionUtils.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the state, text and icon of the new item creation button on discussion screens
 *
 * @param isTopicClosed     Boolean if the topic is closed or not
 * @param textView          The TextView whose text has to be updated
 * @param positiveTextResId The text resource to be applied when topic IS NOT closed
 * @param negativeTextResId The text resource to be applied when topic IS closed
 * @param creationLayout    The layout which should be enabled/disabled and applied listener to
 * @param listener          The listener to apply to creationLayout
 */
public static void setStateOnTopicClosed(boolean isTopicClosed, TextView textView,
                                         @StringRes int positiveTextResId,
                                         @StringRes int negativeTextResId,
                                         ViewGroup creationLayout,
                                         View.OnClickListener listener) {
    Context context = textView.getContext();
    if (isTopicClosed) {
        textView.setText(negativeTextResId);
        TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(textView,
                new IconDrawable(context, FontAwesomeIcons.fa_lock)
                        .sizeRes(context, R.dimen.small_icon_size)
                        .colorRes(context, R.color.white),
                null, null, null
        );
        creationLayout.setOnClickListener(null);
    } else {
        textView.setText(positiveTextResId);
        TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(textView,
                new IconDrawable(context, FontAwesomeIcons.fa_plus_circle)
                        .sizeRes(context, R.dimen.small_icon_size)
                        .colorRes(context, R.color.white),
                null, null, null
        );
        creationLayout.setOnClickListener(listener);
    }
    creationLayout.setEnabled(!isTopicClosed);
}
 
Example 6
Source File: ViewMatchers.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a matcher that matches {@link android.widget.TextView} based on it's color.
 *
 * <p><b>This API is currently in beta.</b>
 */
@Beta
public static Matcher<View> hasTextColor(final int colorResId) {
  return new BoundedMatcher<View, TextView>(TextView.class) {
    private Context context;

    @Override
    protected boolean matchesSafely(TextView textView) {
      context = textView.getContext();
      int textViewColor = textView.getCurrentTextColor();
      int expectedColor;

      if (Build.VERSION.SDK_INT <= 22) {
        expectedColor = context.getResources().getColor(colorResId);
      } else {
        expectedColor = context.getColor(colorResId);
      }

      return textViewColor == expectedColor;
    }

    @Override
    public void describeTo(Description description) {
      String colorId = String.valueOf(colorResId);
      if (context != null) {
        colorId = context.getResources().getResourceName(colorResId);
      }
      description.appendText("has color with ID " + colorId);
    }
  };
}
 
Example 7
Source File: MainActivity.java    From android-EmojiCompat with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitialized() {
    final TextView regularTextView = mRegularTextViewRef.get();
    if (regularTextView != null) {
        final EmojiCompat compat = EmojiCompat.get();
        final Context context = regularTextView.getContext();
        regularTextView.setText(
                compat.process(context.getString(R.string.regular_text_view, EMOJI)));
    }
}
 
Example 8
Source File: AutofitHelper.java    From DarkCalculator with MIT License 5 votes vote down vote up
private AutofitHelper(TextView view) {
    final Context context = view.getContext();
    float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;

    mTextView = view;
    mPaint = new TextPaint();
    setRawTextSize(view.getTextSize());

    mMaxLines = getMaxLines(view);
    mMinTextSize = scaledDensity * DEFAULT_MIN_TEXT_SIZE;
    mMaxTextSize = mTextSize;
    mPrecision = DEFAULT_PRECISION;
}
 
Example 9
Source File: VectorCompatHelper.java    From pandroid with Apache License 2.0 5 votes vote down vote up
static void setupView(TextView view, AttributeSet attrs) {
    Context context = view.getContext();
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PandroidCompat);
    try {
        int drawableLeftRes = typedArray.getResourceId(R.styleable
                .PandroidCompat_drawableLeftPCompat, 0);
        int drawableTopRes = typedArray.getResourceId(R.styleable
                .PandroidCompat_drawableTopPCompat, 0);
        int drawableRightRes = typedArray.getResourceId(R.styleable
                .PandroidCompat_drawableRightPCompat, 0);
        int drawableBottomRes = typedArray.getResourceId(R.styleable
                .PandroidCompat_drawableBottomPCompat, 0);

        Integer tintColor = null;
        if (typedArray.hasValue(R.styleable.PandroidCompat_drawableTintPCompat)) {
            tintColor = typedArray.getColor(R.styleable.PandroidCompat_drawableTintPCompat, 0);
        }
        Drawable drawableStart = getDrawableInternal(context, drawableLeftRes, tintColor);
        Drawable drawableTop = getDrawableInternal(context, drawableTopRes, tintColor);
        Drawable drawableEnd = getDrawableInternal(context, drawableRightRes, tintColor);
        Drawable drawableBottom = getDrawableInternal(context, drawableBottomRes, tintColor);

        view.setCompoundDrawablesWithIntrinsicBounds(drawableStart, drawableTop, drawableEnd, drawableBottom);
    } finally {
        typedArray.recycle();
    }
}
 
Example 10
Source File: AutofitHelper.java    From DarkCalculator with MIT License 5 votes vote down vote up
/**
 * Creates a new instance of {@code AutofitHelper} that wraps a {@link TextView} and enables
 * automatically sizing the text to fit.
 */
public static AutofitHelper create(TextView view, AttributeSet attrs, int defStyle) {
    AutofitHelper helper = new AutofitHelper(view);
    boolean sizeToFit = true;
    if (attrs != null) {
        Context context = view.getContext();
        int minTextSize = (int) helper.getMinTextSize();
        float precision = helper.getPrecision();

        TypedArray ta = context.obtainStyledAttributes(
                attrs,
                R.styleable.AutofitTextView,
                defStyle,
                0);
        sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit);
        minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize,
                minTextSize);
        precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision);
        ta.recycle();

        helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, minTextSize)
                .setPrecision(precision);
    }
    helper.setEnabled(sizeToFit);

    return helper;
}
 
Example 11
Source File: DisabledPaymentMethodDescriptorModel.java    From px-android with MIT License 5 votes vote down vote up
@Override
public void updateLeftSpannable(@NonNull final SpannableStringBuilder spannableStringBuilder,
    @NonNull final TextView textView) {
    final Context context = textView.getContext();
    final SpannableFormatter amountLabeledFormatter = new SpannableFormatter(spannableStringBuilder, context);
    amountLabeledFormatter.withTextColor(ContextCompat.getColor(context, R.color.ui_meli_blue))
        .withStyle(PxFont.SEMI_BOLD);
    if (message != null && TextUtil.isNotEmpty(message.getMessage())) {
        description = message.getMessage();
    } else {
        description = context.getString(R.string.px_payment_method_disable_title);
    }

    amountLabeledFormatter.apply(description);
}
 
Example 12
Source File: MenuMainDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void highlightText(TextView textView) {
  Context context = textView.getContext();
  CharSequence text = textView.getText();
  TypedValue value = new TypedValue();
  context.getTheme().resolveAttribute(R.attr.colorPrimary, value, true);
  Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
  spanText.setSpan(
      new BackgroundColorSpan(value.data), 0, text.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
  textView.setText(spanText);
}
 
Example 13
Source File: VoiceRecodingPanel.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public SlideToCancel(TextView slideToCancelView) {
    this.slideToCancelView = slideToCancelView;

    Context context = slideToCancelView.getContext();
    int clr = context.getResources().getColor(R.color.common_foreground_color);
    for (Drawable drawable : slideToCancelView.getCompoundDrawables()) {
        if (drawable != null) {
            drawable.setColorFilter(new PorterDuffColorFilter(clr, PorterDuff.Mode.SRC_IN));
        }
    }
}
 
Example 14
Source File: FormFieldSelectFragment.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
private static void addDetectedValueHeader(@NonNull ListView listView, @StringRes int labelRes, @NonNull String labelKey, @NonNull String labelValue, @NonNull String value, @NonNull Icon icon) {
    final TextView textView = (TextView) LayoutInflater.from(listView.getContext()).inflate(R.layout.edx_selectable_list_item, listView, false);
    {
        final SpannableString labelValueSpan = new SpannableString(labelValue);
        labelValueSpan.setSpan(new ForegroundColorSpan(listView.getResources().getColor(R.color.edx_brand_gray_base)), 0, labelValueSpan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(ResourceUtil.getFormattedString(listView.getContext().getResources(), labelRes, labelKey, labelValueSpan));
    }
    Context context = textView.getContext();
    TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(textView,
            new IconDrawable(context, icon)
                    .sizeRes(context, R.dimen.edx_base)
                    .colorRes(context, R.color.edx_brand_gray_back)
            , null, null, null);
    listView.addHeaderView(textView, new FormOption(labelValue, value), true);
}
 
Example 15
Source File: VoteAnimation.java    From kaif-android with Apache License 2.0 5 votes vote down vote up
public static ValueAnimator voteUpReverseTextColorAnimation(TextView view) {
  Context context = view.getContext();

  return colorChangeAnimation(view,
      context.getResources().getColor(R.color.vote_state_up),
      context.getResources().getColor(R.color.vote_state_empty));
}
 
Example 16
Source File: StyleBuilderImpl.java    From Markdown with MIT License 5 votes vote down vote up
public StyleBuilderImpl(TextView textView, Html.ImageGetter imageGetter) {
    this.textViewWeakReference = new WeakReference<>(textView);
    this.imageGetter = imageGetter;


    Context context = textView.getContext();
    TypedArray a = context.obtainStyledAttributes(null, R.styleable.MarkdownTheme, R.attr.markdownStyle, 0);
    final boolean failed = !a.hasValue(0);
    if (failed) {
        Log.w("Markdown", "Missing markdownStyle in your theme, using hardcoded color.");

        h1_text_color = 0xdf000000;
        h6_text_color = 0x8a000000;
        quota_color = 0x4037474f;
        quota_text_color = 0x61000000;
        code_text_color = 0xd8000000;
        code_background_color = 0x0c37474f;
        link_color = 0xdc3e7bc9;
        h_under_line_color = 0x1837474f;
    } else {
        h1_text_color = a.getColor(R.styleable.MarkdownTheme_h1TextColor, 0);
        h6_text_color = a.getColor(R.styleable.MarkdownTheme_h6TextColor, 0);
        quota_color = a.getColor(R.styleable.MarkdownTheme_quotaColor, 0);
        quota_text_color = a.getColor(R.styleable.MarkdownTheme_quotaTextColor, 0);
        code_text_color = a.getColor(R.styleable.MarkdownTheme_codeTextColor, 0);
        code_background_color = a.getColor(R.styleable.MarkdownTheme_codeBackgroundColor, 0);
        link_color = a.getColor(R.styleable.MarkdownTheme_linkColor, 0);
        h_under_line_color = a.getColor(R.styleable.MarkdownTheme_underlineColor, 0);
    }

    a.recycle();
}
 
Example 17
Source File: Linkify.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 *  Scans the text of the provided TextView and turns all occurrences of
 *  the link types indicated in the mask into clickable links.  If matches
 *  are found the movement method for the TextView is set to
 *  LinkMovementMethod.
 *
 *  @param text TextView whose text is to be marked-up with links
 *  @param mask Mask to define which kinds of links will be searched.
 *
 *  @return True if at least one link is found and applied.
 */
public static final boolean addLinks(@NonNull TextView text, @LinkifyMask int mask) {
    if (mask == 0) {
        return false;
    }

    final Context context = text.getContext();
    final CharSequence t = text.getText();
    if (t instanceof Spannable) {
        if (addLinks((Spannable) t, mask, context)) {
            addLinkMovementMethod(text);
            return true;
        }

        return false;
    } else {
        SpannableString s = SpannableString.valueOf(t);

        if (addLinks(s, mask, context)) {
            addLinkMovementMethod(text);
            text.setText(s);

            return true;
        }

        return false;
    }
}
 
Example 18
Source File: BindingAdapters.java    From lttrs-android with Apache License 2.0 4 votes vote down vote up
@BindingAdapter("from")
public static void setFrom(final TextView textView, final ThreadOverviewItem.From[] from) {
    final SpannableStringBuilder builder = new SpannableStringBuilder();
    if (from != null) {
        final boolean shorten = from.length > 1;
        for (int i = 0; i < from.length; ++i) {
            final ThreadOverviewItem.From individual = from[i];
            if (builder.length() != 0) {
                builder.append(", ");
            }
            final int start = builder.length();
            if (individual instanceof ThreadOverviewItem.DraftFrom) {
                final Context context = textView.getContext();
                builder.append(context.getString(R.string.draft));
                builder.setSpan(
                        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimary)),
                        start,
                        builder.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                );
            } else if (individual instanceof ThreadOverviewItem.NamedFrom) {
                final ThreadOverviewItem.NamedFrom named = (ThreadOverviewItem.NamedFrom) individual;
                builder.append(shorten ? EmailAddressUtil.shorten(named.name) : named.name);
                if (!named.seen) {
                    builder.setSpan(
                            new StyleSpan(Typeface.BOLD),
                            start,
                            builder.length(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                    );
                }
                if (from.length > 3) {
                    if (i < from.length - 3) {
                        builder.append(" … "); //TODO small?
                        i = from.length - 3;
                    }
                }
            } else {
                throw new IllegalStateException(
                        String.format("Unable to render from type %s", individual.getClass().getName())
                );
            }
        }
    }
    textView.setText(builder);
}
 
Example 19
Source File: BetterLinkMovementExtended.java    From mvvm-template with GNU General Public License v3.0 4 votes vote down vote up
private static BetterLinkMovementExtended linkify(int linkifyMask, TextView textView) {
    BetterLinkMovementExtended movementMethod = new BetterLinkMovementExtended(textView.getContext());
    addLinks(linkifyMask, movementMethod, textView);
    return movementMethod;
}
 
Example 20
Source File: HtmlAssetsImageGetter.java    From html-textview with Apache License 2.0 4 votes vote down vote up
public HtmlAssetsImageGetter(TextView textView) {
    this.context = textView.getContext();
}