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

The following are Jave code examples for showing how to use getContext() of the android.widget.TextView class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: yyox   File: CustomTextView.java   View Source Code Vote up 6 votes
/**
 * 解析机器人对话link
 *
 * @param textView
 */
private static void dealAILink(TextView textView, String type) {
    CharSequence charSequence = textView.getText();
    if (charSequence instanceof Spannable) {
        textView.setText("");
        Spannable s = (Spannable) charSequence;
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span : spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            String clickContent = s.subSequence(start, end).toString();
            AIURLSpan myURLSpan = new AIURLSpan(span.getURL(), type, clickContent, textView.getContext());
            s.setSpan(myURLSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        textView.append(s);
    }
}
 
Example 2
Project: yyox   File: CustomTextView.java   View Source Code Vote up 6 votes
/**
 * 解析富文本link
 *
 * @param textView
 */
private static void dealCustomLink(TextView textView) {
    CharSequence charSequence = textView.getText();
    if (charSequence instanceof Spannable) {
        textView.setText("");
        Spannable s = (Spannable) charSequence;
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span : spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            CustomClickSpan myURLSpan = new CustomClickSpan(textView.getContext(), span.getURL());
            s.setSpan(myURLSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        textView.append(s);
    }
}
 
Example 3
Project: Aequorea   File: AbstractImageLoader.java   View Source Code Vote up 6 votes
private boolean activityIsAlive() {
    TextView textView = textViewWeakReference.get();
    if (textView == null) {
        return false;
    }
    Context context = textView.getContext();
    if (context == null) {
        return false;
    }
    if (context instanceof TintContextWrapper) {
        context = ((TintContextWrapper) context).getBaseContext();
    }
    if (context instanceof Activity) {
        if (((Activity) context).isFinishing()) {
            return false;
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && ((Activity) context).isDestroyed()) {
                return false;
            }
        }
    }
    return true;
}
 
Example 4
Project: WeChatDemo   File: EmojiUtil.java   View Source Code Vote up 6 votes
/** 显示文本和表情 */
public static void setText(TextView textView, String text) {
    Context context = textView.getContext();
    Resources resources = context.getResources();
    SpannableString ss = new SpannableString(text);

    // 正则表达式: [高兴]
    Pattern p = Pattern.compile("\\[([A-Za-z\u4E00-\u9FA5]+)\\]");
    Matcher matcher = p.matcher(ss);
    while (matcher.find()) {
        // 匹配到一个表情字符串
        String emoji = matcher.group();
        // 过滤非表情符,比如: [xxx]
        if (EMOJI_DATAS.containsKey(emoji)) {   // 是表情才处理
            // System.out.println("----------" + emoji);
            // 指定了一张图片
            Bitmap bitmap = BitmapFactory.decodeResource(resources, EMOJI_DATAS.get(emoji));
            bitmap = Global.createBitmap(bitmap, Global.dp2px(20));     // 图片的宽高为20dp
            ImageSpan span = new ImageSpan(context, bitmap, ImageSpan.ALIGN_BOTTOM);
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(span, start, end, 0);
        }
    }
    textView.setText(ss);
}
 
Example 5
Project: DarkCalculator   File: AutofitHelper.java   View Source Code Vote up 5 votes
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 6
Project: AutoResizeEditText   File: AutofitHelper.java   View Source Code Vote up 5 votes
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 7
Project: LuaViewPlayground   File: AutofitHelper.java   View Source Code Vote up 5 votes
/**
 * 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 8
Project: android-autofittextview   File: AutofitHelper.java   View Source Code Vote up 5 votes
/**
 * 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 9
Project: LuaViewPlayground   File: AutofitHelper.java   View Source Code Vote up 5 votes
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 10
Project: LuaViewPlayground   File: TextUtil.java   View Source Code Vote up 5 votes
/**
 * 使文字大小适应frame
 *
 * @param view
 */
public static void adjustTextSize(TextView view) {
    final Context context = view.getContext();
    float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
    TextPaint paint = new TextPaint();
    int maxLines = getMaxLines(view);
    float minTextSize = scaledDensity * DEFAULT_MIN_TEXT_SIZE;
    float maxTextSize = view.getTextSize();
    float precision = DEFAULT_PRECISION;
    adjustTextSize(view, paint, minTextSize, maxTextSize, maxLines, precision);
}
 
Example 11
Project: DarkCalculator   File: AutofitHelper.java   View Source Code Vote up 5 votes
/**
 * 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 12
Project: android-EmojiCompat   File: MainActivity.java   View Source Code Vote up 5 votes
@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 13
Project: BBSSDK-for-Android   File: Theme0StyleModifier.java   View Source Code Vote up 5 votes
public static void setTextSize(TextView textview, String strdimen) {
	if (textview == null || StringUtils.isEmpty(strdimen)) {
		throw new IllegalArgumentException("Illegal argument to setTextSize()!");
	}
	Context context = textview.getContext();
	int tvTitleSize = context.getResources().getDimensionPixelSize(ResHelper.getResId(context, "dimen", "bbs_title_txt_size"));
	textview.setTextSize(TypedValue.COMPLEX_UNIT_PX, tvTitleSize);
}
 
Example 14
Project: BBSSDK-for-Android   File: Theme0StyleModifier.java   View Source Code Vote up 5 votes
public static void setTextColor(TextView textview, String colorid) {
	if (textview == null || StringUtils.isEmpty(colorid)) {
		throw new IllegalArgumentException("Illegal argument to setTextColor()!");
	}
	Context context = textview.getContext();
	textview.setTextColor(context.getResources().getColor(ResHelper.getColorRes(context, colorid)));
}
 
Example 15
Project: android-autofittextview   File: AutofitHelper.java   View Source Code Vote up 4 votes
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize,
        int maxLines, float precision) {
    if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
        // Don't auto-size since there's no limit on lines.
        return;
    }

    int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
    if (targetWidth <= 0) {
        return;
    }

    CharSequence text = view.getText();
    TransformationMethod method = view.getTransformationMethod();
    if (method != null) {
        text = method.getTransformation(text, view);
    }

    Context context = view.getContext();
    Resources r = Resources.getSystem();
    DisplayMetrics displayMetrics;

    float size = maxTextSize;
    float high = size;
    float low = 0;

    if (context != null) {
        r = context.getResources();
    }
    displayMetrics = r.getDisplayMetrics();

    paint.set(view.getPaint());
    paint.setTextSize(size);

    if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
            || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
        size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
                displayMetrics);
    }

    if (size < minTextSize) {
        size = minTextSize;
    }

    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
 
Example 16
Project: Android-Client   File: TextViewAttrHelper.java   View Source Code Vote up 4 votes
public static void applyAttributes(TextView textView, @Nullable AttributeSet attrs, @StyleableRes int[] styleableIds, @StyleableRes int fontStyableId) {
    Context context = textView.getContext();

    Typeface typeface = Font.MEDIUM.getTypeface(context);

    if (attrs != null) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, styleableIds);

        int fontId = typedArray.getInt(fontStyableId, Font.MEDIUM.getId());
        typeface = Font.fromId(fontId).getTypeface(context);

        typedArray.recycle();
    }

    textView.setTypeface(typeface);
}
 
Example 17
Project: AutoResizeEditText   File: AutofitHelper.java   View Source Code Vote up 4 votes
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize,
                            int maxLines, float precision) {
    if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
        // Don't auto-size since there's no limit on lines.
        return;
    }

    int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
    if (targetWidth <= 0) {
        return;
    }

    CharSequence text = view.getText();
    TransformationMethod method = view.getTransformationMethod();
    if (method != null) {
        text = method.getTransformation(text, view);
    }

    Context context = view.getContext();
    Resources r = Resources.getSystem();
    DisplayMetrics displayMetrics;

    float size = maxTextSize;
    float high = size;
    float low = 0;

    if (context != null) {
        r = context.getResources();
    }
    displayMetrics = r.getDisplayMetrics();

    paint.set(view.getPaint());
    paint.setTextSize(size);

    if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
            || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
        size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
                displayMetrics);
    }

    if (size < minTextSize) {
        size = minTextSize;
    }

    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
 
Example 18
Project: DarkCalculator   File: AutofitHelper.java   View Source Code Vote up 4 votes
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize,
                            int maxLines, float precision) {
    if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
        // Don't auto-size since there's no limit on lines.
        return;
    }

    int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
    if (targetWidth <= 0) {
        return;
    }

    CharSequence text = view.getText();
    TransformationMethod method = view.getTransformationMethod();
    if (method != null) {
        text = method.getTransformation(text, view);
    }

    Context context = view.getContext();
    Resources r = Resources.getSystem();
    DisplayMetrics displayMetrics;

    float size = maxTextSize;
    float high = size;
    float low = 0;

    if (context != null) {
        r = context.getResources();
    }
    displayMetrics = r.getDisplayMetrics();

    paint.set(view.getPaint());
    paint.setTextSize(size);

    if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
            || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
        size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
                displayMetrics);
    }

    if (size < minTextSize) {
        size = minTextSize;
    }

    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
 
Example 19
Project: SDHtmlTextView   File: HtmlAssetsImageGetter.java   View Source Code Vote up 4 votes
public HtmlAssetsImageGetter(TextView textView) {
    this.context = textView.getContext();
}
 
Example 20
Project: PSNine   File: HtmlAssetsImageGetter.java   View Source Code Vote up 4 votes
public HtmlAssetsImageGetter(TextView textView) {
    this.context = textView.getContext();
}