Home 3 Column TableLayout not centering properly in Android App
Reply: 0

3 Column TableLayout not centering properly in Android App

Macoryx
1#
Macoryx Published in 2018-01-13 02:38:10Z

I have been working on this odd issue for about 6 hours now, and I have searched countlessly to no avail. I am now asking for help. I am currently trying to make a TableLayout with three columns to display texts of different lengths.

Here are some images. Top left corner

Top Right Corner

The TableLayout is centered before I programmatically add rows to it.

public void populate(String name, String description, String grade){
    row = new TableRow(this);


    t1 = new TextView(this);
    t1.setTextColor(getResources().getColor(R.color.colorPrimary));
    t2 = new TextView(this);
    t2.setTextColor(getResources().getColor(R.color.colorPrimary));
    t3 = new TextView(this);
    t3.setTextColor(getResources().getColor(R.color.colorPrimary));

    t1.setText(name);
    t2.setText(description);
    t3.setText(grade);

    t1.setTypeface(null, 1);
    t2.setTypeface(null, 1);
    t3.setTypeface(null, 1);

    t1.setBackground(getResources() .getDrawable(R.drawable.cell_shape));
    t2.setBackground(getResources() .getDrawable(R.drawable.cell_shape));
    t3.setBackground(getResources() .getDrawable(R.drawable.cell_shape));

    t1.setTextSize(15);
    t2.setTextSize(15);
    t3.setTextSize(15);



    t1.setPadding(20, 0, 0, 0);
    t2.setPadding(20, 0, 0, 0);
    t3.setPadding(20, 0, 0, 0);


    /*
    t1.setGravity(1);
    t2.setGravity(1);
    t3.setGravity(1);*/

    row.addView(t1);
    row.addView(t2);
    row.addView(t3);

    table.addView(row, new 
TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, 
TableLayout.LayoutParams.WRAP_CONTENT));

}
//Code for title columns:
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:scrollbars="none"
    app:layout_constraintDimensionRatio="1:1">

    <HorizontalScrollView
        android:id="@+id/horizontalView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dip"
        android:scrollbars="horizontal|vertical">

        <TableLayout
            android:id="@+id/table1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:stretchColumns="*"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TableRow>
                <TextView
                    android:width="90dp"
                    android:background="@drawable/cell_shape"
                    android:gravity="center_horizontal"
                    android:padding="3dip"
                    android:text="Name" />

                <TextView
                    android:width="90dp"
                    android:background="@drawable/cell_shape"
                    android:gravity="center_horizontal"
                    android:padding="3dip"
                    android:text="Description" />

                <TextView
                    android:width="90dp"
                    android:background="@drawable/cell_shape"
                    android:gravity="center_horizontal"
                    android:padding="3dip"
                    android:text="Grade" />
            </TableRow>
        </TableLayout>
    </HorizontalScrollView>
</ScrollView>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298193 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO