How to create buttons with dynamic size in XML?

0

I'm creating a calculator for android just with the same basic functions. For this I am writing the Layout code directly in XML, however I can not make the buttons grow and occupy the screen both when turning the device and the normal I am hours on it .. Well follow the code.

    <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main">

<TextView
    android:id="@+id/visor"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/green"
    android:gravity="right"
    android:text="@string/visor"
    android:textColor="@color/white"
    android:textSize="30dp" />

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow
        android:layout_height="fill_parent">

    <Button
        android:id="@+id/btn7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7" />

    <Button
        android:id="@+id/btn8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8" />

    <Button
        android:id="@+id/btn9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9" />

    <Button
        android:id="@+id/btnDiv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="/" />
    </TableRow>

    <TableRow
        android:layout_height="fill_parent">

        <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6" />

        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5" />

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4" />

        <Button
            android:id="@+id/btnMul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*" />
    </TableRow>

    <TableRow
        android:layout_height="fill_parent">

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <Button
            android:id="@+id/btnSub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-" />
    </TableRow>

    <TableRow
        android:layout_height="fill_parent">

        <Button
            android:id="@+id/btn0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0" />

        <Button
            android:id="@+id/Cls"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear" />

        <Button
            android:id="@+id/btn="
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9" />

        <Button
            android:id="@+id/btnSum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="=" />
    </TableRow>
</TableLayout>

Print the current Layout.

I'dliketoleavesomethinglikethis.

    
asked by anonymous 22.09.2014 / 15:05

1 answer

1

There was a small problem with your Layout . It was setting its TableRow without the layout_width attribute, so it assumed as wrap_content , and was using layout_height with fill_parent , which does not work in that case.

I have made the following adaptations:

  • I changed all fill_parent's to match_parent , because it has been deprecated, does not change anything, but it may not work in newer versions.
  • I gave weights to your lines and left them with dynamic height, using the layout_weight attribute being 1 and layout_height being for each row. So it will split the height 0dp . Thus every height of TableLayout will be divided equally between its lines. If you add more rows, be sure to update weightSum .
  • I've set% s of% occupying all available space for it, using TextView's as layout_width and 0dp being layout_height , and most important match_parent as layout_weight . So it evenly distributes the space between 1 .
  • Beware of TextView's , it will generate error when compiling your resources , use btn= for example.
  • At the end your layout will be:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".Main">
    
        <TextView
            android:id="@+id/visor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="16dp"
            android:paddingTop="16dp"
            android:background="#00ff00"
            android:gravity="right"
            android:text="FOCA"
            android:textColor="@color/white"
            android:textSize="30sp" />
    
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:weightSum="4">
    
            <TableRow
                android:layout_height="0dp"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:weightSum="4">
    
                <Button
                    android:id="@+id/btn7"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="7" />
    
                <Button
                    android:id="@+id/btn8"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="8" />
    
                <Button
                    android:id="@+id/btn9"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="9" />
    
                <Button
                    android:id="@+id/btnDiv"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="/" />
            </TableRow>
    
            <TableRow
                android:layout_height="0dp"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:weightSum="4">
    
                <Button
                    android:id="@+id/btn6"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="6" />
    
                <Button
                    android:id="@+id/btn5"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="5" />
    
                <Button
                    android:id="@+id/btn4"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="4" />
    
                <Button
                    android:id="@+id/btnMul"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="*" />
            </TableRow>
    
            <TableRow
                android:layout_height="0dp"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:weightSum="4">
    
                <Button
                    android:id="@+id/btn3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="3" />
    
                <Button
                    android:id="@+id/btn2"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="2" />
    
                <Button
                    android:id="@+id/btn1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="1" />
    
                <Button
                    android:id="@+id/btnSub"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="-" />
            </TableRow>
    
            <TableRow
                android:layout_height="0dp"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:weightSum="4">
    
                <Button
                    android:id="@+id/btn0"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="0" />
    
                <Button
                    android:id="@+id/Cls"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Clear" />
    
                <Button
                    android:id="@+id/btnEq"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="9" />
    
                <Button
                    android:id="@+id/btnSum"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="=" />
            </TableRow>
        </TableLayout>
    </LinearLayout>
    

    The result should be:

        
    22.09.2014 / 16:22