Problem: I'm having difficulty finding a way to identify which of two TableLayout objects are being passed to a method.
What I'm trying to do: I'm trying to use Singleton class with list of Static methods that perform common setup functions when creating and or editing a TableLayout
programmatic-ally.
For example, since creating an 'add' Button
within a TableRow
for each TableLayout
is identical except for the button OnClickListener
action, I would like to simply pass the TableLayout object. I then use the TableLayout variable name of that object to identify the correct OnClickListener setup.
In the code example below, the if - else
is an example of what I would like to do. I know there isn't a method name()
... it is my way of demonstrating in the code what I would like to do.
private Button setupAuthorsAddRowButton(Context context, TableLayout table){
Button btnAddRow = new Button(context);
TableRow.LayoutParams trLayoutParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT);
trLayoutParams.setMargins(3,3,3,3);
btnAddRow.setLayoutParams(trLayoutParams);
btnAddRow.setBackgroundColor(Color.WHITE);
btnAddRow.setText("+");
btnAddRow.setTypeface(Typeface.DEFAULT,Typeface.BOLD);
btnAddRow.setGravity(Gravity.CENTER);
btnAddRow.setPadding(5,5,5,5);
if(table.name().equals("tableLayoutAuthors")) // name() is not a real method, just an example
btnAddRow.setOnClickListener(v -> table.addView(setupAuthorsTableRow("", "", "", "", false)));
else if(table.name().equals("tableLayoutFiles"))
btnAddRow.setOnClickListener(v -> table.addView(setupFilesTableRow("", "", false)));
return btnAddRow;
}
UPDATE: Trying the setTag()
and getTag()
to identify the two different TableLayout
s doesn't seem to be working. The mTag
variable appears null
. It's possible I may not be doing it correctly, but here is the setup:
The setTag()
tableLayoutFiles = findViewById(R.id.table_files);
tableLayoutFiles.setTag(R.id.table_files, "tableFiles");
tableLayoutAuthors = findViewById(R.id.table_authors);
tableLayoutAuthors.setTag(R.id.table_authors, "tableAuthors");
The getTag()
if(table.getTag()=="tableAuthors")
btnAddRow.setOnClickListener(v -> table.addView(setupAuthorsTableRow(context, table, "", "", "", "", false)));
else if(table.getTag()=="tableFiles")
btnAddRow.setOnClickListener(v -> table.addView(setupFilesTableRow(context, table, "", "", false)));
return btnAddRow;
The mTag variable
You can use the setTag
method, which allows you to set an arbitrary object to a view. For example:
logoText.setTag(R.id.table_tag_key, "Table1");
Then use getTag
to retrieve it.
https://developer.android.com/reference/android/view/View#setTag(int,%20java.lang.Object)