I have problem with textviews alignment. I want to align two textviews for the listview item to left and to right. In the Android Studio editor it's ok but when I'm deploying application to the device it's not right aligned. I don't know what is the problem. Below is my layout:
Found the problem. Issue actually was with the listview by mistake I set Listview's layout_width="wrap_content" but should be match_parent. As the mentioned layout is the list's item layout. Thanks everyone.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin"
android:orientation="vertical">
<TextView
android:id="#+id/tv_order_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/tv_order_category_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/tv_order_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/order_tin_ein"
android:gravity="left"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_tin_ein"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"
android:text="Test"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="#string/order_address"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/order_email"
android:gravity="left"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/order_phone"
android:gravity="left"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/order_mobile"
android:gravity="left"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_mobile"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/order_fax"
android:gravity="left"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_fax"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/order_created_date"
android:gravity="left"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_created_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/order_start_working_date"
android:gravity="left"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_start_working_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/order_end_date"
android:gravity="left"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_end_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/order_cost"
android:gravity="left"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_cost"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/order_vat"
android:gravity="left"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_order_vat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</TableRow>
</TableLayout>
I think your code it's fine, and this one will improve for your code better.
Just using gravity:right if you wanna put it on the right.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="asd"
android:gravity="left"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_order_tin_ein"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Test" />
</LinearLayout>
Try this :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Text1"
android:layout_weight="1"/>
<TextView
android:text="Text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Related
I am trying to add a table inside a linear layout. Inside my linear layout there is textview, spinner. When I try to add a table layout it doesn't align correctly. Below is my code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cardLayout"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Product"
android:gravity="center"
android:textStyle="bold"
android:textSize="18sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:layout_margin="10sp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<Spinner
android:id="#+id/product_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
>
<TableRow android:background="#0079D6" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Product Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sale" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
Output
Expected Output
I must be missing something that I don't know. How can I align them equally just like in the above image
Any help would be highly appreciated.
Try using below code:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Product"
android:gravity="center"
android:textStyle="bold"
android:textSize="18sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<Spinner
android:id="#+id/product_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_gravity="center"
>
<TableRow android:background="#0079D6" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Product Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sale" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
as you can see, they arranged in a table layout, i set their layout weight to 1 and their weight sum to 4, at first they are all equal in width, but that changes when the nunber has a lot of digits, what to do?
i thought using a table layout was more convenient than multiple linear layouts haha
layout:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:weightSum="4">
<TextView
android:id="#+id/textView9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Food"
android:textSize="12sp" />
<TextView
android:id="#+id/textView10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Supplies"
android:textSize="12sp" />
<TextView
android:id="#+id/textView11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Transport"
android:textSize="12sp" />
<TextView
android:id="#+id/textView12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Misc"
android:textSize="12sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="1">
<TextView
android:id="#+id/tvTotalExpenses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:weightSum="4">
<TextView
android:id="#+id/tvFoodExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
<TextView
android:id="#+id/tvSuppliesExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
<TextView
android:id="#+id/tvTransportExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
<TextView
android:id="#+id/tvMiscExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
</TableRow>
</TableLayout>
You are mentioning layout weight but it will not work properly unless you set the width to 0dp so set layout_width to 0dp like this
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:weightSum="4">
<TextView
android:id="#+id/textView9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Food"
android:textSize="12sp" />
<TextView
android:id="#+id/textView10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Supplies"
android:textSize="12sp" />
<TextView
android:id="#+id/textView11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Transport"
android:textSize="12sp" />
<TextView
android:id="#+id/textView12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Misc"
android:textSize="12sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="1">
<TextView
android:id="#+id/tvTotalExpenses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:weightSum="4">
<TextView
android:id="#+id/tvFoodExpense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
<TextView
android:id="#+id/tvSuppliesExpense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5000000"
android:gravity="center"
android:textSize="14sp" />
<TextView
android:id="#+id/tvTransportExpense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="50000"
android:textSize="14sp" />
<TextView
android:id="#+id/tvMiscExpense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
</TableRow>
</TableLayout>
i am new in android studio.
currently i making listview, i want to make layout like this picture :
this is my code now
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="3">
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type"
android:textSize="18dp"
android:fontFamily="sans-serif"
android:textColor="#color/black"
android:id="#+id/tvTipeRequest"
android:width="130dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:fontFamily="sans-serif"
android:id="#+id/tvTanggalRequest"
android:textSize="15dp"
android:width="130dp" />
</TableRow>
</TableLayout>
<TextView
android:layout_width="1dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black"
android:text="Status"
android:textSize="15dp"
android:gravity="end"
android:paddingTop="10dp"
android:fontFamily="sans-serif"
android:layout_marginTop="15dp"
android:id="#+id/tvStatus"
android:layout_column="38" />
</TableRow>
</TableLayout>
i realize that tablerow cant do the rowspan, so it didnt work out.
is there any simple way to do that?
Use this hierarchy:
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="1"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.2"
android:orientation="vertical"
android:layout_height="wrap_content">
<!--ImageView here-->
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.8"
android:orientation="vertical"
android:layout_height="wrap_content">
<!--All textViews here-->
</LinearLayout>
</LinearLayout>
You can have a single relative layout as your view container. That would be more efficient.
Read.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:src="#drawable/circle" />
<TextView
android:id="#+id/text_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/image_view"
android:text="text_1"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/text_view_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_view_1"
android:layout_toRightOf="#+id/image_view"
android:text="text_2"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/text_view_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_view_2"
android:layout_toRightOf="#+id/image_view"
android:text="text_3"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/text_view_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_view_3"
android:layout_toRightOf="#+id/image_view"
android:text="text_4"
android:textColor="#android:color/black" />
</RelativeLayout>
Output:
I am writing code for an activity that has a table that initially contains 2 rows(see screenshot). I would like the text in those 2 rows to be in the center(not the TextView).
However, setting android:gravity = "center" or android:gravity = "center_horizontal" does not affect the text at all.
What am I missing?
Screenshot
activity_game.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="horizontal"
android:padding="16dp"
tools:context=".MainActivity">
<TableLayout
android:id="#+id/tl1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TableRow
android:id="#+id/tr_header_p1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="#+id/player1_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Player 1"
android:textColor="#000000"
android:textSize="24sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/table_cell_bg"
android:gravity="center"
android:text="Guess"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/table_cell_bg"
android:gravity="center"
android:text="Bulls"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Cows"
android:textColor="#000000"
android:textSize="16sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tl2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TableRow
android:id="#+id/tr_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="#+id/player2_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Player 2"
android:textColor="#000000"
android:textSize="24sp" />
</TableRow>
<TableRow
android:id="#+id/tr_header2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/table_cell_bg"
android:gravity="center"
android:text="Guess"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/table_cell_bg"
android:gravity="center"
android:text="Bulls"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Cows"
android:textColor="#000000"
android:textSize="16sp" />
</TableRow>
</TableLayout>
Try this should work,
add android:gravity="center" to your #+id/tr_header_p1 and #+id/tr_header
like,
<TableRow
android:id="#+id/tr_header_p1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp">
and
<TableRow
android:id="#+id/tr_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp">
this might helps you
EDIT 1
or you need to mention the android:layout_weight="1" to your TextView. like
<TextView
android:id="#+id/player2_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="Player 2"
android:textColor="#000000"
android:textSize="24sp" />
Try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="horizontal"
android:padding="16dp"
tools:context=".MainActivity">
<TableLayout
android:id="#+id/tl1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TableRow
android:id="#+id/tr_header_p1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="#+id/player1_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Player 1"
android:textColor="#000000"
android:textSize="24sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/table_cell_bg"
android:gravity="center"
android:text="Guess"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/table_cell_bg"
android:gravity="center"
android:text="Bulls"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Cows"
android:textColor="#000000"
android:textSize="16sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tl2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TableRow
android:id="#+id/tr_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="#+id/player2_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Player 2"
android:textColor="#000000"
android:textSize="24sp" />
</TableRow>
<TableRow
android:id="#+id/tr_header2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/table_cell_bg"
android:gravity="center"
android:text="Guess"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/table_cell_bg"
android:gravity="center"
android:text="Bulls"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Cows"
android:textColor="#000000"
android:textSize="16sp" />
</TableRow>
</TableLayout>
</LinearLayout>
You can try setting layout_width to wrap_content and then set layout_gravity to center.
That align the entire TextView and not the text inside it
What if you used android:layout_gravity from your TextView which is gonna affect the position of the element according its parent? There's a thread explaining the difference between both below
Gravity and layout_gravity on Android
<TableRow
android:id="#+id/tr_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:gravity="center_horizontal"
android:padding="5dp">
<TextView
android:id="#+id/player2_name"
android:layout_width="match_parent"
android:gravity="center"
android:text="Player 2"
android:textColor="#000000"
android:textSize="24sp" />
</TableRow>
This is working for me
try for the TableRow parent of TextView
android:gravity="CENTER_HORIZONTAL"
the default layout of a table it's applied to children nodes, as described in documentation here
This question already has answers here:
How can I avoid "IllegalStateException: Scrollview can host only one direct child"?
(7 answers)
Closed 9 years ago.
I have multiple LinearLayouts with a combined height that easily exceeds a device's screen height. So in order to make my layout scrollable, I tried adding in a ScrollView, but unfortunately I get the following error:
Scrollview can host only one direct child
I did some research, but couldn't really make sense of how to solve it, even after reading a couple of solutions. I tried a few changes, but nothing worked for me so far. Any help?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20dip">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 1"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q1_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 2"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q2_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 3"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q3_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 4"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q4_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 5"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q5_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 6"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q6_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 7"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q7_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 8"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q8_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 9"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q9_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 10"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q10_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 11"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q11_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 12"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q12_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 13"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q13_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 14"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q14_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 15"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q15_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 16"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q16_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 17"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q17_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 18"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q18_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 19"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q19_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Question 20"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/q20_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Score:"
android:paddingBottom="20dip"
android:gravity="center" />
<TextView
android:id="#+id/final_score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0/5"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Wrap all the children inside of another LinearLayout with wrap_content for both the width and the height as well as the vertical orientation.