android icon indicating copy to clipboard operation
android copied to clipboard

Start using Binding instead of using findViewByID

Open nangarita-coder opened this issue 3 years ago • 6 comments

The findViewById method is often used in the application to retrieve a view from a view hierarchy based on an identifier but the problem is that this seemingly harmless method can, in fact, cause performance issues in your Android application if used carelessly, as it is a very demanding method in terms of resource usage.

To solve this problem it is possible to make use of view binding, this is a feature that allows you to more easily write code that interacts with views. Once view linking is enabled in a module, a link class is generated for each XML layout file present in that module. An instance of a link class contains direct references to all views that have an ID in the corresponding layout.

An example of a method in which many are made is the getView method located in the following path owncloudApp/src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java in this method calls are made to the findViewById method 7 times which is evidenced in the code snippet below.

image_2022-11-25_222958101

TASKS

  • [ ] Investigate the different view binding libraries to find the implementation that best suits the project architecture. ex https://developer.android.com/topic/libraries/view-binding
  • [ ] Create branch name binding Transition
  • [ ] Development tasks
    • [ ] Identify classes where there is excessive use of the findViewByID method.
    • [ ] Implement view binding on the files identified in the previous step
  • [ ] Code review and apply changes requested
  • [ ] Design test plan
  • [ ] QA
  • [ ] Merge branch feature/feature_name into master

nangarita-coder avatar Nov 26 '22 03:11 nangarita-coder

Hi @nangarita-coder!

First of all, thanks for opening an issue!

Currently, we are working hard to refactor a lot of classes in the app and release a 3.0 version. During this process, we have already moved some classes from findViewById to ViewBinding. For example, the LoginActivity is already using ViewBinding

We will take a deeper look after releasing 3.0 to keep replacing the findViewById with ViewBinding 👍

abelgardep avatar Nov 28 '22 08:11 abelgardep

Hey , i would like to work on this ;;

dev-divyansh avatar Apr 06 '23 11:04 dev-divyansh

Hey, @dev-divyansh Go ahead!

We already use ViewBinding for some screens but there are still several uses of findViewById, so take it easy!

abelgardep avatar Apr 10 '23 06:04 abelgardep

Hey @abelgardep I am beginner in OpenSource would need your guidance ; Can I open a PR if i refract all the changes in minimum of one Activity ?

dev-divyansh avatar Apr 11 '23 16:04 dev-divyansh

Sure @dev-divyansh, here you have a little guide on how to contribute

You can send a PR with the changes you propose. If it's your first time, this activity could be a good candidate to start using binding instead of findViewById.

That activity is the one it is shown when we try to move/copy files from one folder to another one.

There are some servers available for testing: demo.owncloud.com u:demo p:demo

If u need something else, just let me know!

abelgardep avatar Apr 11 '23 16:04 abelgardep

@abelgardep hey , can you review my PR

dev-divyansh avatar Apr 14 '23 05:04 dev-divyansh