What is the difference between getting a reference to a widget like this:
TableRow row = findViewById(R.id.table_row);
and:
TableRow row = (TableRow)LayoutInflater.from(this).inflate(R.layout.table_row, null);
Is there also a difference when the TableRow
is a root of its layout or if its just a small part of a layout?
1) Using
TableRow row = findViewById(R.id.table_row);
you are simply obtaining a reference to a View
with id R.id.table_row which has already been created and inflated in the current layout (where current means the Activtiy
's layout or the View
that you are defining).
2) Using
TableRow row = (TableRow)LayoutInflater.from(this).inflate(R.layout.table_row, null);
You are inflating (which means creating) a new views hierarchy based on the XML definition contained in R.layout.table_row. Since you are not passing the parent View parameter in the inflate()method, you will need to add the resulting hierarchy manually to an existent container.