Reputation: 557
I wanted to post an image to describe this problem, but apparently I don't have enough of a reputation with stackoverflow yet, so I'm going to try to describe what I am trying to accomplish.
I have a TableLayout with 5 columns. They consist of a company name, a job number, a last name, a graphic vertical divider, and a right arrow button. I would like the divider and right arrow button to be right justified against the right side of the screen. I would like column 2 (the job number) to expand large enough to hold the entire number without wrapping. I would like columns 1 (company name) and 3 (last name) to fill in the rest of the table and use ellipses if they are too big (no text wrapping). My table is built programmatically.
Currently, whenever the company name gets too long, the divider and right arrow get pushed off the right side of the screen.
Here is my layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/searchResultsFoundTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:text="@string/searchResultsLabel"/>
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:id="@+id/searchResultsTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:stretchColumns="1"
android:shrinkColumns="0,2" />
</ScrollView>
</LinearLayout>
Here is my code to build the table:
TableLayout table = (TableLayout)findViewById(R.id.searchResultsTableLayout);
for (JobBase job : jobList) {
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
row.setPadding(5, 10, 5, 20);
table.addView(row);
TextView customerNameTextView = new TextView(this);
customerNameTextView.setText(job.getCustomerName());
customerNameTextView.setEllipsize(TextUtils.TruncateAt.END);
customerNameTextView.setTextSize(FONT_SIZE);
customerNameTextView.setPadding(10, 5, 10, 0);
row.addView(customerNameTextView);
TextView jobNumberTextView = new TextView(this);
jobNumberTextView.setText(job.getJobNumber());
jobNumberTextView.setTextSize(FONT_SIZE);
jobNumberTextView.setPadding(10, 5, 10, 0);
row.addView(jobNumberTextView);
TextView crewLeaderTextView = new TextView(this);
crewLeaderTextView.setText(job.getCrewLeader().getLastName());
crewLeaderTextView.setEllipsize(TextUtils.TruncateAt.END);
crewLeaderTextView.setTextSize(FONT_SIZE);
crewLeaderTextView.setPadding(10, 5, 10, 0);
row.addView(crewLeaderTextView);
ImageView dividerImageView = new ImageView(this);
dividerImageView.setImageDrawable(getResources().getDrawable(R.drawable.btn_med_hl_div_white));
dividerImageView.setPadding(10, 10, 10, 0);
row.addView(dividerImageView);
Button rightArrowButton = new Button(this);
rightArrowButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.right_arrow_button));
rightArrowButton.setPadding(10, 0, 10, 0);
rightArrowButton.setOnClickListener(new RowSelectedAction(row));
row.addView(rightArrowButton);
row.setOnClickListener(new RowSelectedAction(row));
}
Any help would be much appreciated.
Upvotes: 1
Views: 582
Reputation: 557
I was able to fix this by adding a weight through LayoutParams to the first column:
TextView customerNameTextView = new TextView(this);
customerNameTextView.setText(job.getCustomerName());
// --- Added this statement
customerNameTextView.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
// ---
customerNameTextView.setEllipsize(TextUtils.TruncateAt.END);
customerNameTextView.setTextSize(FONT_SIZE);
customerNameTextView.setPadding(10, 5, 10, 0);
row.addView(customerNameTextView);
The first column now expands and contracts and the divider and button are always right justified. Sometimes the customer name is ellipsized and other times the name wraps, but that doesn't matter to me as long as the column sizes correctly.
Upvotes: 1