RunCodes
Programming & Tech

How to use View Binding in Android?

bye bye findViewById().....

0 493

In this tutorial, we are going to show you how you can use android new feature View Binding in your android application and what are it’s advantage.

Before using this feature, you should fulfill certain requirement. First, you have to have an Android Studio version 3.6 canary 11+ in your system. Second, you have to have android gradle plugin 3.6 or higher. After updating your android studio to 3.6 and gradle version to 3.6, you are ready to use this new feature. So let’s do it.

 

[sociallocker]

Open up your android studio and create new file. After finishing the gradle build, you need to go to the gradle file i.e. app level gradle not in project level gradle and paste the following code inside android and sync the gradle.

viewBinding{
enabled=true
}
How to use View Binding in Android?
How to use View Binding in Android?

After syncing the gradle file, Android Studio will generate one binding class for your layout with the name same as your layout file but start with capital letter and remove the underscore sign means camel case representation and add the word binding at the ends. What it means is if your layout file name is activity_main.xml then the autogenerated binding class name will be ActivityMainBinding.

Next, what you need to do is create the variable of this class inside your activity.

private ActivityMainBinding binding

Let’s talk about the previous method of binding the controls (editText, PlainText, Button etc) . What we did is we declare our controls inside the class and initialized them inside the onCreate() method using the findViewById() and we pass the id of each controls which we already give in the layout file.

But here in View Binding, what you need to do is:

  1. Call the static method inflate() which is include in generated binding class. This creates an instance of the binding class for the activity to use.
  2. Get a reference to the root view by calling getRoot() method.
  3. Pass the root view to setContentView() to make it the active view on the screen.

Now let’s move into the practical part:

Drag and drop some controls inside your activity_main.xml file and give id of each controls or copy following code.


<TextView
android:id="@+id/txtName"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="59dp"
android:layout_marginLeft="59dp"
android:layout_marginTop="240dp"
android:layout_marginEnd="59dp"
android:layout_marginRight="59dp"
android:layout_marginBottom="77dp"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="@+id/btnClick"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btnClick"
android:layout_width="252dp"
android:layout_height="0dp"
android:layout_marginEnd="49dp"
android:layout_marginRight="49dp"
android:layout_marginBottom="222dp"
android:text="Click"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtName" />

Now goto your activity means goto your MainActivity.java file and write the following code.


public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding; //creating the binding class variable

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding=ActivityMainBinding.inflate(getLayoutInflater()); //calling static method inflate()
View view = binding.getRoot(); //get a reference to the root view
setContentView(view); //active view on screen
binding.txtName.setText("TechSupportNep"); //this is how you can access your controls

//class-variable-name.controls-id
binding.btnClick.setOnClickListener(new View.OnClickListener() { //create the listening event of button
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Button is Clicked", Toast.LENGTH_LONG).show();
}
});
}
}

that’s all. Very easy. if you have any confusion while using this new feature, please let us know.

Advantages of View Binding

  1. It is null safe: since viewBinding creates direct reference to view, there is no risk of a null pointer exception due to an invalid view ID.
  2. Type safe: There is no risk of class

[/sociallocker]

  1. cast exception.
  2. it is faster compilation and easy to use.

if you have any confusion watch the video:

 

Leave a comment