I have created a layout using a ScrollView
which has a PercentRelativeLayout
as its child. It doesn't work on Lollipop and older devices but works fine on Marshmallow devices. Please check the code below:
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.percent.PercentRelativeLayout
android:id="@+id/scrollContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_heightPercent="100%"
app:layout_widthPercent="50%">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"
android:text="kkjknadko"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:text="Abcaad"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview2"
android:background="@android:color/holo_red_dark"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview3"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview4"
android:background="@android:color/holo_red_dark"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview5"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview6"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview7"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
</android.support.percent.PercentRelativeLayout>
</ScrollView>
And also I android:fillViewport="true"
, it doesn't show anything in Lollipop and older Android versions.
Unfortunately, the percent layout won't work with ScrollView
before M. The reason for that is that they depend on the size hint being delivered in the measuring step. Before M most layouts would provide size hint 0 when sending unspecified measure spec.
You can try to fix that by creating your own subclass of
ScrollView
and overridingmeasureChild
andmeasureChildWithMargins
(fortunately both are protected) to provide the size hint.
source - plus.google.com.
Can someone help me with creating custom ScrollView
to make it work?
Create one custom
scrollview
and setMeasured Height
andWidth
as you want to it for ExampleCustomScrollView
Use customscrollview in your xml.
hope this will help.
The question as I understand it is: I want these TextViews to be 50% width of parent view.
Way that works for me is:
Then allign the View(s) to that. Either the RelativeLayout or the TextViews. You lose the control (if you ever had it) over the percentage of the RelativeLayout. I tend to go with things like:
Does this help?
Add one Layout after ScrollView because ScrollView can only function with one child layouts.
I encountered the same problem with android K and android L.
You could have used constraint layout instead of percent relative layout but there seems scrolling problem with that too. Even custom Scroll View as suggested in other answers wasn't helpful for me.
Though there's a way.
Convert your percent relative layout to relative layout and set the height of the views programmatically according to your device height.
There seems no problem in scrolling with relative layout. And you can use 'Space' view for margin between TextViews. This is not a very elegant solution but for me it worked.
Java Code:
XML Code: