2

I have a data entry type activity and im using a linear layout to space out evenly the sets of textviews and edittexts. Then I have a scroll view that is supposed to make it so the user can scroll while the softkeyboard is up. If I use android:fillViewport the linearlayout works properly and fills the screen and spreads each item out evenly but then when the keyboard comes up and stops each item being spread out evenly. If i use android:windowSoftInputMode="stateVisible|adjustPan" then the linearlayout remains spread out but the scroll view doesn't work anymore (from all the unsolved posts on this i don't think you can have a working scroll view with adjustPan)

is there any way to have a linearlayout inside a scrollview, with items spread out evenly and still work while the softkeyboard is up with out changing the linearlayout?

     <?xml version="1.0" encoding="utf-8"?>
          <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
          android:fillViewport
           >


         <LinearLayout 
           android:gravity="left" 
           android:orientation="vertical" 
           android:id="@id/tab2" 
           android:paddingLeft="40.0dip"
           android:paddingTop="0.0dip" 
           android:paddingRight="40.0dip" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content"
          >



        <LinearLayout 
              android:orientation="vertical"  
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:layout_weight="1.0">
         <TextView 
              android:id="@id/textViewBrand" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="@string/brand" />

        <AutoCompleteTextView
            android:id="@id/editTextBrand"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >

        </AutoCompleteTextView>
        </LinearLayout>

  ...more linearlayouts with textview and edittext to be spaced out evenly

4 Answers 4

1

set the property android:fillViewport="true" in your scrollview it will work

0

Maybe the ScrollView isn't working because your

android:layout_height attribute

is defined to match_parent. You have to put

wrap_content

1
  • thanks for your answer but if I have layout_height set to wrap_content then all items inside the linearlayout are not spread out across the page but just stacked ontop of each other, and scroll view also doesn't work with adjustPan set
    – Thomas
    Aug 6, 2013 at 23:10
0
Follow This Code, I hope it resolves your Problem

<?xml version="1.0" encoding="utf-8"?>

<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    //Your Main Layout

    <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical"     
     android:weightSum="100">

    // First Sub Layout Under Main Layout
       <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" 
        android:layout_weight="10"
        android:weightSum="100" >

           <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="TextView" 
            android:layout_weight="70" />

           <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="30" />

        </LinearLayout>// Finishing First Sub layout 

// Second Sub Layout Under Main Layout
       <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" 
        android:layout_weight="10"
        android:weightSum="100" >

           <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="TextView" 
            android:layout_weight="70" />

           <EditText
            android:id="@+id/editText2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="30" />

        </LinearLayout>// Finishing Second Sub layout 

similarly for 3rd,4rth,5th sub layouts and so on........

</LinearLayout> // Finishing Main Layout
</ScrollView>   // Finishing ScrollView 
1
  • hey thanks for your answer but this just has all the items stacked at the top
    – Thomas
    Aug 6, 2013 at 23:22
0

you can go over the child views of the LinearLayout and set their size to a fixed size yourself in code using a ViewTreeObserver.OnGlobalLayoutListener. the following code sets the sizes to not change, so you can use the android:fillViewport option with weights, and then essentially remove the weights, but keep the calculated sizes.

I used this code with some modifications to also make sure child views are distributed evenly in a LinearLayout that is inside a ScrollView, where the LinearLayout is too big for the android:fillViewport option to get the weights to actually work. this was done by doing 2 iterations over the children. first getting the max view's height, and then setting all child views to that max height.

private void distributeViewsEvenlyInLinearLayout() {
    ViewTreeObserver observer = linearParentView.getViewTreeObserver();
    final ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {

        @SuppressWarnings("deprecation")
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                linearParentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                linearParentView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            for (int i = 0; i < linearParentView.getChildCount(); i++) {
                View child = linearParentView.getChildAt(i);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, child.getHeight());
                child.setLayoutParams(layoutParams);
            }
        }
    };
    observer.addOnGlobalLayoutListener(globalLayoutListener);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.