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

The following examples show how to use android.widget.TextView#getWidth() . 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: PictureFragment.java    From Bright with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    for (int i = 0; i < labels.length; ++i) {
        TextView label = labels[i];

        int x = (int) label.getX();
        int y = (int) label.getY();
        int width = label.getWidth();
        int height = label.getHeight();

        Bitmap dest = Bitmap.createBitmap(source, x, y, width, height);
        luminances[i] = bright.brightness(dest);
        dest.recycle();
    }
    return source;
}
 
Example 2
Source File: PoemBuilderActivity.java    From cannonball-android with Apache License 2.0 6 votes vote down vote up
private Integer pointToWordIndex(float x, float y) {
    float startX = 0;
    boolean reachedY = false;
    final int count = words.size();
    // Margin top and bottom between words, see flowlayout
    final int space = getResources().getDimensionPixelSize(R.dimen.word_padding_vertical);

    for (int i = 0; i < count; i++) {
        final TextView word = words.get(i);
        final float wordX = word.getLeft();
        final float wordY = word.getTop();

        if (y > wordY - space && y < (wordY + word.getHeight() + space)) {
            if (x > startX && x < (wordX + Math.round(word.getWidth() / 2))) {
                return i;
            }
            startX = (wordX + (word.getWidth() / 2));
            reachedY = true;
        } else if (reachedY) {
            return i;
        }
    }
    return count;
}
 
Example 3
Source File: CategoryTabStrip.java    From QiQuYing with Apache License 2.0 6 votes vote down vote up
private void calculateIndicatorRect(Rect rect) {
	ViewGroup currentTab = (ViewGroup)tabsContainer.getChildAt(currentPosition);
	TextView category_text = (TextView) currentTab.findViewById(R.id.category_text);
	
	float left = (float) (currentTab.getLeft() + category_text.getLeft());
       float width = ((float) category_text.getWidth()) + left;
       
       if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {
       	ViewGroup nextTab = (ViewGroup)tabsContainer.getChildAt(currentPosition + 1);
		TextView next_category_text = (TextView) nextTab.findViewById(R.id.category_text);
		
		float next_left = (float) (nextTab.getLeft() + next_category_text.getLeft());
		left = left * (1.0f - currentPositionOffset) + next_left * currentPositionOffset;
		width = width * (1.0f - currentPositionOffset) + currentPositionOffset * (((float) next_category_text.getWidth()) + next_left);
       }
       
       rect.set(((int) left) + getPaddingLeft(), getPaddingTop() + currentTab.getTop() + category_text.getTop(), 
			((int) width) + getPaddingLeft(), currentTab.getTop() + getPaddingTop() + category_text.getTop() + category_text.getHeight());

}
 
Example 4
Source File: PoemBuilderActivity.java    From cannonball-android with Apache License 2.0 6 votes vote down vote up
private Integer pointToWordIndex(float x, float y) {
    float startX = 0;
    boolean reachedY = false;
    final int count = words.size();
    // Margin top and bottom between words, see flowlayout
    final int space = getResources().getDimensionPixelSize(R.dimen.word_padding_vertical);

    for (int i = 0; i < count; i++) {
        final TextView word = words.get(i);
        final float wordX = word.getLeft();
        final float wordY = word.getTop();

        if (y > wordY - space && y < (wordY + word.getHeight() + space)) {
            if (x > startX && x < (wordX + Math.round(word.getWidth() / 2))) {
                return i;
            }
            startX = (wordX + (word.getWidth() / 2));
            reachedY = true;
        } else if (reachedY) {
            return i;
        }
    }
    return count;
}
 
Example 5
Source File: TextResize.java    From animation-samples with Apache License 2.0 6 votes vote down vote up
private static Bitmap captureTextBitmap(TextView textView) {
    Drawable background = textView.getBackground();
    textView.setBackground(null);
    int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
    int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
    if (width == 0 || height == 0) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
    textView.draw(canvas);
    textView.setBackground(background);
    return bitmap;
}
 
Example 6
Source File: RegisterActivity.java    From medical-data-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * requests focus on a {@link EditText} allowing to see its title too. It is used to set focus on the
 * first question with error.
 *
 * @param et    field we want to set focus in.
 * @param tv_id id of the {@link TextView} which is the title of et.
 */
private void focusFirstError(EditText et, int tv_id) {
    et.clearFocus(); // requestRectangle does not work properly if et is focused
    et.requestFocus();
    TextView title = (TextView) findViewById(tv_id);
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) title.getLayoutParams();
    Rect rect = new Rect(0, -lp.topMargin, title.getWidth(), title.getHeight());
    title.requestRectangleOnScreen(rect);
}
 
Example 7
Source File: TextResize.java    From android-instant-apps with Apache License 2.0 5 votes vote down vote up
public TextResizeData(TextView textView) {
    this.paddingLeft = textView.getPaddingLeft();
    this.paddingTop = textView.getPaddingTop();
    this.paddingRight = textView.getPaddingRight();
    this.paddingBottom = textView.getPaddingBottom();
    this.width = textView.getWidth();
    this.height = textView.getHeight();
    this.gravity = textView.getGravity();
    this.textColor = textView.getCurrentTextColor();
}
 
Example 8
Source File: ViewUtil.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
public static CharSequence ellipsize(@Nullable CharSequence text, @NonNull TextView view) {
  if (TextUtils.isEmpty(text) || view.getWidth() == 0 || view.getEllipsize() != TruncateAt.END) {
    return text;
  } else {
    return TextUtils.ellipsize(text,
                               view.getPaint(),
                               view.getWidth() - view.getPaddingRight() - view.getPaddingLeft(),
                               TruncateAt.END);
  }
}
 
Example 9
Source File: RichTextConfig.java    From RichText with MIT License 5 votes vote down vote up
@Override
public Drawable getDrawable(ImageHolder holder, RichTextConfig config, TextView textView) {
    ColorDrawable drawable = new ColorDrawable(Color.LTGRAY);
    int width = textView.getWidth() ;
    drawable.setBounds(0, 0, width, width / 2);
    HANDLER.obtainMessage(SET_BOUNDS, Pair.create(drawable, textView)).sendToTarget();
    return drawable;
}
 
Example 10
Source File: ProfileActivity.java    From medical-data-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * requests focus on a {@link EditText} allowing to see its title too. It is used to set focus
 * on the first question with error.
 *
 * @param et    field we want to set focus in.
 * @param tv_id id of the {@link TextView} which is the title of et.
 */
private void focusFirstError(EditText et, int tv_id) {
    et.clearFocus(); // requestRectangle does not work properly if et is already focused
    et.requestFocus();
    TextView title = (TextView) findViewById(tv_id);
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) title.getLayoutParams();
    Rect rect = new Rect(0, -lp.topMargin, title.getWidth(), title.getHeight());
    title.requestRectangleOnScreen(rect);
}
 
Example 11
Source File: ViewUtil.java    From android-dialer with Apache License 2.0 5 votes vote down vote up
public static void resizeText(TextView textView, int originalTextSize, int minTextSize) {
  final Paint paint = textView.getPaint();
  final int width = textView.getWidth();
  if (width == 0) {
    return;
  }
  textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, originalTextSize);
  float ratio = width / paint.measureText(textView.getText().toString());
  if (ratio <= 1.0f) {
    textView.setTextSize(
        TypedValue.COMPLEX_UNIT_PX, Math.max(minTextSize, originalTextSize * ratio));
  }
}
 
Example 12
Source File: SuggestionStripLayoutHelper.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
public void layoutImportantNotice(final View importantNoticeStrip,
        final String importantNoticeTitle) {
    final TextView titleView = (TextView)importantNoticeStrip.findViewById(
            R.id.important_notice_title);
    final int width = titleView.getWidth() - titleView.getPaddingLeft()
            - titleView.getPaddingRight();
    titleView.setTextColor(mColorAutoCorrect);
    titleView.setText(importantNoticeTitle); // TextView.setText() resets text scale x to 1.0.
    final float titleScaleX = getTextScaleX(importantNoticeTitle, width, titleView.getPaint());
    titleView.setTextScaleX(titleScaleX);
}
 
Example 13
Source File: StyleBuilderImpl.java    From Markdown with MIT License 5 votes vote down vote up
private int getTextViewRealWidth() {
    TextView textView = textViewWeakReference.get();
    if (textView != null) {
        return textView.getWidth() - textView.getPaddingRight() - textView.getPaddingLeft();
    }
    return 0;
}
 
Example 14
Source File: SuggestionStripLayoutHelper.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
public void layoutImportantNotice(final View importantNoticeStrip,
        final String importantNoticeTitle) {
    final TextView titleView = importantNoticeStrip.findViewById(
            R.id.important_notice_title);
    final int width = titleView.getWidth() - titleView.getPaddingLeft()
            - titleView.getPaddingRight();
    titleView.setTextColor(mColorAutoCorrect);
    titleView.setText(importantNoticeTitle); // TextView.setText() resets text scale x to 1.0.
    final float titleScaleX = getTextScaleX(importantNoticeTitle, width, titleView.getPaint());
    titleView.setTextScaleX(titleScaleX);
}
 
Example 15
Source File: ImageTarget.java    From RichText with MIT License 5 votes vote down vote up
/**
 * 获取可用宽度
 *
 * @return width
 */
int getRealWidth() {
    TextView tv = textViewWeakReference.get();
    if (tv == null) {
        return 0;
    }
    return tv.getWidth() - tv.getPaddingRight() - tv.getPaddingLeft();
}
 
Example 16
Source File: TextResize.java    From animation-samples with Apache License 2.0 5 votes vote down vote up
public TextResizeData(TextView textView) {
    this.paddingLeft = textView.getPaddingLeft();
    this.paddingTop = textView.getPaddingTop();
    this.paddingRight = textView.getPaddingRight();
    this.paddingBottom = textView.getPaddingBottom();
    this.width = textView.getWidth();
    this.height = textView.getHeight();
    this.gravity = textView.getGravity();
    this.textColor = textView.getCurrentTextColor();
}
 
Example 17
Source File: TextUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
static void adjustTextSize(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
Source File: AutofitHelper.java    From DarkCalculator with MIT License 4 votes vote down vote up
/**
 * 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
Source File: ChatSelectTouchListener.java    From revolution-irc with GNU General Public License v3.0 4 votes vote down vote up
private boolean handleSelection(float x, float y, float rawX, float rawY) {
    View view = mRecyclerView.findChildViewUnder(x, y);
    if (view == null)
        return mSelectionLongPressMode;
    long id = mRecyclerView.getChildItemId(view);
    int index = mRecyclerView.getChildAdapterPosition(view);
    TextView textView = findTextViewIn(view);
    if (textView == null)
        return mSelectionLongPressMode;
    textView.getLocationOnScreen(mTmpLocation);
    float viewX = rawX - mTmpLocation[0];
    float viewY = rawY - mTmpLocation[1];

    float tViewY = Math.min(Math.max(viewY, 0), textView.getHeight() -
            textView.getCompoundPaddingBottom()) - textView.getCompoundPaddingTop();
    float tViewX = Math.min(Math.max(viewX, 0), textView.getWidth() -
            textView.getCompoundPaddingRight()) - textView.getCompoundPaddingLeft();

    mLastTouchTextId = id;
    int line = textView.getLayout().getLineForVertical((int) tViewY);
    mLastTouchTextOffset = textView.getLayout().getOffsetForHorizontal(line, tViewX);
    mLastTouchInText = viewX >= textView.getCompoundPaddingLeft() &&
            viewX <= textView.getWidth() - textView.getCompoundPaddingEnd() &&
            viewY >= textView.getCompoundPaddingTop() &&
            viewY <= textView.getHeight() - textView.getCompoundPaddingBottom() &&
            tViewX <= textView.getLayout().getLineWidth(line);
    if (mSelectionLongPressMode) {
        long sel = TextSelectionHelper.getWordAt(textView.getText(), mLastTouchTextOffset,
                mLastTouchTextOffset + 1);
        int selStart = TextSelectionHelper.unpackTextRangeStart(sel);
        int selEnd = TextSelectionHelper.unpackTextRangeEnd(sel);
        int selLongPressIndex = getItemPosition(mSelectionLongPressId);
        if (index > selLongPressIndex ||
                (index == selLongPressIndex && selEnd >= mSelectionLongPressStart)) {
            setSelection(mSelectionLongPressId, mSelectionLongPressStart,
                    mLastTouchTextId, selEnd);
        } else {
            setSelection(mLastTouchTextId, selStart,
                    mSelectionLongPressId, mSelectionLongPressEnd);
        }
        return true;
    }
    return false;
}
 
Example 20
Source File: TextUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 改变外框大小,适应文字
 * 如果当前文字只有一行,则缩到外框适应文字大小
 * 如果文字有多行,则宽度不变,缩小高度到适应文字大小
 * 文字外框的位置不变
 *
 * @param view
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void adjustSize(TextView view) {
    if (view != null && view.getLayoutParams() != null) {
        /*
        //更好的实现方式,但是ios那边不支持这种方式
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        view.setLayoutParams(layoutParams);*/

        // 废弃的实现方式,使用WRAP_CONTENT最简单,且这种方式算出来的width有bug
        // @Deprecated
        /*int lineCount = Math.min(view.getLineCount(), getMaxLines(view));
        int width, height;
        if (lineCount > 1) {//多行,宽度不变,改变高度
            width = view.getWidth();
            height = view.getLineHeight() * lineCount + view.getPaddingTop() + view.getPaddingBottom();
        } else {//一行,改变宽度&高度
             Paint paint = view.getPaint();
            width = (int) paint.measureText(view.getText().toString()) + view.getPaddingLeft() + view.getPaddingRight();
            height = view.getLineHeight() + view.getPaddingTop() + view.getPaddingBottom();
        }*/

        int lineCount = Math.min(view.getLineCount(), getMaxLines(view));
        float width, height;
        if (lineCount > 1) {//多行,宽度不变,改变高度
            width = view.getWidth();
            height = view.getLineHeight() * lineCount + view.getPaddingTop() + view.getPaddingBottom();
        } else {//一行,改变宽度&高度
            width = view.getPaddingLeft() + Layout.getDesiredWidth(view.getText(), view.getPaint()) + view.getPaddingRight();
            height = view.getLineHeight() + view.getPaddingTop() + view.getPaddingBottom();
        }

        if (view.getLayoutParams() != null) {
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.width = (int) width;
            layoutParams.height = (int) height;
            view.setLayoutParams(layoutParams);
        }
    }
}