NAA
NAA

Reputation: 33

How to make TableLayout center in ScrollView for android

I am trying to display table at center in ScrollView but it didn't work. But if I delete ScrollView, the table will be displayed at center. I don't know on which part is my mistake. Can anyone help to find the mistake ?

This is the xml coding:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_rekod"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asiqin.attendance.Rekod"
android:background="#E9F1F2">


<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/button2">


    <TableLayout
        android:id="@+id/table"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:shrinkColumns="1"
        android:divider="?android:attr/dividerHorizontal"
        android:showDividers="middle">

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

This is the java code :

  TableRow rowHeader = new TableRow(context);
    rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0"));
    rowHeader.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT));
    String[] headertext = {"TARIKH","ALASAN","STATUS"};
    for(String c:headertext){
        TextView tv = new TextView(this);
        tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT));
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(15);
        tv.setPadding(5,5,5,5);
        tv.setText(c);
       // tv.setBackgroundDrawable(border1);
        tv.setBackgroundResource(R.drawable.valuecellborder);
        rowHeader.addView(tv);
    }

enter image description here

Upvotes: 0

Views: 560

Answers (1)

ANUJ GUPTA
ANUJ GUPTA

Reputation: 925

There are two ways in which you can achieve what you are trying to achieve

1) Use a relative layout inside the scrollview

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button2">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
<TableLayout
    android:id="@+id/table"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"
    android:shrinkColumns="1"
    android:divider="?android:attr/dividerHorizontal"
    android:showDividers="middle">

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

2) Use android:layout_gravity = "center" in Table layout

<TableLayout
android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:shrinkColumns="1"
android:divider="?android:attr/dividerHorizontal"
android:layout_gravity="center"
android:showDividers="middle">

The problem with your code is that you are using android:layout_centerHorizontal="true" which works when the parent layout is a Relative Layout but it won't work if the parent layout is a ScrollView.

Upvotes: 2

Related Questions