Reviews

Android Growth Fundamentals: Find out how to add View Binding to an Android Gradle venture

Related Articles

Most Android builders are most likely conversant in the basic findViewById() technique. Move it an ID of one of many Views in your XML format and it will return a reference to the inflated model of that View. That is all assuming you handed the precise ID although, and that the View really exists. findViewById() has no checks inbuilt to forestall you from attempting to retrieve a View you may’t retrieve. Enter View Binding.


As an alternative of utilizing findViewById() on every View you need, View Binding robotically generates a binding class for every format XML. Every View with an ID is robotically added to the category, so you may reference them immediately.

Including View Binding to an Android Gradle venture is tremendous easy.


Gradle Setup

View Binding is enabled on the module degree in Gradle. When you have a number of modules, you will have to allow it individually for each.

Within the android block in your module-level construct.gradle, add the choice to allow View Binding.

 android {
    ...

    buildFeatures {
        viewBinding true
    }
}

There could also be a warning about unlawful entry, however that is a lint bug and could be safely ignored.

Sync the venture and View Binding might be enabled. It is that straightforward.

Utilizing View Binding

There are a couple of methods to make use of View Binding, however earlier than any of that occurs, let’s discuss how the binding courses are generated.

Class Identify Syntax

Say you will have a format XML named some_layout.xml. Its corresponding binding class might be named SomeLayoutBinding. That sample holds for all recordsdata.

Every phrase (separated by underscores within the file title) might be capitalized, and the underscores might be eliminated. “Binding” then will get added to the top.

Instantiating with Present View

In the event you’ve already inflated the format file and you’ve got a reference to the foundation of the format, you may inform the View binding class to make use of the present format.

Kotlin:

 val binding = SomeLayoutBinding.bind(someLayoutRoot ) 

Java:

 SomeLayoutBinding binding = SomeLayoutBinding.bind(someLayoutRoot ); 

For instance, if you happen to needed to make use of the binding class in a Fragment, it could look one thing like this.

Kotlin:

 class SomeFragment : Fragment(R.format.some_layout) {
   
    non-public val binding by lazy { SomeLayoutBinding.bind(view) }

    override enjoyable onViewCreated(view: View, savedInstanceState: Bundle?) {
        tremendous.onViewCreated(view, savedInstanceState)
        
       
    }
}

Java:

 public class SomeFragment extends Fragment {
    non-public SomeLayoutBinding binding = null;

    public SomeFragment() {
        tremendous(R.format.some_layout);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        tremendous.onViewCreated(view, savedInstanceState);
   
       
        binding = SomeLayoutBinding.bind(view);
    }
}

Instantiating with New View

The binding class may also handle inflating the format for you.

Kotlin:

 val binding = SomeLayoutBinding.inflate(layoutInflater ) 

Java:

 SomeLayoutBinding binding = SomeLayoutBinding.inflate(layoutInflater ); 

This technique is helpful in each Fragments and Actions.

An instance Fragment would look one thing like the next.

Kotlin:

 class SomeFragment : Fragment() {
    non-public val binding by lazy { SomeLayoutBinding.inflate(LayoutInflater.from(requireContext())) }

    override enjoyable onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) {
       
        return binding.root
    }
}

Java:

 public class SomeFragment extends Fragment {
    non-public SomeLayoutBinding binding = null;

    @Override
    public void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       
        binding = SomeLayoutBinding.inflate(inflater);
       
        return binding.getRoot();
    }
}

An instance Exercise would look one thing like the next.

Kotlin:

 class SomeActivity : AppCompatActivity() {
    non-public val binding by lazy { SomeLayoutBinding.inflate(layoutInflater) }

    override enjoyable onCreate(savedInstanceState: Bundle?) {
        tremendous.onCreate(savedInstanceState)

       
        setContentView(binding.root)
    }
}

Java:

 public class SomeActivity extends AppCompatActivity {
    non-public SomeLayoutBinding binding = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        tremendous.onCreate(savedInstanceState);

       
        binding = SomeLayoutBinding.inflate(getLayoutInflater());
       
        setContentView(binding.getRoot());
    }
}

Referencing Views

Now that the View binding class is ready up and able to use, it is time to really use it.

To illustrate the contents of the some_layout.xml are one thing like the next:

 <LinearLayout
    android:
    ...>
    <FrameLayout
        android:
        ...
        />
    <ImageView
        android:
        ...
        />
    <LinearLayout
        android:
        ...>
        <ImageView
            android:
            ...
            />
    </LinearLayout>
</LinearLayout>

There are plenty of IDs there to reference in code. However so long as you will have the binding class instantiated, referencing might be straightforward.

In Kotlin, Views are referenced by variables matching their IDs, with some adjustments. Underscores are eliminated and the ensuing string is camel-cased. For instance, to reference some_frame_layout from code, you’d use binding.someFrameLayout. The someFrameLayout variable might be an occasion of FrameLayout.

 val someFrameLayout: FrameLayout = binding.someFrameLayout 

In Java, Views are referenced by getter strategies matching their IDs, with an identical format to Kotlin. For instance, to reference some_frame_layout, you’d use binding.getSomeFrameLayout(). The tactic will return an occasion of FrameLayout.

 FrameLayout someFrameLayout = binding.getSomeFrameLayout(); 

The View references are additionally flattened within the binding. Referencing inner_imageview is similar as referencing some_frame_layout.

Conclusion

As I am positive you may see, View Binding in Android is each straightforward to implement and simple to make use of. In lots of instances, it is simpler to make use of than findViewById().

For extra particulars on implementing View Binding, together with some examples, take a look at Google’s official documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button