Chris Mikkelsen
Chris Mikkelsen

Reputation: 4067

How to Left Align TableRow objects?

They currently appear on the center.

enter image description here

Android Java Code

public void populateTable() {
    for(DoctorBean post: beanPostArrayList){
        String result = "";

        TableRow row = new TableRow(getActivity());
        row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        ImageView docImageView = new ImageView(getActivity());
        docImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        docImageView.setPadding(5, 5, 5, 5);
        docImageView.setImageResource(R.drawable.ic_doctor);

        TextView textView = new TextView(getActivity());
        textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        textView.setPadding(5, 5, 5, 5);

        result += "\n" + post.getHeroName();

        textView.setGravity(Gravity.LEFT);
        docImageView.setForegroundGravity(Gravity.LEFT);
        row.setGravity(Gravity.LEFT);

        textView.setText(result);
        row.addView(docImageView);
        row.addView(textView);
        tableLayout.addView(row);

    }
}

Layout Code

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="229dp"
    class="com.google.android.gms.maps.SupportMapFragment" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:shrinkColumns="*"
        android:stretchColumns="*">

    </TableLayout>

</ScrollView></LinearLayout>

So there I want to align the ImageView and the TextView to the left.

Upvotes: 2

Views: 416

Answers (1)

Sreehari
Sreehari

Reputation: 5655

An alternative approach should solve your issue

  1. You don't need to add explicit scroll for a TableView
  2. TableView can be directly added to your main LinearLayout. As you have already specified the height of MapView

Suggested Modified xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainLL"
    android:orientation="vertical">

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="229dp"
        class="com.google.android.gms.maps.SupportMapFragment" />
    <TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    </TableLayout>
    </LinearLayout>

Corresponding code change

         LinearLayout linearLayout=(LinearLayout)view.findViewById(R.id.mainLL);

        //Table Layout parameters
        TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
        TableLayout tableLayout = (TableLayout) view.findViewById(R.id.tableLayout);
        TableRow trHead = new TableRow(context);
        LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        trHead.setLayoutParams(tableRowParams);

        TextView nameHead = new TextView(context);
        nameHead.setText("Content left");
        nameHead.setLayoutParams(textViewParam);
        nameHead.setGravity(Gravity.LEFT);//or Gravity.CENTER according to your requirement
        trHead.addView(nameHead);

        TextView detailHead = new TextView(context);
        detailHead.setText("Content right");
        detailHead.setGravity(Gravity.RIGHT);//or Gravity.CENTER according to your requirement
        detailHead.setLayoutParams(textViewParam);

        trHead.addView(detailHead);
        tableLayout.addView(trHead);

        //add table layout to linear layout

        linearLayout.add(tableLayout);

NOTE:

Many values have been referred from resource files, You can either neglect/replicate those. I have added two textviews. You may need to change according to your requirement

Upvotes: 1

Related Questions