15

The View Binding in Android is heavily advocated as a better replacement for findViewById() and even Kotlin extensions, but for some reason the most basic functionality as searching usages of a View by its ID in Layout Editor in Android studio doesn't work.

It doesn't work even in the View Binding Sample in the official Acrh Components Samples repository.
Here we have:

  1. Xml layout fragment_blank.xml with a TextView with ID textViewFragment as following:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".InflateFragment">

    <TextView
        android:id="@+id/textViewFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
  1. Fragment InflateFragment.kt which sets text to this TextView using view binding:
override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val binding = FragmentBlankBinding.inflate(inflater, container, false)
        fragmentBlankBinding = binding
        binding.textViewFragment.text = getString(R.string.hello_from_vb_inflatefragment)
        return binding.root
    }
  1. So, when I select this ID in the layout editor and do action "Find Usages" from the context menu: enter image description here

  2. Nothing actually gets found, but we clearly see that we are setting text to this text view in InflateFragment.kt enter image description here

Any ideas on how to make such usages searchable by View ID from the Layout Editor?
Can't believe that such a basic functionality is missing for View Binding.

Android Studio ver 4.0.1.
Gradle ver 6.6.1.
Android Gradle Plugin ver 4.0.1

Alex Wih
  • 1,015
  • 2
  • 11
  • 24
  • also wait for solution for a long time. have you tried AS preview versions? – j2esu Oct 07 '20 at 10:58
  • Hi You can goto the generated file code and find use from there , I have written a blog post completely explaining view binding , you can use it to find the code path .checkout [Androidbites|ViewBinding](https://chetangupta.net/viewbinding/) – Chetan Gupta Dec 06 '20 at 10:52

0 Answers0