Android-Data Binding
The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically
1.First create a new Android project and chose empty activity and Java language.
2.Go to build.gradle file and add this under android{}:
1 2 3 4 5 6 |
android { dataBinding { enabled = true } } |
After that sync project.
Data binding in Activity
3. Go to on xml file which is default created activity_main.xml. Below is the code for the activity_main.xml file.
Here just giving an example of how to use binding for a button and what its use is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data> </data> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:padding="16dp" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_submit" android:text="Submit" android:layout_gravity="center" android:layout_marginTop="8dp" /> </LinearLayout> </layout> |
4. Working on the java file, MainActivity.java file, and using the following code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.example.databindingexample; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import android.os.Bundle; import android.view.View; import com.example.databindingexample.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { ActivityMainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding= DataBindingUtil.setContentView(this,R.layout.activity_main); binding.btnSubmit.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { } } ); } } |
Data binding in Fragment
1.Go to the app right-click and create a new blank fragment as MainFragement. Use the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data> </data> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:gravity="center_horizontal" android:layout_height="match_parent" tools:context=".MainFragment"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt_submit" android:text="submit" android:layout_marginTop="8dp" /> </LinearLayout> </layout> |
2.Then Go to the MainFragment.java file and use the following code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package com.example.databindingexample; import android.os.Bundle; import androidx.databinding.DataBindingUtil; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.databindingexample.databinding.FragmentMainBinding; public class MainFragment extends Fragment { private FragmentMainBinding binding; private View view; // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; public MainFragment() { // Required empty public constructor } // TODO: Rename and change types and number of parameters public static MainFragment newInstance(String param1, String param2) { MainFragment fragment = new MainFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { binding= DataBindingUtil.inflate(inflater,R.layout.fragment_main,container,false); view=binding.getRoot(); binding.btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); return view; } } |