Android: Custom class for setting the max height of a NestedScrollView does not work (there is no scrollbar)
Your MaxHeightNestedScrollView
is incomplete, as it does not specify how to use maxHeight
attribute from xml. Use below modified MaxHeightNestedScrollView
class(Difference are commented out).
MaxHeightNestedScrollView.java
public class MaxHeightNestedScrollView extends NestedScrollView {
private int maxHeight = -1;
public MaxHeightNestedScrollView(@NonNull Context context) {
this(context, null, 0); // Modified changes
}
public MaxHeightNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0); // Modified changes
}
public MaxHeightNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr); // Modified changes
}
// Modified changes
private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr){
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.MaxHeightNestedScrollView, defStyleAttr, 0);
maxHeight =
a.getDimensionPixelSize(R.styleable.MaxHeightNestedScrollView_maxHeight, 0);
a.recycle();
}
public int getMaxHeight() {
return maxHeight;
}
public void setMaxHeight(int maxHeight) {
this.maxHeight = maxHeight;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (maxHeight > 0) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Also to show scrollbar in NestingScrollView just add android:scrollbars="vertical"
attribute to your MaxHeightNestedScrollView
view in xml.
After changes your layout file will look like.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_weight="1"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem
ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum" />
<com.example.testgradle.MaxHeightNestedScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" // Modified changes
app:maxHeight="130dp">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#000000"
android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsumLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum" />
</com.example.testgradle.MaxHeightNestedScrollView>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Hope this help.