Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to equally span buttons/layouts vertically in android?

I have a layout like in calculator , 4 buttons in each row , with some 5 rows. I created these each row by using LinearLayout. I had managed to fill width of each row completely by using android:layout_weight="1" in each button in LinearLayout. But i don't know how to fill the vertical space in this way. After five rows there's space remaining at the bottom for Normal XDPI screens(Nexus 4)

<LinearLayout
        android:id="@+id/first_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry">

    <Button
            android:id="@+id/seven"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="7"
            android:textColor="@color/Blue_ICS"
            android:textSize="@dimen/button_text_size"
            />

    <Button
            android:id="@+id/eight"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="8"
            android:textColor="@color/Blue_ICS"

            android:textSize="@dimen/button_text_size"/>

    <Button
            android:id="@+id/nine"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="9"

            android:textColor="@color/Blue_ICS"
            android:textSize="@dimen/button_text_size"/>

    <Button
            android:id="@+id/plus"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="+"
            android:textColor="@color/Blue_ICS"
            android:textSize="@dimen/button_text_size"/>
</LinearLayout>

The layout is too long so i share here code of only first row, remaining rows are same too.

like image 504
Vins Avatar asked Jun 04 '13 07:06

Vins


People also ask

Is it possible to evenly distribute buttons across the width of an android LinearLayout?

I suggest you use LinearLayout's weightSum attribute. Adding the tag android:weightSum="3" to your LinearLayout's xml declaration and then android:layout_weight="1" to your Buttons will result in the 3 buttons being evenly distributed.

How do you do a vertical LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How do you put a space between buttons in linear layout?

good answer. Small improvement: use a <Space android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1" > </Space> instead of a <View> to make your objective clearer in your XML.

How do I change the space between buttons in android?

Notice the line that says android:layout_marginTop="10dip" which ensures that you leave a reasonable 10 dip space in between your buttons. Ofcourse, you can increase (or decrease) that space in between your buttons. That's your choice. Hope this answered your question satisfactorily.


1 Answers

You should use a TableLayout, replacing all your LinearLayout for TableRow. The attribute stretchColumns allows you to make all columns with the same width without having to use weight. This way you can use the weight to handle the TableRow height.

In the example below I even added some space (empty LinearLayout) to show the results, like a calculator screen. I hope this is what you're looking for! (You'll have to change the ids)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/outerLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/screenLayout"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:orientation="vertical"></LinearLayout>

<TableLayout
    android:id="@+id/myLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/screenLayout"
    android:stretchColumns="*" >

    <TableRow
        android:id="@+id/first_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/seven"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="7" />

        <Button
            android:id="@+id/eight"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="8" />

        <Button
            android:id="@+id/nine"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="9" />

        <Button
            android:id="@+id/plus"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="+" />
    </TableRow>

    <TableRow
        android:id="@+id/first_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/seven"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="4" />

        <Button
            android:id="@+id/eight"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="5" />

        <Button
            android:id="@+id/nine"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="6" />

        <Button
            android:id="@+id/plus"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="-" />
    </TableRow>

    <TableRow
        android:id="@+id/first_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/seven"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="1" />

        <Button
            android:id="@+id/eight"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="2" />

        <Button
            android:id="@+id/nine"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="3" />

        <Button
            android:id="@+id/plus"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="*" />
    </TableRow>

    <TableRow
        android:id="@+id/first_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/seven"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="0" />

        <Button
            android:id="@+id/eight"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="," />

        <Button
            android:id="@+id/nine"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="/" />

        <Button
            android:id="@+id/plus"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:text="=" />
    </TableRow>
</TableLayout>

</RelativeLayout>

Result:

enter image description here

like image 88
PX Developer Avatar answered Sep 23 '22 11:09

PX Developer