Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android layout, setting buttons to have the same size

I'm am completely new to programming android apps in eclipse.

With that said, I based myself off code on the internet and and came up with this xml file for my layout. I can not get the four buttons to have the same size, and when I play around with the other parts, the progress bar and texts under it, they go bananas, displaying it off screen.

Could someone review the code and tell me what is gong on? I simply don't understand. Here is a picture.

enter image description here

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/txt_currentDate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:gravity="center"
            android:text="Current Date"
            android:textSize="18dp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/progressBarRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="5dp" >

        <ProgressBar
            android:id="@+id/DailyCalorieProgress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:progress="80" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="5dp" >

        <TextView
            android:id="@+id/textView1"
            android:gravity="center"
            android:text="Calorie Limit : "
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView2"
            android:gravity="center"
            android:text="2000  " >
        </TextView>

        <TextView
            android:id="@+id/textView3"
            android:gravity="center"
            android:text="Calories Left : "
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView4"
            android:gravity="center"
            android:text="552" >
        </TextView>
    </TableRow>

    <TextView
        android:id="@+id/textView5"
        android:gravity="center_horizontal"
        android:text="____________________" >
    </TextView>

    <TableLayout
        android:id="@+id/ButtonLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp"
        android:stretchColumns="0,1" >

        <TableRow
            android:id="@+id/buttonRow1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:paddingTop="10dp" >

        <Button
            android:id="@+id/btn_addFood"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="Add Food"/>

        <Button
            android:id="@+id/btn_addExercise"
            android:layout_width="130dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="Add Exercise"/>

        </TableRow>

        <TableRow
            android:id="@+id/buttonRow2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_recWeight"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"
            android:text="Record Weight" />

        <Button
            android:id="@+id/btn_sgstMeal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"
            android:text="Suggest Meal/Exercise" />

    </TableRow>
    </TableLayout>
</TableLayout>

Edits:

It now almost works.

enter image description here

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/txt_currentDate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:gravity="center"
            android:text="@string/CurrentDate"
            android:textSize="18dp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/progressBarRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="5dp" >

        <ProgressBar
            android:id="@+id/DailyCalorieProgress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:progress="80" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="5dp" >

        <TextView
            android:id="@+id/textView1"
            android:gravity="center"
            android:text="@string/CalorieLimit"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView2"
            android:gravity="center"
            android:text="@string/CalorieLimitData" >
        </TextView>

        <TextView
            android:id="@+id/textView3"
            android:gravity="center"
            android:text="@string/CaloriesLeft"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView4"
            android:gravity="center"
            android:text="@string/CaloriesLeftData" >
        </TextView>
    </TableRow>

    <TextView
        android:id="@+id/textView5"
        android:gravity="center_horizontal"
        android:text="@string/Line" >
    </TextView>



        <TableRow
            android:id="@+id/buttonRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:paddingTop="10dp" >

        <Button
            android:id="@+id/btn_addFood"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="@string/AddFood"/>

        <Button
            android:id="@+id/btn_addExercise"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="@string/AddExercise"/>

        </TableRow>

        <TableRow
            android:id="@+id/buttonRow2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:paddingTop="10dp" >

        <Button
            android:id="@+id/btn_recWeight"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="@string/RecordWeight" />

        <Button
            android:id="@+id/btn_sgstMeal"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="@string/SuggestedMeal_Exercise" />

    </TableRow>
</TableLayout>
like image 633
Andre Viau Avatar asked Dec 22 '22 00:12

Andre Viau


1 Answers

Your layout isn't really "efficient". But anyway..to solve your problem you have to set your TableRow (the ones that contain your buttons) to

android:layout_width="fill_parent"
android:layout_height="wrap_content"

and all of your buttons to

android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"

0dip does not make anything. But because of the weight param android does the work for measuring the actual size anyway so you do not need to provide a "relevant" value

like image 105
207 Avatar answered Dec 27 '22 01:12

207