I have an EditText in Android. It has long content and it scrolls into pages
How can I find the real height of content, not just the visible area?
I tried this, but it just gives height of visible area:
EditText editor=(EditText)findViewById(R.id.ed1);
.
.
.
double H=editor.getHeight();
not H is 1150, not 10000 or something like that which is real height of content.
This is what I do:
EditText tv = ...;
if (Build.VERSION.SDK_INT >= 16) {
totalHeight = Math.round((tv.getLineCount() * (tv.getLineHeight() + tv.getLineSpacingExtra()) *
tv.getLineSpacingMultiplier())) + tv.getCompoundPaddingTop() + tv.getCompoundPaddingBottom();
} else {
totalHeight = tv.getLineCount() * tv.getLineHeight() + tv.getCompoundPaddingTop() + tv.getCompoundPaddingBottom();
}
You can do this in following steps
count the lines in the edit text get help from here
after that calculate the height(includeing top padding) of single line then simply multiply that number to get the real height of the text inside the editText
It was enough for me to call EditText.getLayout().getHeight();
I've just debugged an app where I need this functionality too:
int contentHeight = view.getTextArea().getLayout().getHeight();
int visibleAreaHeight = view.getTextArea().getHeight();
The values are:
contentHeight = 7531
visibleAreaHeight = 1408
Related
i am new to Android Studio . I want to increase or decrease textView's line spacing on click. currently i am using this one and it works but i want when user click + than line spacing increased and decreased for - .
here is my code.
textView.setLineSpacing(0,1.1f);
i have tried this but not works
private int textSpace = (int) 1.0f;
private int diff = (int) 0.1f;
and than this
textSize = textSpace+diff;
textView.setLineSpacing(0,textSize);
same for minus, but its not working , please help
Here is a checked example. I hope everything is well described
// Here put ids of your views
TextView textView = findViewById(R.id.textView);
Button plusButton = findViewById(R.id.plus);
Button minusButton = findViewById(R.id.minus);
// The multiplier may be unchanged
final float multiplier = textView.getLineSpacingMultiplier();
// Set the appropriate offset
final float offset = 1f;
plusButton.setOnClickListener(view -> textView.setLineSpacing(textView.getLineSpacingExtra() + offset, multiplier));
minusButton.setOnClickListener(view -> textView.setLineSpacing(textView.getLineSpacingExtra() - offset, multiplier));
I suppose you want to achieve something like this:
private int textSize = 20;
button.setOnClickListerner {
textSize += diff;
textView.setLineSpacing(0, textSize);
}
I have a Card widget with TextFormField as a child. I want to increase the height of the card each time a user gets to a new line. The card is wrapped in a Container and I have a height variable to determine the height.
How can I detect new line in TextFormField? Also whenever the text wraps to the next line.
I think you need to create a function to check the TextFormField specific line.
I think this an be useful for you "https://stackoverflow.com/questions/45900387/multi-line-textfield-in-flutter"
To update height without pressing enter:
onChanged: (String e) {
int sizeIncreaseConstant = 30; //the fontSize 20 + buffer
int widthOfCharacter = 17; // 85% of fontsize
int newNumLines = ((e.length * widthOfCharacter)/widthOfContainer).truncate();
if( newNumLines != numLines) {
setState(() {
if(newNumLines > numLines)
heightOfContainer = heightOfContainer + sizeIncreaseConstant;
else
heightOfContainer = heightOfContainer - sizeIncreaseConstant;
numLines = newNumLines;
});
}
},
initial values:
int numLines = 0;
double widthOfContainer = 120;
double heightOfContainer = 50;
//fontSize = 20;
For this, you will have to use a font that has equal width for all characters like Monospace. And you will also need to determine the width of the character based on fontSize. It is supposed to be 50-60% of the font size but 85% worked for me.
In Android, how can I know if my edittext field is scrolled to the bottom, i.e. the last line is fully visible?
I tried this, but it seems to be too much code for a simple thing:
EditText et;
int scrollY = et.getScrollY();
int sum = et.getHeight() + scrollY;
Layout layout = et.getLayout();
int lastVisibleLineNumber = layout.getLineForVertical(sum);
int lineBottom = layout.getLineBottom(lastVisibleLineNumber);
int diff = lineBottom + layout.getBottomPadding() + et.getPaddingBottom() + et.getPaddingTop() - sum;
if (diff <= 0) // Scrolled to the bottom
you can use
android:ellipsize="end"
to get bottom of edit text.
You can try
int diff = et.getBottom() - (et.getHeight() + et.getScrollY());
I'm trying to get the exact Y position of a given line of text. I thought I would use i * TextView.getLineHeight() to get the Y position of the ith line, but that doesn't seem to be right.
The reason I think this is wrong is because TextView.getLineHeight() * TextView.getLineCount() != TextView.getHeight() which I assume is due to the same error. The line spacing and line multiplier are both default for this TextView.
What is the proper method to get the y position of the ith line in a TextView?
EDIT ---
It seems even TextView.getLayout().getLineTop(i) isn't correct, although please tell me if I'm overlooking something
private int getTextViewHeight(TextView textView) {
Layout layout = textView.getLayout();
int desired = layout.getLineTop(textView.getLineCount());
int padding = textView.getCompoundPaddingTop() +
textView.getCompoundPaddingBottom();
return desired + padding;
}
This code is helped.Read the TextView SourceCode(about onMeasure) you can know how it worked.
I have a TextView with an OnTouchListener. What I want is the character index the user is pointing to when I get the MotionEvent. Is there any way to get to the underlying font metrics of the TextView?
Have you tried something like this:
Layout layout = this.getLayout();
if (layout != null)
{
int line = layout.getLineForVertical(y);
int offset = layout.getOffsetForHorizontal(line, x);
// At this point, "offset" should be what you want - the character index
}
Hope this helps...
I am not aware of a simple direct way to do this but you should be able to put something together using the Paint object of the TextView via a call to TextView.getPaint()
Once you have the paint object you will have access to the underlying FontMetrices via a call to Paint.getFontMetrics() and have access to other functions like Paint.measureText() Paint.getTextBounds(), and Paint.getTextWidths() for accessing the actual size of the displayed text.
While it generally works I had a few problems with the answer from Tony Blues.
Firstly getOffsetForHorizontal returns an offset even if the x coordinate is way beyond the last character of the line.
Secondly the returned character offset sometimes belongs to the next character, not the character directly underneath the pointer. Apparently the method returns the offset of the nearest cursor position. This may be to the left or to the right of the character depending on what's closer by.
My solution uses getPrimaryHorizontal instead to determine the cursor position of a certain offset and uses binary search to find the offset underneath the pointer's x coordinate.
public static int getCharacterOffset(TextView textView, int x, int y) {
x += textView.getScrollX() - textView.getTotalPaddingLeft();
y += textView.getScrollY() - textView.getTotalPaddingTop();
final Layout layout = textView.getLayout();
final int lineCount = layout.getLineCount();
if (lineCount == 0 || y < layout.getLineTop(0) || y >= layout.getLineBottom(lineCount - 1))
return -1;
final int line = layout.getLineForVertical(y);
if (x < layout.getLineLeft(line) || x >= layout.getLineRight(line))
return -1;
int start = layout.getLineStart(line);
int end = layout.getLineEnd(line);
while (end > start + 1) {
int middle = start + (end - start) / 2;
if (x >= layout.getPrimaryHorizontal(middle)) {
start = middle;
}
else {
end = middle;
}
}
return start;
}
Edit: This updated version works better with unnatural line breaks, when a long word does not fit in a line and gets split somewhere in the middle.
Caveats: In hyphenated texts, clicking on the hyphen at the end of a line return the index of the character next to it. Also this method does not work well with RTL texts.