Mike
Mike

Reputation: 6839

Android table xml not centered and partially off screen to the left

I am trying to organize some data in a table in android but I seem to not be able to figure out how to center the whole entire table:

my xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/beerTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="40sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <ImageView android:id="@+id/image"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_margin="10dip"/>

    <TableLayout  

    android:id="@+id/tableLayout1"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:gravity="center_horizontal"
    android:shrinkColumns="*"  
    android:stretchColumns="*"> 

    <TableRow
        android:id="@+id/tableStatTitles"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="5dip" >

        <TextView
            android:id="@+id/abvTitle"
            android:text="ABV"
            android:textStyle = "bold"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            ></TextView>

        <TextView
            android:id="@+id/IBUTitle"
            android:text="IBU"
            android:textStyle = "bold"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            ></TextView>

        <TextView
            android:id="@+id/glassTitle"
            android:text="Glass"
            android:textStyle = "bold"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            ></TextView>


        </TableRow>

    <TableRow
        android:id="@+id/tableStat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="5dip" >

        <TextView
            android:id="@+id/abv"
            android:text=""
            android:textSize="15sp"
            android:layout_width="wrap_content"
            ></TextView>

        <TextView
            android:id="@+id/IBU"
            android:text=""
            android:textSize="15sp"
            android:layout_width="wrap_content"
            ></TextView>

        <TextView
            android:id="@+id/glass"
            android:text=""
            android:textSize="15sp"
            android:layout_width="wrap_content"
            ></TextView>


        </TableRow>

    </TableLayout> 


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

    <TextView
            android:id="@+id/breweryTitle"
            android:text="Brewery: "
            android:textStyle = "bold"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            ></TextView>


    <Button
        android:id="@+id/buttonBrewery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text="" />

    </TableRow>

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

    <TextView
            android:id="@+id/styleTitle"
            android:text="Style: "
            android:textStyle = "bold"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            ></TextView>

    <Button
        android:id="@+id/buttonStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text="" />

    </TableRow>

    <TextView
        android:id="@+id/beerDEscriptionTitle"
        android:textStyle = "bold"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:text="Description:"
        android:padding="5dip"
        ></TextView>
    <TextView
        android:id="@+id/beerDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="15sp"
        android:padding="5dip"

        ></TextView>

</LinearLayout>
</ScrollView>

Upvotes: 0

Views: 236

Answers (2)

Rarw
Rarw

Reputation: 7663

I think your problem is related to the difference between gravity and layout_gravity. gravity only sets the positioning of a view's contents, not where it sits within the parent. Changing the layout_gravity on the table instead of the gravity will center it within the parent view. You can read more about it here

Upvotes: 2

rahultheOne
rahultheOne

Reputation: 154

Try with android:gravity="center_horizontal" in scroll View

Upvotes: 0

Related Questions