YouKnowWho
YouKnowWho

Reputation: 31

Android ViewBinding unable to render LinearLayout configuration

The ViewBinding implementation is unable to render the layout configuration for LinearLayout used in my code, while the same layout works with the older technique of findViewById()

I have setup the gradle to use ViewBinding

android {
    ...
    buildFeatures {
        viewBinding = true
}

Below is the activity which uses a LinearLayout

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:orientation="vertical"
    tools:context=".MainActivity">

  <ImageView
        ... />

  <Button
        ... />

</LinearLayout>

The corresponding Kotlin class includes the binding instance with its root view passed to setContentView()

MainActivity.kt

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    ...

    setContentView(binding.root)
}

While the Layout editor renders a correct alignment of the app, once it is launched onto the emulator the layout configuration such as center_vertical alignment set using layout_gravity of LinearLayout is ignored.

Is something incorrect or missing the way ViewBinding is implemented?

Upvotes: 3

Views: 858

Answers (4)

Lucase
Lucase

Reputation: 21

I had the same problem doing an exercise in the "Developing Android Apps with Kotlin" course.

I did not find a solution because I suppose that without using view binding, a view above the one in main_activity.xml must be taken as root (probably the same full screen), so when using layout_gravity without view binding, it works, because it positions the LinearLayout relative to the screen. However, when using view binding, I assume that it takes the LinearLayout as its root, so layout_gravity does nothing (again, I'm not sure, I don't know very well how view binding works).

The solution I used was to simply try to achieve the same view with something different. For this in activity_main.xml I used:

android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
tools:context=".MainActivity">

Upvotes: 2

Malaka Perera
Malaka Perera

Reputation: 21

Had the same issue, Solved with adding android:gravity="center_vertical" to LinearLayout in activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:orientation="vertical"
    android:id="@+id/root_view"
    tools:context=".MainActivity">

</LinearLayout>

Upvotes: 2

mojtaba joshaghani
mojtaba joshaghani

Reputation: 71

in your activity_main.xml give id to your LinearLayout

Upvotes: 2

Pranav P
Pranav P

Reputation: 1815

There are one possibility is that your LinearLayout have not any id.

When you are doing binding.root it keeps a reference of root view which is LinearLayout in your case.

As per the official document:

If view binding is enabled for a module, a binding class is generated for each XML layout file that the module contains. Each binding class contains references to the root view and all views that have an ID.

Give id to your LinearLayout android:id="rootView, rebuild your project and try to run. I hope, it will solve your issue.

Thanks & Happy coding..!

Upvotes: 1

Related Questions