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

How to make a 'SET SELECTION' in spinner if you have a model type array list data in android kotlin?

 //if a have a value like 'Item 3' need to set selection   val spinnerItemValue="Item 3"//some country name //find the index of the item with the target text val position = arrayList!!.indexOfFirst { it.countryName(//Note: model key which store the country   name for spinner dorp-down)==spinnerItemValue}  //set the selection in the spinner  if(position>=0){       //if you user data binding in your project        binding.countrySpinner.setSelection(position)     //if you use findViewById() in your project       Spinner countrySpinner=findViewById(R.id.countrySpinner)       countrySpinner.setSelection(position) }

What is the Core Building Blocks of Android?

The core building blocks or fundamental components of android are activities, views, intents, services, content providers, fragments and AndroidManifest. Activity: An activity is a class that represents a single screen in application. View: A view is the User Interface element such as button, label, text field etc. Anything that you see is a view. Intent: Intent is used to invoke components. It is mainly used to: => Start the service => Launch an activity => Display a web page => Display a list of contacts => Broadcast a message => Dial a phone call etc. For Example:     Intent intent=new Intent(Intent.ACTION_VIEW);       intent.setData(Uri.parse(" https://moonyincoding.blogspot.com "));       startActivity(intent);    Service: Service is a background process that can run for a long time.  Content Provider: Content Providers are used to share data between the applications.  Fragment: Fragments ...

Retrofit 2 — Receive Plain-String Responses

Android apps usually interact with REST APIs, which often use JSON as a data format. We've focused almost all of our tutorials on sending JSON or XML requests, and converting JSON or XML responses. Scalars Converter for Plain Strings To receive plain-text or plain-string responses you can utilize the scalars converter. You can integrate the converter by adding it as a dependency to your app's build.gradle: dependencies {       implementation 'com.squareup.retrofit2:retrofit:2.9.0'     implementation 'com.squareup.retrofit2:converter-gson:2.9.0'     implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'//You need to add this     implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2' } Next, you need to describe the endpoint you want to interact with. In this demo case, you'll use a GET request with the dynamic URL feature to pass any URL to the method, and set the response type as String. @GET() Call<String...