Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a child view match the width of a parent scrollview

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?

like image 684
14 revs, 12 users 16% Avatar asked Mar 16 '14 20:03

14 revs, 12 users 16%


People also ask

How do I get scroll view with match parent?

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.

Can we use linear layout in ScrollView?

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.

How many children can a scroll view have?

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.

How many views can you use within a ScrollView?

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.


1 Answers

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 !

like image 164
rupps Avatar answered Sep 21 '22 22:09

rupps