3

I have a table layout that contain two edit texts and a spinner in each row, I want to center it inside a relative layout but I couldn`t get around it.

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:src="@drawable/SomePhoto" />

<TableLayout 
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="40dp"
    android:clickable="false"
    android:duplicateParentState="false"
    android:focusable="false"
    android:layoutDirection="rtl"
    android:focusableInTouchMode="false"
    android:longClickable="false"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

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

    <EditText
        android:id="@+id/editText_4_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:duplicateParentState="false"
        android:ems="5"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:freezesText="false"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="none"
        android:longClickable="false"
        android:scrollHorizontally="false"
        android:text="Course"
        android:textAllCaps="false" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText_4_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:duplicateParentState="false"
        android:ems="6"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="none"
        android:longClickable="false"
        android:text="credit hours"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/editText_4_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:duplicateParentState="false"
        android:ems="4"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="none"
        android:longClickable="false"
        android:text="Grades" />

</TableRow>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="24.67" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


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

    <EditText
        android:id="@+id/editText_4_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="4"
        android:gravity="center_vertical|center_horizontal"
        android:hint="course_1"
        android:maxLines="1" />

    <EditText
        android:id="@+id/editText_4_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="6"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="number"
        android:text="0" />

    <Spinner
        android:id="@+id/spinner_4_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</TableRow>



<RelativeLayout
    android:id="@+id/tableRow13"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:baselineAligned="true" >



    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:background="@drawable/ResetBtn"
            android:id="@+id/button_4_1"
            android:layout_marginLeft="70dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            /> 
        <Button
        android:background="@drawable/CalculateBtn"
        android:id="@+id/button_4_2"
        android:layout_toRightOf="@+id/button_4_1"
        android:layout_marginLeft="90dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    </RelativeLayout>


</RelativeLayout>
</TableLayout>
</ScrollView>

<com.mopub.mobileads.MoPubView
android:id="@+id/adview"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
/>

</TableLayout>

</RelativeLayout>

Any help on how to get around this issue would be appreciated. Thankx

user3713255
  • 37
  • 1
  • 6

4 Answers4

6

Make these changes to you xml code..i hope it will work

 <RelativeLayout 
    android:id="@+id/Relative_4"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" //This is new line
    >

    <TableLayout 
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_centerInParent="true"//This is new line      
        android:clickable="false"
        android:focusable="false"
        android:layoutDirection="rtl"
        android:focusableInTouchMode="false"
        android:longClickable="false"
        android:gravity="center_horizontal"
        android:orientation="vertical" >
Wasim Ahmed
  • 358
  • 3
  • 19
0

Try to add this line in Table Layout:

 android:layout_centerInParent="true"
Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
Rutvij
  • 493
  • 3
  • 10
0
if you want whole table layout inside relative layout to center then do this :

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

and if you want the content inside tablerow to center then you just add this line to your table row:
android:gravity="center"
Imran Betara
  • 649
  • 3
  • 8
0
 <TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
Areshu
  • 21
  • 2