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

The following examples show how to use android.widget.TextView#getPaddingRight() . 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: EditTextWithCustomError.java    From WidgyWidgets with Apache License 2.0 6 votes vote down vote up
private void chooseSize(PopupWindow pop, CharSequence text, TextView tv) {
	int wid = tv.getPaddingLeft() + tv.getPaddingRight();
	int ht = tv.getPaddingTop() + tv.getPaddingBottom();

	/*
	 * Figure out how big the text would be if we laid it out to the
	 * full width of this view minus the border.
	 */
	int cap = getWidth() - wid;
	if (cap < 0) {
		cap = 200; // We must not be measured yet -- setFrame() will fix it.
	}

	Layout l = new StaticLayout(text, tv.getPaint(), cap,
			Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
	float max = 0;
	for (int i = 0; i < l.getLineCount(); i++) {
		max = Math.max(max, l.getLineWidth(i));
	}

	/*
	 * Now set the popup size to be big enough for the text plus the border.
	 */
	pop.setWidth(wid + (int) Math.ceil(max));
	pop.setHeight(ht + l.getHeight());
}
 
Example 2
Source File: DetailSharedElementEnterCallback.java    From android-instant-apps with Apache License 2.0 6 votes vote down vote up
@Override
public void onSharedElementStart(List<String> sharedElementNames,
                                 List<View> sharedElements,
                                 List<View> sharedElementSnapshots) {
    TextView author = getAuthor();
    targetTextSize = author.getTextSize();
    targetTextColors = author.getTextColors();
    targetPadding = new Rect(author.getPaddingLeft(),
            author.getPaddingTop(),
            author.getPaddingRight(),
            author.getPaddingBottom());
    if (IntentUtil.hasAll(intent,
            IntentUtil.TEXT_COLOR, IntentUtil.FONT_SIZE, IntentUtil.PADDING)) {
        author.setTextColor(intent.getIntExtra(IntentUtil.TEXT_COLOR, Color.BLACK));
        float textSize = intent.getFloatExtra(IntentUtil.FONT_SIZE, targetTextSize);
        author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        Rect padding = intent.getParcelableExtra(IntentUtil.PADDING);
        author.setPadding(padding.left, padding.top, padding.right, padding.bottom);
    }
}
 
Example 3
Source File: SpanUtils.java    From Markwon with Apache License 2.0 6 votes vote down vote up
public static int width(@NonNull Canvas canvas, @NonNull CharSequence cs) {
    // Layout
    // TextView
    // canvas

    if (cs instanceof Spanned) {
        final Spanned spanned = (Spanned) cs;

        // if we are displayed with layout information -> use it
        final Layout layout = TextLayoutSpan.layoutOf(spanned);
        if (layout != null) {
            return layout.getWidth();
        }

        // if we have TextView -> obtain width from it (exclude padding)
        final TextView textView = TextViewSpan.textViewOf(spanned);
        if (textView != null) {
            return textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
        }
    }

    // else just use canvas width
    return canvas.getWidth();
}
 
Example 4
Source File: ViewUtils.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
public static float GetLetterPositionX(@NonNull TextView aView, int aLetterIndex, boolean aClamp) {
    Layout layout = aView.getLayout();
    if (layout == null) {
        return 0;
    }
    float x = layout.getPrimaryHorizontal(aLetterIndex);
    x += aView.getPaddingLeft();
    x -= aView.getScrollX();
    if (aClamp && x > (aView.getMeasuredWidth() - aView.getPaddingRight())) {
        x = aView.getMeasuredWidth() - aView.getPaddingRight();
    }
    if (aClamp && x < aView.getPaddingLeft()) {
        x = aView.getPaddingLeft();
    }
    return x;
}
 
Example 5
Source File: CenterDrawableHelper.java    From ViewUtils with Apache License 2.0 6 votes vote down vote up
private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;
    switch (gravity) {
        case Gravity.RIGHT:
            ratio = -1;
        case Gravity.LEFT:
            total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth() + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
            canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
            break;
        case Gravity.BOTTOM:
            ratio = -1;
        case Gravity.TOP:
            Paint.FontMetrics fontMetrics0 = view.getPaint().getFontMetrics();
            total = fontMetrics0.descent - fontMetrics0.ascent + drawable.getIntrinsicHeight() + drawablePadding + view.getPaddingTop() + view.getPaddingBottom();
            canvas.translate(0, ratio * (view.getHeight() - total) / 2);
            break;
    }
}
 
Example 6
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 7
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 8
Source File: TextSizeTransition.java    From android-login with MIT License 5 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 9
Source File: SystemUtils.java    From QRScanner with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 判断TextView的内容宽度是否超出其可用宽度
 *
 * @param tv
 * @return
 */
public static boolean isOverFlowed(TextView tv, int maxWidth) {
    int availableWidth = maxWidth - tv.getPaddingLeft() - tv.getPaddingRight();
    Paint textViewPaint = tv.getPaint();
    float textWidth = textViewPaint.measureText(tv.getText().toString());
    if (textWidth > availableWidth) {
        return true;
    } else {
        return false;
    }
}
 
Example 10
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 11
Source File: AbstractImageLoader.java    From RichText with MIT License 5 votes vote down vote up
private int getRealWidth() {
    TextView tv = textViewWeakReference.get();
    if (tv == null) {
        return 0;
    }
    return tv.getWidth() - tv.getPaddingRight() - tv.getPaddingLeft();
}
 
Example 12
Source File: AutoSplitTextView.java    From SprintNBA with Apache License 2.0 5 votes vote down vote up
@NonNull
private String autoSplitText(final TextView tv) {
    final String rawText = tv.getText().toString(); //原始文本
    final Paint tvPaint = tv.getPaint(); //paint,包含字体等信息
    final float tvWidth = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight(); //控件可用宽度
    
    //将原始文本按行拆分
    String [] rawTextLines = rawText.replaceAll("\r", "").split("\n");
    StringBuilder sbNewText = new StringBuilder();
    for (String rawTextLine : rawTextLines) {
        if (tvPaint.measureText(rawTextLine) <= tvWidth) {
            //如果整行宽度在控件可用宽度之内,就不处理了
            sbNewText.append(rawTextLine);
        } else {
            //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行
            float lineWidth = 0;
            for (int cnt = 0; cnt != rawTextLine.length(); ++cnt) {
                char ch = rawTextLine.charAt(cnt);
                lineWidth += tvPaint.measureText(String.valueOf(ch));
                if (lineWidth <= tvWidth) {
                    sbNewText.append(ch);
                } else {
                    sbNewText.append("\n");
                    lineWidth = 0;
                    --cnt;
                }
            }
        }
        sbNewText.append("\n");
    }
    
    //把结尾多余的\n去掉
    if (!rawText.endsWith("\n")) {
        sbNewText.deleteCharAt(sbNewText.length() - 1);
    }
    
    return sbNewText.toString();
}
 
Example 13
Source File: TextResize.java    From atlas 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 14
Source File: TextResize.java    From atlas with Apache License 2.0 5 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 15
Source File: SystemUtils.java    From qrcode_android with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 判断TextView的内容宽度是否超出其可用宽度
 *
 * @param tv
 * @return
 */
public static boolean isOverFlowed(TextView tv, int maxWidth) {
    int availableWidth = maxWidth - tv.getPaddingLeft() - tv.getPaddingRight();
    Paint textViewPaint = tv.getPaint();
    float textWidth = textViewPaint.measureText(tv.getText().toString());
    if (textWidth > availableWidth) {
        return true;
    } else {
        return false;
    }
}
 
Example 16
Source File: ViewUtils.java    From Cotable with Apache License 2.0 5 votes vote down vote up
public static float a(TextView tv) {
    float f = 0.0F;
    if (tv != null) {
        float f1 = 0.0f;
        CharSequence text = tv.getText();
        if (!TextUtils.isEmpty(text)) {
            f1 = tv.getPaint().measureText(text.toString());
        }
        int i = tv.getPaddingLeft();
        int j = tv.getPaddingRight();
        f = f1 + (float) (j + i);
    }
    return f;
}
 
Example 17
Source File: SuggestionStripLayoutHelper.java    From Indic-Keyboard 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 18
Source File: RichTextConfig.java    From RichText with MIT License 5 votes vote down vote up
@Override
public void dispatchMessage(Message msg) {
    if (msg.what == SET_BOUNDS) {
        //noinspection unchecked
        Pair<Drawable, TextView> pair = (Pair<Drawable, TextView>) msg.obj;
        Drawable drawable = pair.first;
        TextView textView = pair.second;
        int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
        drawable.setBounds(0, 0, width, width / 2);
    }
}
 
Example 19
Source File: ScrollSlidingTextTabStrip.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public void addTextTab(final int id, CharSequence text) {
    int position = tabCount++;
    if (position == 0 && selectedTabId == -1) {
        selectedTabId = id;
    }
    positionToId.put(position, id);
    idToPosition.put(id, position);
    if (selectedTabId != -1 && selectedTabId == id) {
        currentPosition = position;
        prevLayoutWidth = 0;
    }
    TextView tab = new TextView(getContext());
    tab.setWillNotDraw(false);
    tab.setGravity(Gravity.CENTER);
    tab.setText(text);
    tab.setBackgroundDrawable(Theme.createSelectorDrawable(Theme.getColor(selectorColorKey), 3));
    tab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    tab.setSingleLine(true);
    tab.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    tab.setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);
    tab.setOnClickListener(v -> {
        int position1 = tabsContainer.indexOfChild(v);
        if (position1 < 0) {
            return;
        }
        if (position1 == currentPosition && delegate != null) {
            delegate.onSamePageSelected();
            return;
        }
        boolean scrollingForward = currentPosition < position1;
        scrollingToChild = -1;
        previousPosition = currentPosition;
        currentPosition = position1;
        selectedTabId = id;

        if (animatingIndicator) {
            AndroidUtilities.cancelRunOnUIThread(animationRunnable);
            animatingIndicator = false;
        }

        animationTime = 0;
        animatingIndicator = true;
        animateIndicatorStartX = indicatorX;
        animateIndicatorStartWidth = indicatorWidth;

        TextView nextChild = (TextView) v;
        animateIndicatorToWidth = getChildWidth(nextChild);
        animateIndicatorToX = nextChild.getLeft() + (nextChild.getMeasuredWidth() - animateIndicatorToWidth) / 2;
        setEnabled(false);

        AndroidUtilities.runOnUIThread(animationRunnable, 16);

        if (delegate != null) {
            delegate.onPageSelected(id, scrollingForward);
        }
        scrollToChild(position1);
    });
    int tabWidth = (int) Math.ceil(tab.getPaint().measureText(text, 0, text.length())) + tab.getPaddingLeft() + tab.getPaddingRight();
    allTextWidth += tabWidth;
    positionToWidth.put(position, tabWidth);
    tabsContainer.addView(tab, LayoutHelper.createLinear(0, LayoutHelper.MATCH_PARENT));
}
 
Example 20
Source File: ScrollSlidingTextTabStrip.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public void addTextTab(final int id, CharSequence text) {
    int position = tabCount++;
    if (position == 0 && selectedTabId == -1) {
        selectedTabId = id;
    }
    positionToId.put(position, id);
    idToPosition.put(id, position);
    if (selectedTabId != -1 && selectedTabId == id) {
        currentPosition = position;
        prevLayoutWidth = 0;
    }
    TextView tab = new TextView(getContext());
    tab.setWillNotDraw(false);
    tab.setGravity(Gravity.CENTER);
    tab.setText(text);
    tab.setBackgroundDrawable(Theme.createSelectorDrawable(Theme.getColor(selectorColorKey), 3));
    tab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    tab.setSingleLine(true);
    tab.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    tab.setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);
    tab.setOnClickListener(v -> {
        int position1 = tabsContainer.indexOfChild(v);
        if (position1 < 0) {
            return;
        }
        if (position1 == currentPosition && delegate != null) {
            delegate.onSamePageSelected();
            return;
        }
        boolean scrollingForward = currentPosition < position1;
        scrollingToChild = -1;
        previousPosition = currentPosition;
        currentPosition = position1;
        selectedTabId = id;

        if (animatingIndicator) {
            AndroidUtilities.cancelRunOnUIThread(animationRunnable);
            animatingIndicator = false;
        }

        animationTime = 0;
        animatingIndicator = true;
        animateIndicatorStartX = indicatorX;
        animateIndicatorStartWidth = indicatorWidth;

        TextView nextChild = (TextView) v;
        animateIndicatorToWidth = getChildWidth(nextChild);
        animateIndicatorToX = nextChild.getLeft() + (nextChild.getMeasuredWidth() - animateIndicatorToWidth) / 2;
        setEnabled(false);

        AndroidUtilities.runOnUIThread(animationRunnable, 16);

        if (delegate != null) {
            delegate.onPageSelected(id, scrollingForward);
        }
        scrollToChild(position1);
    });
    int tabWidth = (int) Math.ceil(tab.getPaint().measureText(text, 0, text.length())) + tab.getPaddingLeft() + tab.getPaddingRight();
    allTextWidth += tabWidth;
    positionToWidth.put(position, tabWidth);
    tabsContainer.addView(tab, LayoutHelper.createLinear(0, LayoutHelper.MATCH_PARENT));
}