Skip to content

Latest commit

 

History

History
150 lines (113 loc) · 4.74 KB

part-1.md

File metadata and controls

150 lines (113 loc) · 4.74 KB

Data binding - Part 1 - View binding

Warning!

Refactoring view ids do not work.
Refactoring ids in xml do not automatically change them in activity classes.

Enable in gradle

android {
    //...
    buildFeatures {
        viewBinding true
    }
}

Binding in Activity

Layout XML file

Let the layout file name be activity_main_1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!--           Note the view id          -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/number_as_text"
        />
        
</LinearLayout>

Activity class

                                                    // DO NOT PASS layout reference in
                                                    // AppCompatActivity constructor.
class MainActivity: AppCompatActivity() {

                                                    // This class is generated automatically.
                                                    // Note the name is ActivityMain1Binding
                                                    // as layout file is activity_main_1.xml
    lateinit var binding: ActivityMain1Binding
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

                                                    // Inflate the layout.
                                                    // Set the root layout.
        binding = ActivityMain1Binding.inflate(layoutInflater)
        setContentView(binding.root)
        
                                                    // Access the views.
                                                    // Note that original ID of view
                                                    // is simple_text
                                                    // but the binding has the name in
                                                    // camel case as simpleText.
        binding.simpleText.text = "Hello world"
        
    }

}

Binding in Fragment

Layout XML file

Let the layout file name be just_a_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--           Note the view id          -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/just_a_text"/>

</LinearLayout>

Fragment class

class FragmentClass: Fragment() {

                                                    // Note the name of binding class.
                                                    // XML name - just_a_fragment.xml
                                                    // Binding class - JustAFragmentBinding
    lateinit var binding: JustAFragmentBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
    
                                                    // Inflate the binding and return the root.
        binding = JustAFragmentBinding.inflate(inflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
                                                    // Access the views.
                                                    // Note the name of id in xml is
                                                    // just_a_text
                                                    // but in the binding it is in camel case
                                                    // justAText
        binding.justAText.text = "Another text"
    }

}

Alternate way for the same thing as above

                                                    // Pass the layout in Fragment constructor
class FragmentClass: Fragment(R.layout.just_a_fragment) {

    lateinit var binding: JustAFragmentBinding
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

                                                    // Use the bind function of binding class
                                                    // to bind to the parent view.
        binding = JustAFragmentBinding.bind(view)

        binding.justAText.text = "Another text"
    }

}

Part 2