Layout_span in TableLayout on Android

I would like to create a login screen for an Android application. I use TableLayout to get the correct alignment. So, the two lines are composed of TextView and EditText , and I would like to add below the Button that the width is stretched to the screen. So I put the Button in another TableRow , and I added layout_span="2" for the Button , but Button displayed in the first column.

I think this should be correct, but I have to do something wrong in the XML file. Do you have an idea what is wrong?

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".LoginActivity" android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingRight="15dp" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/evUsername" /> <EditText android:id="@+id/username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="text" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingRight="15dp" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/evPassword" /> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="textPassword" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btnLogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_span="2" android:text="@string/btnLogin" /> </TableRow> </TableLayout> 

Thanks in advance!

+7
source share
2 answers

In the end, I wrapped my TableLayout in a LinearLayout , and I added a Button after the TableLayout .

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".LoginActivity" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingRight="15dp" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/evUsername" /> <EditText android:id="@+id/username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="text" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingRight="15dp" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/evPassword" /> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="textPassword" /> </TableRow> </TableLayout> <Button android:id="@+id/btnLogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btnLogin" /> </LinearLayout> 
+3
source

In the xml source file, you can simply add

Android: layout_weight = "1"

in your button

+3
source

All Articles