0

In my App i am trying to create a scroll view that has 3 big buttons that fills the entire screen. so every button's size is 1/3 of the screen. How can I do that? When I am using weights its not working .

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imageView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/header_layout" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/main_btn_call" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/main_btn_unlock" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/main_btn_push" />

        </TableRow>
    </TableLayout>

</ScrollView>
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
roiberg
  • 13,629
  • 12
  • 60
  • 91

3 Answers3

4

The only change is add the following property in your XML:

android:fillViewport="true"

and your scroll view width and height should be match_parent

android:layout_width="match_parent"
android:layout_height="match_parent"
Piyush
  • 1,528
  • 2
  • 24
  • 39
Harshad07
  • 608
  • 7
  • 23
1

You're using TableLayout which is a LinearLayout with orientation set to vertical - This is all fine so far, you using weights on the layout_height attribute.

The problem as I see it is that you're using wrap_content in both your ScrollView and in your TableLayout. Weight values allow your views to expand to fill space available to it but your parent is wrapping the content to 0 + 0 + 0. If you change the hight to match_parent for both parent elements your weight values will have space to expand

Graeme
  • 25,714
  • 24
  • 124
  • 186
1

I meneged to do this with code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ImageView incallImage = (ImageView) findViewById(R.id.incall_image);
    final ImageView unlockImage = (ImageView) findViewById(R.id.unlock_image);
    final ImageView pushImage = (ImageView) findViewById(R.id.push_image);
    final ScrollView scrollViewMain = (ScrollView) findViewById(R.id.scroll_view_main);
    scrollViewMain.post(new Runnable() {
        @Override
        public void run() {
            scrollMainHeight = scrollViewMain.getHeight();
            LinearLayout.LayoutParams layoutParams  = new    
                     LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, scrollMainHeight / 3);
            incallImage.setLayoutParams(layoutParams);
            unlockImage.setLayoutParams(layoutParams);
            pushImage.setLayoutParams(layoutParams);
        }
    });
}

If anyone finds a way to do this in the xml it will be great!, Thanks!

roiberg
  • 13,629
  • 12
  • 60
  • 91