Search code examples
androidkotlinandroid-viewbinding

How to use ViewBindings for mutiple layouts


I'm migrating from kotlinx.android.synthetic to ViewBindings.

I have two layouts(for phones and tablets) with the same set of ids:

class GameActivity: AppCompatActivity() {
    lateinit var binding: ViewBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (AppData.instance.isTablet)
            binding=ActivityGameTabletBinding.inflate(layoutInflater)
        else
            binding=ActivityGamePhoneBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val btn=binding.menuBtn //no such property
    }
    ...

}

The problem is binding contains only one property which is root.

Thus I'm forced to fallback to old getViewById. Is there way to have viewBinding features for different layouts?


Solution

  • You cannot have a variable have binded to 2 different views instead you can create 2 variables and initialize both of them and use them accordingly.

    Here is an example.

    private lateinit var phoneBinding : ActivityGamePhoneBinding
    private lateinit var tabletBinding : ActivityGameTabletBinding
    

    Then initialize both of them.

    phoneBinding = ActivityGamePhoneBinding.inflate(layoutInflater)
    tabletBinding = ActivityGameTabletBinding.inflate(layoutInflater)
    
      if (AppData.instance.isTablet){
           val btn = tabletBinding.button
           //Do rest of your tablet code
      } else {
           val btn = phoneBinding.button
           //Do rest of your phone code
      }
    

    If you are doing common things you should create a function and call the same function in both scenarios.

    phoneBinding = ActivityGamePhoneBinding.inflate(layoutInflater)
    tabletBinding = ActivityGameTabletBinding.inflate(layoutInflater)
    
      if (AppData.instance.isTablet){
           val btn = tabletBinding.button
           onButtonClick(btn)
      } else {
           val btn = phoneBinding.button
           onButtonClick(btn)
      }
    
      fun onButtonClick(button : Button){
       button.setOnclickListner{
        //rest of you code.
       }
      }