Android MVVM Design Pattern Examples

JavaAndroidDesign PatternsMvvmDependency Injection

Java Problem Overview


I currently do a lot of WPF development and have started creating some basic Android apps. When creating WPF apps I often use MVVM, normally using Prism, and would like to know if there are any examples of MVVM for the Android platform?

Java Solutions


Solution 1 - Java

I am the developer of Android-Binding. Like @Brentley said, it's a very new project but I do hope to get more buzz and experience so that it can be improved. Back to your question, I have written some simple introduction/tutorials on MVVM with android-binding:

Potential adopters please also register on the project discussion group.

Solution 2 - Java

I sometimes use ViewModels to translate from a pure Model to what the Model should be displayed as, but so much of the MVVM-isms come from the fact that you have this massive data binding engine built into the WPF framework. You probably won't find the exact experience of WPF + MVVM in the Android world, but you can take a lot of the good concepts and implement them (just without the automatic data binding stuff).

For one, just create ViewModels. You don't need a framework like Prism to create ViewModels. You don't have all the PropertyChanged notifications and stuff like that, but you can translate your data into information that can be better used by your UI which will clean up your code. A perfect example of this is something I did with a slider-heavy UI. Android's SeekBar is always zero based and works with integer values, so you can't bind to min, max, and increment values from your model. You can use a ViewModel to translate your min/max values into 0-based equivalents that your SeekBar can use...just an example. Same goes for displaying colors and sizes based on value ranges, etc. To me, that's what ViewModels are all about.

As far as DependencyInjection stuff, check out RoboGuice. I just started using this in one of my projects after seeing a presentation by its creator at a local Meetup, and it's probably just what you're looking for.

RoboGuice on Google Code

RoboGuice Google Group

Solution 3 - Java

There is now an official Android Data Binding plugin although it's still in beta at the moment. Work is also being done to add tooling support for the binding syntax in the beta version of Android Studio.

See below for more information:

https://developer.android.com/tools/data-binding/guide.html

Solution 4 - Java

Great! Articals by @Florina Muntenescu

The Model-View-ViewModel Pattern

The main players in the MVVM pattern are:

  • The View — that informs the ViewModel about the user’s actions
  • The ViewModel — exposes streams of data relevant to the View
  • The DataModel — abstracts the data source. The ViewModel works with the DataModel to get and save the data.

Example of MVVM Architecture:

https://github.com/erikcaffrey/People-MVVM

https://github.com/googlesamples/android-architecture/tree/todo-mvvm-databinding/

https://github.com/iammert/Android-MVVM-Architecture

https://github.com/segunfamisa/android-mvvm-sample

https://github.com/manas-chaudhari/android-mvvm

Solution 5 - Java

There is a relatively new framework being developed that allows for Views to be bound to ViewModels called android-binding. Using this framework and RoboGuice you can get your MVVM on. The framework still needs some work, but it's a good starting point.

Solution 6 - Java

Android-Data-Binding library is a tool for connecting data to user interface elements. Once the layout file created and each item is tagged, one line of code binds all the data to user interface elements and saves your time for other tasks.

Solution 7 - Java

Recently I have implemented the MVVM pattern for building an Android app with Data Binding Library. Here you can read the detailed review of the work I have done and the code fragments: http://cases.azoft.com/mvvm-android-data-binding/

To learn more about the topic, you can also have a look at these app samples: https://github.com/ivacf/archi

There are visual examples of work done with the search and list screen.

Solution 8 - Java

A few years ago I also do some WPF&WP development,Prism & MVVM Light Toolkit is commonly use to build WP App,it is perfect for windows phone application architecture I think ! so I use my previous experience of WP development then imitate to create Android MVVM Light Toolkit(A toolkit help to build Android MVVM Application,We have more attributes for Data Binding of View(like Uri for ImageView) ,we create some command for deal with event( like click of Button),also have a global message pipe to communicate with other ViewModel).

GitHub:Android MVVM Light Toolkit, there are samples for reference.

Architecture: http://upload-images.jianshu.io/upload_images/966283-78b410b9af8b18fa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240

hope to help you!

Solution 9 - Java

There is one project called MVVMCross.

It's free, open-source and well designed MVVM framework, developed by Stuart Lodge.

He implemented binding for Android and iPhone, so now MVVM is applicable to all of these platforms too.

For me it is one of the best MVVM frameworks - it really shows the power of MVVM. With it you can write one code (model and viewmodel layers) for different platforms (WP7, Android, iPhone, WinRT) and just change application UI (view layer).

Solution 10 - Java

Just to post as a reference to other people who may be interested. I am a contributor to RoboBinding - A data-binding Presentation Model framework for the Android platform. It is another framework for the same purpose. Apart from helping with project structure, one major focus for RoboBinding is to make testing android apps with normal JUnit tests possible instead of Android unit tests, as Unit tests are so important to guarantee the quality of projects, but Android unit tests take ages to run and make unit tests somewhat impractical. RoboBinding itself comes with more than 300 JUnit tests to ensure its quality. MVVM originated from Microsoft as a specialization of the Presentation Model design pattern introduced by Martin Fowler. Other alternatives: Android-Binding, Bindroid and MvvmCross.

Solution 11 - Java

You can follow these steps for DataBinding in Fragments: I have posted design and java class both in Example for Binding Data in Fragment.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
    <data class=".UserBinding">
        <variable  name="user" type="com.darxstudios.databind.example.User"/>
    </data>
 <RelativeLayout

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment"
    >



        <TextView android:text='@{user.firstName+"  "+user.lastName}' android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:id="@+id/textView" />

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="New Button"
         android:id="@+id/button"
         android:layout_below="@+id/textView"
         android:layout_toEndOf="@+id/textView"
         android:layout_marginStart="40dp"
         android:layout_marginTop="160dp" />

 </RelativeLayout>
</layout>


    public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final User user = new User();
        user.setFirstName("Michael");
        user.setLastName("Cameron");
        UserBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_main, container, false);
        binding.setUser(user);

        View view = binding.getRoot();

        final Button button = (Button) view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                user.setFirstName("@Darx");
                user.setLastName("Val");
            }
        });

        return view;
    }

}

Developer site DataBinding Guide Line

Solution 12 - Java

I found this Writing Testable Android MVVM App series written about MVVM using Android Data Binding library is really nice. In the series he explained from simple example to recyclerview, and there are tests as well.

You can maybe try the mv2m library, too.

Solution 13 - Java

There are plenty of examples for MVVM framework in github. I recommend using DroidWizard

DroidWizard does its own coupling between view and view model and the framework is slightly different from mvvm.

ModelEngine ViewModel View instean of Model ViewModel View

Solution 14 - Java

https://github.com/MindorksOpenSource/android-mvvm-architecture

Android MVVM Architecture: Sample App

This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava, FastAndroidNetworking, PlaceHolderView and AndroidDebugDatabase

The app has following packages:

data: It contains all the data accessing and manipulating components.
di: Dependency providing classes using Dagger2.
ui: View classes along with their corresponding ViewModel.
utils: Utility classes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTom DudfieldView Question on Stackoverflow
Solution 1 - JavaxandyView Answer on Stackoverflow
Solution 2 - JavaRichView Answer on Stackoverflow
Solution 3 - JavaOdyView Answer on Stackoverflow
Solution 4 - JavaDhaval JivaniView Answer on Stackoverflow
Solution 5 - JavaBrentley JonesView Answer on Stackoverflow
Solution 6 - Javafurkan3ayraktarView Answer on Stackoverflow
Solution 7 - JavaVladimir TchernitskiView Answer on Stackoverflow
Solution 8 - JavaKelinHongView Answer on Stackoverflow
Solution 9 - JavagerichhomeView Answer on Stackoverflow
Solution 10 - JavaChengView Answer on Stackoverflow
Solution 11 - JavaRamkailashView Answer on Stackoverflow
Solution 12 - JavaninjahoahongView Answer on Stackoverflow
Solution 13 - JavaPrasanna AnbazhaganView Answer on Stackoverflow
Solution 14 - Javamohit issarView Answer on Stackoverflow