-2

I want to retrieve the id of a Layout inside of one of my layout xmls, and it's possible to use both R.id.* and the binding for this - details below. Which is the preferred way?

Given the following structure:

app  
|-- java.org.romco.appname  
|  |-- MainActivity.kt  
|-- res.layout  
|  |-- main_activity.xml  
|  |-- main_content.xml  

Let's say my main_activity.xml is a CoordinatorLayout and includes the main_content.xml with a defined id of "main_content", such as:

    android:id="@+id/main_content"
    layout="@layout/content_main" />

My content_main.xml then contains a FrameLayout with a defined if of "task_details_container", such as:

    <FrameLayout
        android:id="@+id/task_details_container" />

Following the official guides, the binding in MainActivity.kt would be created as:

class MainActivity : AppCompatActivity() {

    private lateinit var mainActivityBinding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mainActivityBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mainActivityBinding.root)
    }

If I now want to refer to the FrameView inside of my content_main.xml, how do I go about it? Should i use what I'd normally use, so R.id.task_details_container, or is it preferred to use the binding to retrieve the ID now that I have it available? If I try to log it in onCreate, the output is the same:

        val id1 = R.id.task_details_container
        val id2 = mainActivityBinding.mainContent.taskDetailsContainer.id

        Log.d("MainActivity", "R id = $id1, binding id = $id2")

Log output:

D/MainActivity: R id = 2131231072, binding id = 2131231072

From what I've found so far:

But none of these talk about whether using the bound id is better than using R.id.. My guess is that using R.id. is preferred, since not all elements have to be view-bound and therefore, for the sake of consistency, I can use R.id.* everywhere, where as I possibly can't use binding where the property is not accessible. But as I said, it's just a guess and I'd like to hear a wiser opinion on this.

roman
  • 151
  • 1
  • 9
  • 1
    can't tell without knowing what you need the ID for – EpicPandaForce Jan 15 '21 at 12:51
  • 1
    no need to use findViewId just use binding.ViewId or button or textView id and acess it directly like binding.tvName.text="Any" – Amit pandey Jan 15 '21 at 12:52
  • I'm trying to programatically add a fragment into the container: `supportFragmentManager.beginTransaction() .replace(mainActivityBinding.mainContent.taskDetailsContainer.id, newFragment) ` ---- OR `supportFragmentManager.beginTransaction() .replace(R.id.task_details_container, newFragment) .commit() ` – roman Jan 15 '21 at 13:42

1 Answers1

2

You should always use View Binding. It is more efficient than findViewById. Every time you call findViewById(), the Android system traverses the entire view hierarchy at runtime to find the view. Whereas View Binding traverses the view hierarchy only once. For a large view hierarchy, using findViewById can noticeably slow down the app for the user.

If you are using view ids for anything other than calling findViewById(), is safe to use R.id.view_id instead binding.view.id. Both have the same performance characteristics

Sinner of the System
  • 2,558
  • 1
  • 8
  • 17