BujjiDeepu BujjiDeepu - 27 days ago 4
Android Question

How to place a view component in center of a cell in table layout

I am having a table layout of five columns and having check boxes at the last column I want to place the check box at the center of the column in the table layout can any one tell me how to place check box in center at table layout column in android.

This is my xml:

<TableLayout
android:id="@+id/table_layout_manual_mode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:shrinkColumns="5">

<TableRow
android:id="@+id/After_connection_heading_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#673AB7" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="CHECK ON J14"
android:textColor="#FFFFFF"
android:textSize="20dp" />
</TableRow>

<TableRow
android:id="@+id/tableRow1_manual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >

<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="0.35"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/TP_15_Sno"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="1.5"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/TP_15_Description"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />

<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/Test_Point_15"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />

<TextView
android:id="@+id/textView4"
android:layout_width="1dp"
android:layout_height="65dp"


android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/TP_15_Range"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />

<CheckBox
android:id="@+id/cb_1"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:layout_gravity="center"
/>
</TableRow>
</TableLayout>


This is my drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#9e9e9e"/>


</shape>

Answer

Try to add your checkbox into another layout like this

<LinearLayout
    android:background="@drawable/shp_circle"
    android:layout_width="wrap_content"
    android:layout_height="65dp"
    android:gravity="center">

        <CheckBox
            android:id="@+id/cb_1"
            android:layout_width="wrap_content"
            android:layout_height="65dp"
            android:checked="false" />

</LinearLayout>

I have changes your code into my way so just add this into your code. its solve your problem.

Thank You