Problem: When I select a file with a long path or name, it forces adjacent columns to disappear.
The layout xml just positions the TableLayout
as a placeholder in the Activity
and Java code is used to create the `TableRows and views as necessary.
What I'm trying to do: A user can click an "Add" button to select a file on their phone to be added as a path and file name to a TableRow
within a TableLayout
. The first column has a button ( - ) to optionally remove the newly added TableRow, and the second column contains the path and file name.
What I have tried: I feel that I have tried everything programmatically adjusting various LayoutParams
and either Button
or TextView
width and height parameter to set a fixed width... at least for the button. But nothing seems to work. When I search here for a programming example, it is always the layout XML code, not the Java.
I'm still kind of learning the Android but I'm stuck on this and thought someone may be able to point me in the right direction. Below are some images and the code that creates the TableRows and views within. Feel kind of stupid I can't figure this out.
Before adding
After adding
Code methods within a singleton class that create the Button
and TextView
views to the TableRow
and TableLayout
. The first method calls to the other two which create the Button ad TextView
public static TableRow setupFilesTableRow(Context context, TableLayout table, String fileID, String fileName, boolean header) {
TableRow row = new TableRow(context);
if(header) {
row.addView(setupFilesAddRowButton(context, table));
row.addView(addRowTextViewToTable(context, fileName, true));
}else{
row.addView(setupDeleteRowButton(context, table));
for(int r=1; r < 2; r++){
row.addView(addRowTextViewToTable(context, fileName, false));
row.setClickable(true);
}
}
return row;
}
public static Button setupDeleteRowButton(Context context, TableLayout table){
Button btnDelete = new Button(context);
TableRow.LayoutParams trLayoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
trLayoutParams.setMargins(3,3,3,3);
btnDelete.setBackgroundColor(Color.WHITE);
btnDelete.setLayoutParams(trLayoutParams);
btnDelete.setText("-");
btnDelete.setTypeface(Typeface.DEFAULT,Typeface.BOLD);
btnDelete.setGravity(Gravity.CENTER);
btnDelete.setPadding(5,5,5,5);
btnDelete.setOnClickListener(v -> {
deleteTableRows(table);
});
return btnDelete;
}
public static TextView addRowTextViewToTable(Context context, String value, boolean bold){
TextView tv;
tv = new TextView(context);
TableRow.LayoutParams trLayoutParams = new TableRow.LayoutParams();
trLayoutParams.setMargins(3,3,3,3);
tv.setText(String.valueOf(value));
if(bold) tv.setTypeface(null, Typeface.BOLD);
tv.setLayoutParams(trLayoutParams);
tv.setTextSize(12);
tv.setGravity(Gravity.CENTER);
tv.setPadding(8,8,8,8);
tv.setBackgroundColor(Color.WHITE);
return tv;
}
After I learned to use the term "dynamic" instead of "programmatic", I was able to find some help, and as a result, resolved my issue. The following link provided the basis for me to infer a solution to my issue. Below the link is my solution in answer to this question.
Inspired source: http://mangoprojects.info/android-2/creating-a-tablelayout-dynamically-in-android/
Solution: I failed to apply LayoutParams
at the declaration of the TableRow
. Once I added these parameters, I could use TableRow.LayoutParams
to help define and control the individual Button
and TextViews
using .weight
and .height
parameters in my particular case. See the updated code and pic of the results.
Visual Result of Code Update
Code Updates to setupFilesTableRow
TableRow row = new TableRow(context);
LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
row.setLayoutParams(ll);
Code Updates to setupDeleteRowButton
(adding a weight and height helped)
Button btnDelete = new Button(context);
TableRow.LayoutParams trLayoutParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT);
trLayoutParams.setMargins(3,3,3,3);
trLayoutParams.weight = 1;
trLayoutParams.height =75;
btnDelete.setLayoutParams(trLayoutParams);
Code Updates to addRowTextViewToTable
*
TextView tv;
tv = new TextView(context);
TableRow.LayoutParams trLayoutParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT);
trLayoutParams.setMargins(3,3,3,3);
trLayoutParams.weight = 5;
tv.setLayoutParams(trLayoutParams);