Reputation: 422
I'm creating TextViews and TableRows programmatically and I'm trying to align one of the TextViews (the one named Status: 1) to the right within the TableRow.
Current result:
Expected result:
Code:
TableRow tr = new TableRow(getContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
ImageView profileImage = new ImageView(getContext());
profileImage.setLayoutParams(new TableRow.LayoutParams(128, 128));
TextView usernameText = new TextView(getContext());
TextView friendStatus = new TextView(getContext());
tr.setGravity(Gravity.CENTER_VERTICAL);
friendStatus.setGravity(Gravity.RIGHT); // It's not pushing the textview to the right?
usernameText.setText(username);
usernameText.setTextColor(Color.parseColor("#000000"));
friendStatus.setText("Status: " + status);
tr.addView(profileImage);
tr.addView(usernameText);
tr.addView(friendStatus);
tableLayout.addView(tr);
The TableLayout, reffered to as tableLayout is hardcoded in XML.
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
<TableLayout
android:id="@+id/tableLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
Upvotes: 0
Views: 31
Reputation: 5241
You have to stretch columns to make TextView full width by android:stretchColumns="2"
<TableLayout
android:id="@+id/tableLayout"
android:orientation="horizontal"
android:stretchColumns="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
Upvotes: 1