sarah
sarah

Reputation: 77

Tablelayout showing it in the mid of the screen

enter image description here

I wanted to show the table layout in the middle of the screen adding a background image only for table layout not for the entire screen,below is my activity file ,please let me know how to do this?

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">

    <TableRow>
        <TextView
            android:text="User Name: "
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>

        <EditText
            android:text=""
            android:id="@+id/txtUname"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </EditText>
    </TableRow>


    <TableRow>
        <TextView
            android:text="Password: "
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>


        <EditText
            android:text=""
            android:id="@+id/txtPwd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:password="true">
        </EditText>
    </TableRow>


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


        <Button
            android:text="Login"
            android:id="@+id/btnLogin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </Button>


    </TableRow>


</TableLayout>

Thanks,

Working code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/loginbackmain"
    android:gravity="center">
    <ImageView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher"
        android:id="@+id/userDp"/>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:background="@drawable/loginformback"
        android:stretchColumns="1">

        <TableRow>
            <TextView
                android:text="User Name: "
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>

            <EditText
                android:text=""
                android:id="@+id/txtUname"
                android:layout_width="fill_parent"
                android:paddingLeft="5dp"
                android:layout_height="wrap_content">
            </EditText>
        </TableRow>


        <TableRow>
            <TextView
                android:text="Password: "
                android:id="@+id/TextView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>


            <EditText
                android:text=""
                android:id="@+id/txtPwd"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:password="true">
            </EditText>
        </TableRow>


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


            <Button
                android:text="Login"
                android:id="@+id/btnLogin"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </Button>


        </TableRow>


    </TableLayout>
  </LinearLayout>

Upvotes: 0

Views: 110

Answers (3)

Thinsky
Thinsky

Reputation: 4246

set android:gravity="true" for TableLayout

Upvotes: 0

Dhinakaran Thennarasu
Dhinakaran Thennarasu

Reputation: 3356

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    android:layout_gravity="center"
    android:background="@drawable/yourimagename">

This worked for me!

Upvotes: 0

M. Erfan Mowlaei
M. Erfan Mowlaei

Reputation: 1436

I guess this is what you want. P.S. I added some margin so that it looks better try using margin (and padding for EditTexts).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:gravity="center"
    android:background="#23f3AA"
    android:stretchColumns="1">

    <TableRow>
        <TextView
            android:text="User Name: "
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>

        <EditText
            android:text=""
            android:id="@+id/txtUname"
            android:layout_width="fill_parent"
            android:paddingLeft="5dp"
            android:layout_height="wrap_content">
        </EditText>
    </TableRow>


    <TableRow>
        <TextView
            android:text="Password: "
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>


        <EditText
            android:text=""
            android:id="@+id/txtPwd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:password="true">
        </EditText>
    </TableRow>


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


        <Button
            android:text="Login"
            android:id="@+id/btnLogin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </Button>


    </TableRow>


</TableLayout>

Upvotes: 2

Related Questions