Text width in a textview android - android

I am new to Android.. I have a textview with size 250dp.. I am trying to calculate the text width inside it.. But I could not find any API that returns the actual content width.. Can someone help how it can be done.
Thanks in advance.

You can use the Paint object held by the TextView to measure the width of the text contents:
CharSequence text = textView.getText();
float width = textView.getPaint().measureText(text, 0, text.length());
There is also a version that measures the full bounds (left/right/top/bottom) of the text contents:
Rect bounds = new Rect();
CharSequence text = textView.getText();
textView.getPaint().getTextBounds(text, 0, text.length(), bounds);
//bounds now contains the rect of the actual text string
int width = bounds.getWidth();

The Devunwired answer is completely right.
However, it's useful the reader to know that the text width measure in pixels don't need to be linked with any real content of a TextView or EditText and isn't restricted to the view widht, so one can simulate if a determinated text fits well in the view before set the view with a text.
Besides, if one uses the whole string you don't need to specify a initial index and a final index (exclusive).
So one can write
float width = textView.getPaint().measureText("my favorite fruit is a banana");
It works smoothly

There is no API for this.first you have to find the text size and find lenth of string.finally multipl both you will get width of text in textview.

Related

Android setMaxLines with animation

I'm using a TextView to be viewed in ListView item, when that item is clicked I need that text to completely be viewed instead of just first 2 lines.
What I'm doing now is changing maxLines value from 2 (the initial value) to the maximum integer value.
I need this to be done with expand animation. I already know how to expand it, but I only need to find the new height such that I can simply call the expand method.
Update:
I'm using this code to solve my problem, but the returned full height is smaller than the actual one. I think it's px and dp issue:
public int getFullHeight(TextView tv) {
Context context = tv.getContext();
TextView textView = new TextView(context);
textView.setText(tv.getText().toString());
textView.setTypeface(tv.getTypeface());
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, tv.getTextSize());
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(tv.getLayoutParams().width, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredHeight();
}
Given that tv is the original TextView that's with 2 lines.
If I change COMPLEX_UNIT_PX to COMPLEX_UNIT_DIP then it gets larger than the full size, with a white space at the bottom.
Best solution I can find is this:
Measuring text height to be drawn on Canvas ( Android )
Or here: http://egoco.de/post/19077604048/calculating-the-height-of-text-in-android
What they are doing is, creating a paint object with specified width, placing the text and style to use in it and then geting the boundaries.
Hope that helps

Android Multiple Textviews auto adjust one big text

I am working on Book application.Let me give you all concept
There is one big text and have Multiple Textviews as per length of the Text.
I have to show one text(part of big text) which appear in the screen(No scroll).if other text not visible to the user,than other text should appear in the other textview (Next page).
as i googling , I can't find a way to take length of the text which is appear in the one textview (one page).So I can take other text in the other page
I have attached some image for clear the concepts.
Please suggest me. What can i do for that? Thank in advance
ok there are three solutions
if you calculate screen sizes and use sizes like small medium and large.
and you just divide your data and set in textview.
Get number of lines of text that will fit on the screen than Measure how much text will fit across the TextView. Calculate Total amount of text to fill the TextView is approximately.
private void TextMeasure(String text,
TextView tvl, TextView tvr) {
int linesPerScreen = tvl.getHeight() / (tvl.getLineHeight() + (int) tvl.getLineSpacingExtra());
Paint paint = tvl.getPaint();
int textWidth = paint.breakText(text, 0, text.length(),
true, tvl.getWidth(), null);
int totalText = textWidth * linesPerScreen;
String leftText = text.substring(0, totalText);
String rightText = text.substring(totalText,
text.length());
tvl.setText(leftText);
tvr.setText(rightText);
}
source
probably this question help you out.

How to improve font of a TextButton on Android

I have some squarish TextViews (with different dynamic sizes(I don't know them while I'm developing)) in my Application and want to set their text. But if I say TextView.setText(); the text font is pretty small. If I set TextView.setFontSize sometimes the text is not fully displayed (remember: they have a dynamic size). So my question: how can I give the text the perfect font (using the full space of the button AND being fully displayed) or make the Text squarish itself so that it fills the whole space of the TextView?
Thank You
You may use Paint.getTextBounds() like this:
int textViewWidth = textView.getMeasuredWidth();
float textSize = 1.0;
do {
textSize += 0.5
textView.setTextSize(textSize);
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text, 0, text.length(), rect);
int width = rect.width();
} while (width < textViewWidth);
textView.setTextSize(textSize - 0.5);
Looking over the documentation, it looks like you should be able to use something like this:
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setTextScaleX(tv.getWidth());
This will scale the width of your text to the width of the TextView.
I think you will need to calculate heights of the button and the text you wish to set. APIs you will need are: Paint.getTextBounds() and View.getHeight().

Get the size of a text in TextView

I have a problem placing a textView at specified center's x and y coordinates.
Firstly, I tried to set the text in the textView, and to move the view with the width and the height of the view
from this link.
But it doesn't work, and I'd like to try something else.
I'd like to know if there is a method to get the size which a specified text will take in my textView?
I mean, I know the text and the textSize, how can I get the width and the height my textView will take?
Something like the method (NSString)sizeWithFont; for those who know iPhone dev.
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText().toString(), 0, textView.getText().length(), bounds);
bounds.width() will give you the accurate width of the text in the Text View.
If your textview is called tv
tv.setText("bla");
tv.measure(0, 0); //must call measure!
tv.getMeasuredHeight(); //get height
tv.getMeasuredWidth(); //get width
More on this (updated): How to get width/height of a View
For some reason the solution of Midverse Engineer does not give me always correct results (at least on some Android versions). The solution of Sherif elKhatib works, but has the side effect of changing MeasuredWidth and Height. This could lead to incorrect positioning of the textView.
My solution:
width = textView.getPaint().measureText(text);
metrics = textView.getPaint().getFontMetrics();
height = metrics.bottom - metrics.top;
Using the TextView inner Paing class is not so hot when you have multiple lines of text and different paddings. So stop trying to reinvent the wheel. Use getMeasuredHeight and getMeasuredWidth methods after calling measure(UNSPECIFIED, UNSPECIFIED). Just don't forget to get the new values inside the post, otherwise mostly you'll get a wrong result.
tv.setText(state.s);
tv.measure(UNSPECIFIED,UNSPECIFIED);
tv.post(new Runnable() {
#Override
public void run() {
Log.d("tv","Height = "+tv.getMeasuredHeight());
Log.d("tv","Width = "+tv.getMeasuredWidth());
}
});

How do I determine how much text will fit in a TextView in Android?

I have a layout that looks something like this:
[TextView 1] [TextView 2]
[ TextView 2 spill-over ]
Essentially, I need the contents of TextView 2 to wrap to the next line, but start where TextView 1 starts. I was thinking that if I knew how much text would fit into TextView 2 before it runs out of space on line one, I could take the rest of the text and put it in another TextView below the first two. So I need to measure how much text will fit into a TextView (which can be tricky because as far as I can tell, Android will try to break the text in a TextView at a good location so that it won't break a word in the middle if it can be avoided) or I need a new idea on how to lay this out.
Any help would be greatly appreciated.
Thanks in advance,
groomsy
Unfortunately, Paint.breakText didn't return the exact same result as in was seen in my two-line TextView.
However, this worked
int numChars = textView.getLayout().getLineEnd(1);
(use numberOfLines - 1 as the parameter to it)
Ref
http://developer.android.com/reference/android/text/Layout.html#getLineEnd(int)
Set individual lines of TextView to different width
You can create a Paint object with TextView2's text size and use breakText() to measure how many characters will fit in your TextView2's width.
(This is untested code - might need some slight modifications)
String textToBeSplit = arbitraryText; // Text you want to split between TextViews
float textView2Width = somehowGetItsWidth; // TextView2's width
float myTextSize = textView2.getTextSize();
Paint paint = new Paint();
paint.setTextSize(myTextSize); // Your text size
int numChars = paint.breakText(textToBeSplit, true, float textView2Width, null);
numChars tells you how many characters in textToBeSplit will fit in TextView2's width, enabling you to split it between your views.
You don't need two TextViews in order to do this, you should always use as few views as possible and you can use a spannable in order to have two styles for the same textview.
For example with :
TextView tv = (TextView) findViewById(R.id.textview);
SpannableString text = new SpannableString(myString);
text.setSpan(new TextAppearanceSpan(getContext(), R.style.myStyle), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new TextAppearanceSpan(getContext(), R.style.myNextStyle), 6, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text, TextView.BufferType.SPANNABLE);

Categories

Resources