Giter Club home page Giter Club logo

android-mvvm-basic's Introduction

MVVM - Model View ViewModel

Setup :

app/build.gradle
android {
    dataBinding {
        enabled = true
    }
 }  

Basic Layout Structure :

<layout>
  <data>
       <variable
            name="user"
            type="com.example.mvvmhelloworldexample.model.User" />
    </data>
 
    <LinearLayout>
       <!-- YOUR LAYOUT HERE -->
    </LinearLayout>
</layout>

Calling from class :

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

Note: Binding class will generate with your xml layout prefix

Example: activity_main.xml -> ActivityMainBinding.java

Print value in view :

<TextView
    android:layout_width="wrap_content"
    android:textSize="25sp"
    android:layout_height="wrap_content"
    android:text="@{user.email}" />

Note: For Real time update data you should declare notifyPropertyChanged(BR.name) in setter method And apply @Bindable on your getter method

Call a mehod from layout file :

<TextView
	android:layout_width="wrap_content"
	android:textSize="30sp"
	android:layout_height="wrap_content"
	android:text="@{BindingUtils.capitalize(user.name)}" />

Note: You Should import class and method must be static

Import class in Layout file :

<data>
	<import type="com.example.mvvmhelloworldexample.BindingUtils" />
</data>

Call Custom Listener method :

<Button
    android:id="@+id/btn"
    android:text="Click"
    android:onClick="@{handlers::onButtonClicked}"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
Call with param -
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="@{(v) -> handlers.onButtonClickWithParam(v, user)}"
    android:text="CLICK WITH PARAM" />

Note: You should declare variable in data section

Image Binding :

Model class:

private String profileImage;

public String getProfileImage() {
       return profileImage;
   }

   public void setProfileImage(String profileImage) {
       this.profileImage = profileImage;
   }

   @BindingAdapter({"android:profileImage"})
   public static void loadImage(ImageView view, String imageUrl) {
       //If you use picasso
       Picasso.get().load(imageUrl).into(view);
   }

From layout:

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginTop="5dp"
    android:profileImage="@{user.profileImage}" />

Calling for uses:

private void imageBinding(){
    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    User user = new User();
    user.setProfileImage("https://cdn.pixabay.com/photo/2017/12/21/12/26/glowworm-3031704_960_720.jpg");
    binding.setUser(user);
}

android-mvvm-basic's People

Contributors

akramul-hasan-m4 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.