Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the size of buttons in percentage

Please have a look at the following code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HomeScreen" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*"
        android:layout_alignParentTop="true" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

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

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

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

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

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

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

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

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

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

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

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

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

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

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

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

        </TableRow>
    </TableLayout>

</RelativeLayout>

This generates the following output.

enter image description here

But this is not what I need. I need these 12 buttons to fill the entire screen, keep equal spaces between each other and to be equal in size as well. I don't need to set a fixed size lie hight=100, width=100, instead, it should adjust the size according to the device screen size. That means, each button could have 8.3% space in screen (100/12 = 8.3).

I tried this by setting the height of each button to 0dp and adding the android:layout_weight = 0.083. But this didn't work because no button were displayed after this.

How can I do this?

like image 535
PeakGen Avatar asked Feb 15 '23 23:02

PeakGen


1 Answers

You cant have a dynamic layout which you want inside the xml..

In your activity/fragment where you load the layout.. you have to set the tableRow height yourself depending on the screen size

To get screensize you can use

    DisplayMetrics metrics = new DisplayMetrics();
    Activity aActivity = (Activity)  container.getContext();
    aActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

    screen_Width = metrics.widthPixels;
    screen_Height = metrics.heightPixels;

Now you can set each table row width to be totalHeight/NoOFRows - padding.

        tableRow.setMinimumHeight(calculatedHeight);

similar calculation can be done to width to make them properly fill and maintain same size

like image 144
Shubhank Avatar answered Feb 26 '23 04:02

Shubhank