AVEbrahimi
AVEbrahimi

Reputation: 19154

Android EditText real content height

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.

Upvotes: 6

Views: 1022

Answers (3)

ka3ak
ka3ak

Reputation: 3191

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

Upvotes: 0

grebulon
grebulon

Reputation: 7972

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();
}

Upvotes: 3

Ali Imran
Ali Imran

Reputation: 9217

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

Upvotes: 0

Related Questions