Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@maniac103
Copy link
Contributor

@maniac103 maniac103 commented Apr 24, 2025

Using findViewById is somewhat error prone due to the lack of a direct relationship between the XML description and the usage in code. View binding automatically generates code for creating that relationship.

I've sat on this for quite some time now, for two reasons:

  • Ensuring type safety was the cause for larger refactoring in WidgetAdapter, which caused that class to even grow (other code paths became smaller due to less internal member variables and findViewById calls)
  • Our duplicated widget list layouts (normal vs. compact) stretch the design of view binding quite a bit. I'm using the binding class for the normal layout for both cases, which has a consequence on layout compatibility: previously both layouts just needed to share view IDs, now the whole hierarchy needs to be the same (that is, the same classes, XML attributes obviously can differ). Whether that works or not is determined at runtime.

For the latter point, writing a unit test should be easy in theory (have a list of widget-layout-binding to compact-widget-layout-binding, inflate the former, bind the latter to the root view of the former), but this needs to be an instrumented Android unit test, and I'm not sure if and how this can be run in GH actions.

@mueller-ma Please let me know your thoughts on this.

TODO:

  • PrimaryServerPreference currently crashes on bind

@maniac103 maniac103 force-pushed the no-findviewbyid branch 2 times, most recently from d81cad4 to 5f52963 Compare April 25, 2025 10:43
@mueller-ma
Copy link
Member

Nice work! This is something I also wanted to do at some point. I'll have a look at running the emulator in CI, but for now you can add the test case to the repo and execute it locally.

@maniac103
Copy link
Contributor Author

Added the test, and it even found something ;-) Unfortunately it doesn't seem possible to exempt single views from being included in the binding.

@maniac103
Copy link
Contributor Author

@mueller-ma This conflicts with #3898, I guess check that one first? I'll rebase and undraft this one afterwards.

Only exception is WidgetAdapter due to its internal complexity.

Signed-off-by: Danny Baumann <[email protected]>
Also fix the one incompatible layout spotted by the unit test.

Signed-off-by: Danny Baumann <[email protected]>
@maniac103 maniac103 marked this pull request as ready for review July 16, 2025 13:02
Signed-off-by: Danny Baumann <[email protected]>
@mueller-ma mueller-ma mentioned this pull request Aug 10, 2025
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants