Skip to main content

A Simple Data Binding Example

 Step-1: Enable data binding to android project ADD below line to your app level build.gradle file.

android {

    buildFeatures {

        dataBinding = true

    }


Setp-2: Add all your XML layout design inside this <layout></layout> tag

<?xml version="1.0" encoding="utf-8"?>

<layout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout

    android:id="@+id/main"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <TextView

        android:id="@+id/greeting_text_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/insert_you_name_here"

        android:textSize="30sp"

        android:textStyle="bold"

        android:typeface="sans"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintHorizontal_bias="0.532"

        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintRight_toRightOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintVertical_bias="0.175" />


    <EditText

        android:id="@+id/name_edit_text"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_margin="20dp"

        android:ems="10"

        android:hint="@string/insert_you_name_here"

        android:inputType="textPersonName"

        android:textSize="30sp"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/greeting_text_view"

        app:layout_constraintVertical_bias="0.126" />


    <Button

        android:id="@+id/submit_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/submit"

        android:textSize="30sp"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintHorizontal_bias="0.498"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintVertical_bias="0.613" />


</androidx.constraintlayout.widget.ConstraintLayout>

</layout>


Step-3: Bind your XML layout to MainActivity.java


import android.os.Bundle

import android.widget.Button

import android.widget.EditText

import android.widget.TextView

import androidx.activity.enableEdgeToEdge

import androidx.appcompat.app.AppCompatActivity

import androidx.core.view.ViewCompat

import androidx.core.view.WindowInsetsCompat

import androidx.databinding.DataBindingUtil

import com.anushka.bindingdemo1.databinding.ActivityMainBinding


class MainActivity : AppCompatActivity() {

    private lateinit var binding : ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        enableEdgeToEdge()

//        setContentView(R.layout.activity_main)

        binding = DataBindingUtil.setContentView(this,R.layout.activity_main)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->

            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())

            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)

            insets

        }

//        val button = findViewById<Button>(R.id.submit_button)

//        button.setOnClickListener {

//            displayGreeting()

//        }

        binding.submitButton.setOnClickListener {

            displayGreeting()

        }

    }

    private fun displayGreeting() {

//        val messageView = findViewById<TextView>(R.id.greeting_text_view)

//        val nameText = findViewById<EditText>(R.id.name_edit_text)

//

//        messageView.text = "Hello! "+ nameText.text

        binding.apply {

            greetingTextView.text = "Hello! "+ nameEditText.text

        }

    }


Comments

Popular posts from this blog

What is android?

  Android is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones etc. Or We can say, android is a Linux-Based Operating System. It is currently used in various devices such as mobiles, tablets, televisions etc.

What is the Android Architecture?

 Well, Android Architecture is categorized into five part.     1. Linux kernel     2. Native Libraries (Middleware),     3. Android Runtime     4. Application Framework     5. Applications You can see the picture shown below. Linux kernel: It is the heart of android architecture that exists at the root of android architecture. Linux kernel is responsible for device drivers, power management, memory management, device management and resource access. Native Libraries: On the top of Linux kernel, their are Native libraries such as Web-Kit, Open-GL, Free-Type, SQLite, Media, C runtime library (libc) etc. The Web-Kit library is responsible for browser support, SQLite is for database, Free-Type for font support, Media for playing and recording audio and video formats. Android Runtime: In android runtime, there are core libraries and DVM (Dalvik Virtual Machine) which is responsible to run android application. DVM...

How to call Post type API using Retrofit in Android?

Step by Step Implementation Step 1: Create a New Project Step 2: Add the below dependency in your build.gradle file Navigate to the Gradle Scripts > build.gradle (Module:app) and add the below dependency in the dependencies section.  // below dependency for using the retrofit implementation ‘com.squareup.retrofit2:retrofit:2.9.0’ implementation ‘com.squareup.retrofit2:converter-gson:2.5.0’ After adding this dependency sync your project and now move towards the AndroidManifest.xml part.   Step 3: Adding permissions to the internet in the AndroidManifest.xml file Navigate to the app > AndroidManifest.xml and add the below code to it.  <!--permissions for INTERNET--> <uses-permission android:name="android.permission.INTERNET"/> Step 4: Working with the activity_main.xml file Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.  <?xml version="1.0"...