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

The following examples show how to use android.widget.TextView#getPaddingLeft() . 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 animation-samples 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: 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 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: DetailSharedElementEnterCallback.java    From atlas 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.INSTANCE.hasAll(intent,
            IntentUtil.INSTANCE.getTEXT_COLOR(), IntentUtil.INSTANCE.getFONT_SIZE(), IntentUtil.INSTANCE.getPADDING())) {
        author.setTextColor(intent.getIntExtra(IntentUtil.INSTANCE.getTEXT_COLOR(), Color.BLACK));
        float textSize = intent.getFloatExtra(IntentUtil.INSTANCE.getFONT_SIZE(), targetTextSize);
        author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        Rect padding = intent.getParcelableExtra(IntentUtil.INSTANCE.getPADDING());
        author.setPadding(padding.left, padding.top, padding.right, padding.bottom);
    }
}
 
Example 7
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 8
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 9
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 10
Source File: ChatMsgAdapter.java    From MaterialQQLite with Apache License 2.0 5 votes vote down vote up
private void setBubble(TextView txtContent, ChatMsg chatMsg) {		
	Integer nOldBubble = (Integer)txtContent.getTag();
	if (null == nOldBubble || nOldBubble != chatMsg.m_nBubble) {
		int left = txtContent.getPaddingLeft();
        int right = txtContent.getPaddingRight();
        int top = txtContent.getPaddingTop();
        int bottom = txtContent.getPaddingBottom();

        boolean bIsUser = (ChatMsg.RIGHT == chatMsg.m_nType);
		boolean bUseDefBubble = true;
		
		if (chatMsg.m_nBubble != 0) {				
			//
		}
		
		if (bUseDefBubble) {
			if (bIsUser) {
				txtContent.setBackgroundResource(R.drawable.btn_style7);
				txtContent.setTextColor(0xFFFFFFFF);
                   txtContent.setTextIsSelectable(true);
				txtContent.setLinkTextColor(0xFF0000FF);
			} else {
				txtContent.setBackgroundResource(R.drawable.btn_style6);
				txtContent.setTextColor(0xFF000000);
                   txtContent.setTextIsSelectable(true);
				txtContent.setLinkTextColor(0xFF0000FF);
			}
			txtContent.setTag(0);
		}
		
		txtContent.setPadding(left, top, right, bottom);
	}
}
 
Example 11
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 12
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 13
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 14
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 15
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 16
Source File: InstructionTarget.java    From graphhopper-navigation-android with MIT License 4 votes vote down vote up
private static CharSequence truncateImageSpan(Spannable instructionSpannable, TextView textView) {
  int availableSpace = textView.getWidth() - textView.getPaddingRight() - textView.getPaddingLeft();
  return TextUtils.ellipsize(instructionSpannable, textView.getPaint(), availableSpace, TextUtils.TruncateAt.END);
}
 
Example 17
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 18
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));
}
 
Example 19
Source File: AutofitHelper.java    From kAndroid with Apache License 2.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.
 */
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 20
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));
}