-1

I want to place 9 buttons in my activity with two conditions:

1.If the screen is small the buttons get scroll.

2.If the screen enough big to fit all of them in one go then they get stretched to fill the screen.

I tried but its not working. thanks.

screenshot of layout

<include
    android:id="@+id/toolbar"
    layout="@layout/appbar"></include>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff0fd"
            android:orientation="vertical"
            android:layout_margin="8dp">



            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/First_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Second_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:onClick="onButtonClick"
                android:text="@string/Third_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Forth_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Fifth_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Sixth_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button7"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Seventh_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button8"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Eighth_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button9"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Gate"
                android:textStyle="bold" />

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

sohel yadav
  • 303
  • 4
  • 10

1 Answers1

1

You have to make different layouts for them, by creating folders in your res folder named layout-small , layout-large, as described here: Supporting Multiple Screens

the folder named 'layout' will be used ofcourse. But, when you create a folder named layout-small, that one will be used instead if the screen size is lower than 470dp x 320dp , which is the minimum resolution for layout-normal.

As for the stretching, use the layout weights and weightsum in a linearlayout for the layouts that look like they can fit in the preview.

Remove the weight parameters, and set layout_height to wrap_content, like so:

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fff0fd"
            android:orientation="vertical"
            android:layout_margin="8dp">

        <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/First_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Second_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="onButtonClick"
                android:singleLine="true"
                android:text="@string/Third_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Forth_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Fifth_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Sixth_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Seventh_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Eighth_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button9"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Gate"
                android:textStyle="bold"/>

    </LinearLayout>
</ScrollView>

You can preview different screen resolutions in the editor to determine which to make with or without a scrollview right here:

Multiple screen previews

Grease
  • 1,308
  • 11
  • 12