I have a horizontal scrollview with many EditText children. I want each of these children to be the same width of the visible area of the parent scrollview. Is this possible in XML?
Try adding android:fillViewport="true"to your ScrollView To work around this, you need to use the ScrollView attribute called android:fillViewport. When set to true, this attribute causes the scroll view's child to expand to the height of the ScrollView if needed.
Here in this example of Linear Layout inside ScrollView we create a custom layout for user registration form using different views(TextView, EditText etc). After creating different views we enclose them inside Linear Layout and then we enclose the whole layout in ScrollView to make all the element or views scrollable.
A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.
ScrollView is a subclass of FrameLayout , which means that you can place only one View as a child within it; that child contains the entire contents to scroll.
You can do it writing a small helper class:
We are creating a very small class that extends EditText
called FullWidthEditText
like this:
package com.YOURPACKAGENAME;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
public class FullWidthEditText extends EditText {
public FullWidthEditText(Context context) { super(context);}
public FullWidthEditText(Context context, AttributeSet attrs) { super(context, attrs); }
public FullWidthEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View parentScrollView=((View)(getParent().getParent()));
if (parentScrollView!=null) {
// check the container of the container is an HorizontallScrollView
if (parentScrollView instanceof HorizontalScrollView) {
// Yes it is, so change width to HSV's width
widthMeasureSpec=parentScrollView.getMeasuredWidth();
}
}
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
}
Now you have this class created, you can use it in your XML just like a normal EditText:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff0000" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:orientation="horizontal" >
<com.YOURPACKAGENAME.FullWidthEditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/yourEditTextId">
</com.YOURPACKAGENAME.FullWidthEditText>
<com.YOURPACKAGENAME.FullWidthEditText
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</com.YOURPACKAGENAME.FullWidthEditText>
.
.
.
</LinearLayout>
</HorizontalScrollView>
You can also create it programmatically, add handlers, listeners, change text, findViewById ... etc ... just like a normal EditText.
EditText editText=new EditText(context);
FullWidthEditText fullWidthEditText=new FullWidthEditText(context);
Note that this solution works for any other Widget as well. If you need ie. a full-width button, just change extends EditText
for extends Button
and you got it.
You can easily customize the size, the key line is: widthMeasureSpec=parentScrollView.getMeasuredWidth();
so you can ie. make full width minus 10 or 20px, make half width, etc.
Hope it helps !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With