Java Code Examples for android.widget.TextView#getPaddingLeft()
The following examples show how to use
android.widget.TextView#getPaddingLeft() .
These examples are extracted from open source projects.
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 Project: WidgyWidgets File: EditTextWithCustomError.java License: Apache License 2.0 | 6 votes |
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 Project: animation-samples File: DetailSharedElementEnterCallback.java License: Apache License 2.0 | 6 votes |
@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 Project: FirefoxReality File: ViewUtils.java License: Mozilla Public License 2.0 | 6 votes |
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 4
Source Project: atlas File: DetailSharedElementEnterCallback.java License: Apache License 2.0 | 6 votes |
@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 5
Source Project: ViewUtils File: CenterDrawableHelper.java License: Apache License 2.0 | 6 votes |
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 Project: android-instant-apps File: DetailSharedElementEnterCallback.java License: Apache License 2.0 | 6 votes |
@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 7
Source Project: Cotable File: ViewUtils.java License: Apache License 2.0 | 5 votes |
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 8
Source Project: RichText File: RichTextConfig.java License: MIT License | 5 votes |
@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 9
Source Project: qrcode_android File: SystemUtils.java License: GNU Lesser General Public License v3.0 | 5 votes |
/** * 判断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 Project: AOSP-Kayboard-7.1.2 File: SuggestionStripLayoutHelper.java License: Apache License 2.0 | 5 votes |
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 11
Source Project: atlas File: TextResize.java License: Apache License 2.0 | 5 votes |
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 12
Source Project: SprintNBA File: AutoSplitTextView.java License: Apache License 2.0 | 5 votes |
@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 Project: MaterialQQLite File: ChatMsgAdapter.java License: Apache License 2.0 | 5 votes |
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 14
Source Project: android-instant-apps File: TextResize.java License: Apache License 2.0 | 5 votes |
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 15
Source Project: android-login File: TextSizeTransition.java License: MIT License | 5 votes |
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 16
Source Project: Telegram File: ScrollSlidingTextTabStrip.java License: GNU General Public License v2.0 | 4 votes |
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 17
Source Project: kAndroid File: AutofitHelper.java License: Apache License 2.0 | 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
Source Project: Telegram-FOSS File: ScrollSlidingTextTabStrip.java License: GNU General Public License v2.0 | 4 votes |
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 Project: graphhopper-navigation-android File: InstructionTarget.java License: MIT License | 4 votes |
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 20
Source Project: DarkCalculator File: AutofitHelper.java License: MIT License | 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); }