No more findViewById

One of the little known feature of  developing a Application with android Studio is DATA BINDING. Most basic thing you get by using this feature is eleminating findViewById().

                   Isn't it a pain to write findViewById again and again?



TextView hello = (TextView) findViewById(R.id.hello);


There are tools available whose main job is to eliminate this small bit of code, but now there is an official way with Android Studio 1.5 and higher. First, you must edit your Application’s build.gradle file and add the following into the android block:


android {
    
    dataBinding.enabled = true
}

The next thing is to change the layout file by making the outer tag <layout> instead of whatever ViewGroup you use:

hello_world.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".MainActivity">

        <TextView
                android:id="@+id/hello"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    </RelativeLayout>
</layout>
The next thing you have to do is to tell it to load your layout file differently at runtime. Because this works all the way back to the Eclaire release, there is no reliance on new framework changes to load these preprocessed layout files. Therefore, you do have to make a slight change to your loading procedure.
From an Activity, instead of: (N)

1
2
3
4
setContentView(R.layout.hello_world);
TextView hello = (TextView) findViewById(R.id.hello);
hello.setText("Hello World"); // for example, but you'd use
                              // resources, right?

You load it like this: (Y)


1
2
3
HelloWorldBinding binding = 
    DataBindingUtil.setContentView(this, R.layout.hello_world);
binding.hello.setText("Hello World"); // you should use resources!


Here you can see that a class, HelloWorldBinding was generated for the hello_world.xml layout file and the View with the ID “@+id/hello” was assigned to a final field hello that you can use. No casting, no findViewById.

It turns out that this mechanism for accessing views is not only much easier than findViewById, but can also be faster! 

One thing you will see is that it camel-casifies your variable names (just like hello_world.xml becomes the class HelloWorldBinding), so if you gave it the ID “@+id/hello_text” then the field name would be helloText.
Note-: If you are using recyclerview, ViewPager and you are inflating the view that aren't setting the activity component. 
There are several versions that match the LayoutInflater, so use the one that is most appropriate for your use. For example:

1
2
HelloWorldBinding binding = HelloWorldBinding.inflate(
    getLayoutInflater(), container, attachToContainer);

Now, you may be wondering, what if I have a layout with different configurations with some differing Views? The layout preprocessing and runtime inflation stages take care of this for you by adding all View IDs to the generated class and just sets them to null if they aren’t in the inflated layout.
No more findViewById No more findViewById Reviewed by Unknown on 02:27 Rating: 5

No comments:

Powered by Blogger.