Android: Total Height of Scrollview

Android: Total height of ScrollView

A ScrollView always has 1 child. All you need to do is get the height of the child to determine the total height:

int totalHeight = scrollView.getChildAt(0).getHeight();

How to get the scrollview total height = Scrollview.getScrollY() on Android

Maybe change

layout_height="wrap_content"

to

layout_height="match_parent"

Get height of ScrollView hidden part

mScrollView.getScrollY(); returns the scrolled top position of this view which is the height of the "hidden" part at the top.

How to set max height to scrollView?

Because you're using ConstraintLayout you need to add 2 constraints to your CardView

app:layout_constraintTop 

app:layout_constraintBotttom

And you also need to add the following lines

app:layout_constrainedHeight="true"

app:layout_constraintHeight_max="300dp" // Change this to your maximum height

It should look something like this

<androidx.cardview.widget.CardView 
android:layout_width="0dp"
android:layout_height="wrap_content"
app:cardCornerRadius="@dimen/card_radius_10"
app:layout_constrainedHeight="true"
app:layout_constraintHeight_max="300dp" // Change this to your maximum height
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" //Change based on your layout
app:layout_constraintTop_toTopOf="parent"> //Change based on your layout

How to get the height of a (scroll)view including masked area

You can measure the child view of the ScrollView instead of the ScrollView itself:

ScrollView sv = (ScrollView) findViewById(R.id.scrollView_affiche);
View child = sv.getChildAt(0); // or find it by id
int height = child.getHeight();
int width = child.getWidth();

Android Constraint Layout height inside scrollview have no effect

Try this:

<ScrollView android:layout_width="match_parent"
android:layout_height="300dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:background="#FFC0CB"
android:minHeight="500dp"
android:layout_height="500dp" >

<TextView
android:id="@+id/new_realm_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="301dp"
android:text="long text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

It seems like there is a bug in constraint layout or layout height cannot be applied to constraint layout in a scroll view but you can use minimum height attribute in constraint layout.

Is there a method to get the height of the entire 'ScrollView'?

You want the height of the ScrollView's child. Try scrollView.getChildAt(0).getMeasuredHeight().

EDIT

You could possibly try having the View measure itself and give it a measurespec that lets it use as much space as it wants. Something like this:

int width = ... // get screen width;
int height = 65536; // arbitrarily large value
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
view.onMeasure(widthMeasureSpec, heightMeasureSpec);
// now get the height
int measuredHeight = view.getMeasuredHeight();

Full disclosure: I don't know if this will work or what effect it may have in your app. There is a side-effect of calling this method, which is that the view's measured dimensions are set. If it's possible, you might try to create a separate, duplicate view and measure that one; or, after you do this measuring hack, call requestLayout() on the view to schedule another measure & layout of the view tree.



Related Topics



Leave a reply



Submit