Site icon C1CTech

A Simple Basic Example of ViewModel

<p><strong> <span style&equals;"color&colon; &num;008000&semi;">ViewModelExample<&sol;span><&sol;strong>  is a very<em> <&sol;em>straightforward app with buttons that increase Or decrease  the no of items&period; The finished app has a bug though&semi; if you rotate the phone&comma; your selected number of items will again set up with the initial value&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-medium wp-image-369" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;04&sol;Screenshot&lowbar;2018-04-27-21-40-391-180x300&period;png" alt&equals;"" width&equals;"180" height&equals;"300" &sol;>        <img class&equals;"alignnone size-medium wp-image-370" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;04&sol;Screenshot&lowbar;2018-04-27-21-41-191-300x180&period;png" alt&equals;"" width&equals;"300" height&equals;"180" &sol;><&sol;p>&NewLine;<p>What’s going on&quest; Rotating a device is one of a few <span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;guide&sol;topics&sol;manifest&sol;activity-element&period;html&num;config" data-href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;guide&sol;topics&sol;manifest&sol;activity-element&period;html&num;config"><strong>configuration changes<&sol;strong><&sol;a><&sol;span> that an app can go through during its lifetime&period; When configurations changed during run time &lpar;such as screen orientation&comma; keyboard availability&comma; and language&rpar;&comma; Android usually destroys application’s existing Activity or Fragment and recreate it&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>ViewModel<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>At <strong><span style&equals;"color&colon; &num;008000&semi;">Google I&sol;O 2017<&sol;span><&sol;strong>&comma; the Android Framework team introduced a new set of <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;topic&sol;libraries&sol;architecture&sol;index&period;html" data-href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;topic&sol;libraries&sol;architecture&sol;index&period;html">Architecture Components<&sol;a><&sol;span><&sol;strong>&comma; one of which deals with this exact rotation issue&period;<&sol;p>&NewLine;<p>The <span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;arch&sol;lifecycle&sol;ViewModel&period;html" data-href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;arch&sol;lifecycle&sol;ViewModel&period;html"><strong>ViewModel<&sol;strong><&sol;a><&sol;span> class is designed to hold and manage UI-related data in a life-cycle conscious way&period; This allows data to survive configuration changes such as screen rotations&period;<&sol;p>&NewLine;<p>Download <strong>Github<&sol;strong> code from <a href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;ViewModelExample"><span style&equals;"color&colon; &num;00ff00&semi;"><strong>here<&sol;strong><&sol;span><&sol;a>&period;<&sol;p>&NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"15oSrAB0fxk" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;15oSrAB0fxk" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;15oSrAB0fxk&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;15oSrAB0fxk&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p>&NewLine;<p>Previously&comma; you might have used <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Activity&period;html&num;onRetainNonConfigurationInstance&percnt;28&percnt;29" data-href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Activity&period;html&num;onRetainNonConfigurationInstance&lpar;&rpar;">onRetainNonConfigurationInstance<&sol;a><&sol;span><&sol;strong> to save this data during a configuration change and unpack it on the other end&period;<&sol;p>&NewLine;<p>In the diagram below&comma; you can see the lifecycle of an Activity which undergoes a rotation and then is finally finished&period; The lifetime of the ViewModel is shown next to the associated Activity lifecycle&period; Note that ViewModels can be easily used with both Fragments and Activities&comma; which I’ll call <span style&equals;"color&colon; &num;008000&semi;"><strong>UI controllers<&sol;strong><&sol;span>&period; This example focuses on Activities&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-374 size-full aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;04&sol;Capture&period;jpg" alt&equals;"" width&equals;"514" height&equals;"491" &sol;>The ViewModel exists when you first request a ViewModel &lpar;usually in the onCreate the Activity&rpar; until the Activity is finished and destroyed&period; onCreate may be called several times during the life of an Activity&comma; such as when the app is rotated&comma; but the ViewModel survives throughout&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>There are three steps to setting up and using a ViewModel&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;008000&semi;">Step 1&colon; Create a ViewModel class<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>public class OrderViewModel extends ViewModel &lbrace;&NewLine;&NewLine; public int orderedCoffee &equals; 0&semi;&NewLine;&NewLine; public int orderedTea &equals; 0&semi;&NewLine;&NewLine; public int orderedColddrink &equals; 0&semi;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;008000&semi;">Step 2&colon; Set up communications between your ViewModel and your UI controller<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>Your UI controller &lpar; Activity or Fragment&rpar; needs to know about your ViewModel&period; This is so your UI controller can display the data and update the data when UI interactions occur&comma; such as pressing a button to increase or decrease the no of items&period;<&sol;p>&NewLine;<pre>OrderViewModel mViewModel&semi;&NewLine;&NewLine;&commat;Override&NewLine;protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine;mViewModel &equals; ViewModelProviders&period;of&lpar;this&rpar;&period;get&lpar;OrderViewModel&period;class&rpar;&semi;&NewLine;displayNoOfCoffee&lpar;mViewModel&period;orderedCoffee&rpar;&semi;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;008000&semi;">Step 3&colon; Use the ViewModel in your UI Controller<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>To access or change UI data&comma; you can now use the data in your ViewModel&period; Here’s an example of the new onCreate method and a method for updating the no of items &colon;<&sol;p>&NewLine;<pre>OrderViewModel mViewModel&semi;&NewLine;&commat;Override&NewLine;protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;btnIncreaseCoffee &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;increase&lowbar;coffee&rpar;&semi;&NewLine;btnDecreaseCoffee &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;decrease&lowbar;coffee&rpar;&semi;&NewLine;&NewLine;&NewLine;noOfCoffee &equals; &lpar;TextView&rpar; findViewById&lpar;R&period;id&period;tv&lowbar;no&lowbar;ofcoffee&rpar;&semi;&NewLine;mViewModel &equals; ViewModelProviders&period;of&lpar;this&rpar;&period;get&lpar;OrderViewModel&period;class&rpar;&semi;&NewLine;displayNoOfCoffee&lpar;mViewModel&period;orderedCoffee&rpar;&semi;&NewLine;public void displayNoOfCoffee&lpar;int coffee&rpar; &lbrace;&NewLine;&NewLine; noOfCoffee&period;setText&lpar;coffee &plus; ""&rpar;&semi;&NewLine;&rcub;&NewLine;&commat;Override&NewLine;public void onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; switch &lpar;view&period;getId&lpar;&rpar;&rpar; &lbrace;&NewLine; case R&period;id&period;btn&lowbar;increase&lowbar;coffee&colon; &lbrace;&NewLine; mViewModel&period;orderedCoffee &equals; mViewModel&period;orderedCoffee &plus; 1&semi;&NewLine; displayNoOfCoffee&lpar;mViewModel&period;orderedCoffee&rpar;&semi;&NewLine; break&semi;&NewLine; &rcub;&NewLine; case R&period;id&period;btn&lowbar;decrease&lowbar;coffee&colon; &lbrace;&NewLine; if &lpar;mViewModel&period;orderedCoffee &gt&semi; 0&rpar; &lbrace;&NewLine; mViewModel&period;orderedCoffee &equals; mViewModel&period;orderedCoffee - 1&semi;&NewLine; displayNoOfCoffee&lpar;mViewModel&period;orderedCoffee&rpar;&semi;&NewLine; &rcub;&NewLine; break&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>The first time the <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;arch&sol;lifecycle&sol;ViewModelProviders&period;html&num;of&percnt;28android&period;support&period;v4&period;app&period;Fragment&percnt;29" data-href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;arch&sol;lifecycle&sol;ViewModelProviders&period;html&num;of&lpar;android&period;support&period;v4&period;app&period;Fragment&rpar;">ViewModelProviders<&sol;a> <&sol;span><&sol;strong>method is called by MainActivity&comma; it creates a new ViewModel instance&period; When this method is called again&comma; which happens whenever onCreate is called&comma; it will return the pre-existing ViewModel associated with the specific <strong><span style&equals;"color&colon; &num;008000&semi;">ViewModelExample<&sol;span><&sol;strong> MainActivity&period; This is what preserves the data&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>1&period;In Android Studio&comma; go to <span style&equals;"color&colon; &num;008000&semi;"><strong>File <&sol;strong><strong>&Implies;<&sol;strong><strong> New Project<&sol;strong><&sol;span> and fill all the details required to create a new project&period; When it prompts to select a default activity&comma; select <span style&equals;"color&colon; &num;008000&semi;"><strong>Blank Activity<&sol;strong> <&sol;span>and proceed&period;<&sol;p>&NewLine;<p>2&period;Open <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span> and add ViewModel dependency&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Build&period;gradle<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>dependencies &lbrace;&NewLine; &NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;viewmodel dependency<&sol;strong><&sol;span>&NewLine; implementation "android&period;arch&period;lifecycle&colon;extensions&colon;1&period;0&period;0"&NewLine; annotationProcessor "android&period;arch&period;lifecycle&colon;compiler&colon;1&period;0&period;0"&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<p>3&period;  Open <strong><span style&equals;"color&colon; &num;008000&semi;">OrderViewModel&period;Java<&sol;span><&sol;strong> and write the below code&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;000080&semi;">OrderViewModel&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;viewmodelexample&semi;&NewLine;&NewLine;import android&period;arch&period;lifecycle&period;ViewModel&semi;&NewLine;&NewLine;public class OrderViewModel extends ViewModel &lbrace;&NewLine;&NewLine; public int orderedCoffee &equals; 0&semi;&NewLine;&NewLine; public int orderedTea &equals; 0&semi;&NewLine;&NewLine; public int orderedColddrink &equals; 0&semi;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<p>4&period;  Open <strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong> and write the below code&period;Here to increase or decrease items i have created increase and decrease button&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;000080&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 3&period;0&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;ScrollView xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"com&period;example&period;lenovo&period;viewmodelexample&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;orientation&equals;"vertical"&NewLine; android&colon;padding&equals;"8dp"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;gravity&equals;"center"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"15dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"15dp"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;text&equals;"Order what you would like to drink&quest;"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;blue&lowbar;dark"&NewLine; android&colon;textSize&equals;"22sp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;RelativeLayout&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;padding&equals;"10dp"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentLeft&equals;"true"&NewLine; android&colon;layout&lowbar;centerVertical&equals;"true"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;text&equals;"No&period; of coffee"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;light"&NewLine; android&colon;textSize&equals;"20sp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentRight&equals;"true"&NewLine; android&colon;layout&lowbar;centerVertical&equals;"true"&NewLine; android&colon;orientation&equals;"horizontal"&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;increase&lowbar;coffee"&NewLine; android&colon;layout&lowbar;width&equals;"40dp"&NewLine; android&colon;layout&lowbar;height&equals;"40dp"&NewLine; android&colon;text&equals;"&plus;" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;no&lowbar;ofcoffee"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;gravity&equals;"center"&NewLine; android&colon;padding&equals;"8dp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;decrease&lowbar;coffee"&NewLine; android&colon;layout&lowbar;width&equals;"40dp"&NewLine; android&colon;layout&lowbar;height&equals;"40dp"&NewLine; android&colon;text&equals;"-" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;RelativeLayout&gt&semi;&NewLine;&NewLine; &lt&semi;RelativeLayout&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;padding&equals;"10dp"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentLeft&equals;"true"&NewLine; android&colon;layout&lowbar;centerVertical&equals;"true"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;text&equals;"No&period; of tea"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;light"&NewLine; android&colon;textSize&equals;"20sp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentRight&equals;"true"&NewLine; android&colon;layout&lowbar;centerVertical&equals;"true"&NewLine; android&colon;orientation&equals;"horizontal"&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;increase&lowbar;tea"&NewLine; android&colon;layout&lowbar;width&equals;"40dp"&NewLine; android&colon;layout&lowbar;height&equals;"40dp"&NewLine; android&colon;text&equals;"&plus;" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;no&lowbar;oftea"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;gravity&equals;"center"&NewLine; android&colon;padding&equals;"8dp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;decrease&lowbar;tea"&NewLine; android&colon;layout&lowbar;width&equals;"40dp"&NewLine; android&colon;layout&lowbar;height&equals;"40dp"&NewLine; android&colon;text&equals;"-" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;RelativeLayout&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;RelativeLayout&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;padding&equals;"10dp"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentLeft&equals;"true"&NewLine; android&colon;layout&lowbar;centerVertical&equals;"true"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;text&equals;"No&period; of colddrink"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;light"&NewLine; android&colon;textSize&equals;"20sp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentRight&equals;"true"&NewLine; android&colon;layout&lowbar;centerVertical&equals;"true"&NewLine; android&colon;orientation&equals;"horizontal"&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;increase&lowbar;colddrink"&NewLine; android&colon;layout&lowbar;width&equals;"40dp"&NewLine; android&colon;layout&lowbar;height&equals;"40dp"&NewLine; android&colon;text&equals;"&plus;" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;no&lowbar;ofcolddrink"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;gravity&equals;"center"&NewLine; android&colon;padding&equals;"8dp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;decrease&lowbar;colddrink"&NewLine; android&colon;layout&lowbar;width&equals;"40dp"&NewLine; android&colon;layout&lowbar;height&equals;"40dp"&NewLine; android&colon;text&equals;"-" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;RelativeLayout&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;order"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;gravity&equals;"center"&NewLine; android&colon;layout&lowbar;marginTop&equals;"10dp"&NewLine; android&colon;text&equals;"order" &sol;&gt&semi;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&lt&semi;&sol;ScrollView&gt&semi;&NewLine;&NewLine;<&sol;pre>&NewLine;<p>5&period;  Open <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;Java<&sol;strong><&sol;span> and write the below code&period;Here I have set up connection between UI controller<br &sol;>&NewLine;and ViewModel and also perform read and write operation on viewmodel&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;000080&semi;">MainActivity&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;viewmodelexample&semi;&NewLine;&NewLine;import android&period;arch&period;lifecycle&period;ViewModelProviders&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;Button&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;import android&period;widget&period;Toast&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity implements View&period;OnClickListener &lbrace;&NewLine;&NewLine; Button btnIncreaseCoffee&comma; btnDecreaseCoffee&comma; btnIncreaseTea&comma; btnIncreaseColddrink&comma; btnDecreaseTea&comma; btnDecreaseColddrink&semi;&NewLine; Button btnOrder&semi;&NewLine; TextView noOfCoffee&comma; noOfTea&comma; noOfColddrink&semi;&NewLine; OrderViewModel mViewModel&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; btnIncreaseCoffee &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;increase&lowbar;coffee&rpar;&semi;&NewLine; btnDecreaseCoffee &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;decrease&lowbar;coffee&rpar;&semi;&NewLine;&NewLine; btnIncreaseTea &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;increase&lowbar;tea&rpar;&semi;&NewLine; btnDecreaseTea &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;decrease&lowbar;tea&rpar;&semi;&NewLine;&NewLine; btnIncreaseColddrink &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;increase&lowbar;colddrink&rpar;&semi;&NewLine; btnDecreaseColddrink &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;decrease&lowbar;colddrink&rpar;&semi;&NewLine;&NewLine; btnOrder &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;order&rpar;&semi;&NewLine;&NewLine; noOfCoffee &equals; &lpar;TextView&rpar; findViewById&lpar;R&period;id&period;tv&lowbar;no&lowbar;ofcoffee&rpar;&semi;&NewLine; noOfTea &equals; &lpar;TextView&rpar; findViewById&lpar;R&period;id&period;tv&lowbar;no&lowbar;oftea&rpar;&semi;&NewLine; noOfColddrink &equals; &lpar;TextView&rpar; findViewById&lpar;R&period;id&period;tv&lowbar;no&lowbar;ofcolddrink&rpar;&semi;&NewLine;&NewLine; &sol;&sol;adding listener&NewLine; btnIncreaseCoffee&period;setOnClickListener&lpar;this&rpar;&semi;&NewLine; btnDecreaseCoffee&period;setOnClickListener&lpar;this&rpar;&semi;&NewLine; btnIncreaseTea&period;setOnClickListener&lpar;this&rpar;&semi;&NewLine; btnDecreaseTea&period;setOnClickListener&lpar;this&rpar;&semi;&NewLine; btnIncreaseColddrink&period;setOnClickListener&lpar;this&rpar;&semi;&NewLine; btnDecreaseColddrink&period;setOnClickListener&lpar;this&rpar;&semi;&NewLine; btnOrder&period;setOnClickListener&lpar;this&rpar;&semi;&NewLine;&NewLine;&NewLine; mViewModel &equals; ViewModelProviders&period;of&lpar;this&rpar;&period;get&lpar;OrderViewModel&period;class&rpar;&semi;&NewLine; displayNoOfCoffee&lpar;mViewModel&period;orderedCoffee&rpar;&semi;&NewLine; displayNoOfTea&lpar;mViewModel&period;orderedTea&rpar;&semi;&NewLine; displayNoOfColddrink&lpar;mViewModel&period;orderedColddrink&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public void displayNoOfCoffee&lpar;int coffee&rpar; &lbrace;&NewLine; noOfCoffee&period;setText&lpar;coffee &plus; ""&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void displayNoOfTea&lpar;int tea&rpar; &lbrace;&NewLine; noOfTea&period;setText&lpar;tea &plus; ""&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void displayNoOfColddrink&lpar;int colddrink&rpar; &lbrace;&NewLine; noOfColddrink&period;setText&lpar;colddrink &plus; ""&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; switch &lpar;view&period;getId&lpar;&rpar;&rpar; &lbrace;&NewLine; case R&period;id&period;btn&lowbar;increase&lowbar;coffee&colon; &lbrace;&NewLine; mViewModel&period;orderedCoffee &equals; mViewModel&period;orderedCoffee &plus; 1&semi;&NewLine; displayNoOfCoffee&lpar;mViewModel&period;orderedCoffee&rpar;&semi;&NewLine; break&semi;&NewLine; &rcub;&NewLine; case R&period;id&period;btn&lowbar;decrease&lowbar;coffee&colon; &lbrace;&NewLine; if &lpar;mViewModel&period;orderedCoffee &gt&semi; 0&rpar; &lbrace;&NewLine; mViewModel&period;orderedCoffee &equals; mViewModel&period;orderedCoffee - 1&semi;&NewLine; displayNoOfCoffee&lpar;mViewModel&period;orderedCoffee&rpar;&semi;&NewLine; &rcub;&NewLine; break&semi;&NewLine; &rcub;&NewLine; case R&period;id&period;btn&lowbar;increase&lowbar;tea&colon; &lbrace;&NewLine; mViewModel&period;orderedTea &equals; mViewModel&period;orderedTea &plus; 1&semi;&NewLine; displayNoOfTea&lpar;mViewModel&period;orderedTea&rpar;&semi;&NewLine; break&semi;&NewLine; &rcub;&NewLine; case R&period;id&period;btn&lowbar;decrease&lowbar;tea&colon; &lbrace;&NewLine; if &lpar;mViewModel&period;orderedTea &gt&semi; 0&rpar; &lbrace;&NewLine; mViewModel&period;orderedTea &equals; mViewModel&period;orderedTea - 1&semi;&NewLine; displayNoOfTea&lpar;mViewModel&period;orderedTea&rpar;&semi;&NewLine; &rcub;&NewLine; break&semi;&NewLine; &rcub;&NewLine; case R&period;id&period;btn&lowbar;increase&lowbar;colddrink&colon; &lbrace;&NewLine; mViewModel&period;orderedColddrink &equals; mViewModel&period;orderedColddrink &plus; 1&semi;&NewLine; displayNoOfColddrink&lpar;mViewModel&period;orderedColddrink&rpar;&semi;&NewLine; break&semi;&NewLine; &rcub;&NewLine; case R&period;id&period;btn&lowbar;decrease&lowbar;colddrink&colon; &lbrace;&NewLine; if &lpar;mViewModel&period;orderedColddrink &gt&semi; 0&rpar; &lbrace;&NewLine; mViewModel&period;orderedColddrink &equals; mViewModel&period;orderedColddrink - 1&semi;&NewLine; displayNoOfColddrink&lpar;mViewModel&period;orderedColddrink&rpar;&semi;&NewLine; &rcub;&NewLine; break&semi;&NewLine; &rcub;&NewLine; case R&period;id&period;btn&lowbar;order&colon; &lbrace;&NewLine; Toast&period;makeText&lpar;this&comma; "&bsol;n" &plus; mViewModel&period;orderedCoffee &plus; " " &plus; "Coffee" &plus; "&bsol;n" &plus; mViewModel&period;orderedTea &plus; " " &plus; "Tea" &plus; "&bsol;n" &plus;&NewLine; mViewModel&period;orderedColddrink &plus; " " &plus; "Colddrink" &plus; "&bsol;n"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine; break&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>When you run the app it will look like this&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<figure id&equals;"attachment&lowbar;372" aria-describedby&equals;"caption-attachment-372" style&equals;"width&colon; 180px" class&equals;"wp-caption alignleft"><img class&equals;"wp-image-372 size-medium" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;04&sol;Screenshot&lowbar;2018-04-27-22-13-311-180x300&period;png" alt&equals;"" width&equals;"180" height&equals;"300" &sol;><figcaption id&equals;"caption-attachment-372" class&equals;"wp-caption-text">Vertical Orientation<&sol;figcaption><&sol;figure>&NewLine;<figure id&equals;"attachment&lowbar;371" aria-describedby&equals;"caption-attachment-371" style&equals;"width&colon; 300px" class&equals;"wp-caption alignnone"><img class&equals;"wp-image-371 size-medium" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;04&sol;Screenshot&lowbar;2018-04-27-22-14-261-300x180&period;png" alt&equals;"" width&equals;"300" height&equals;"180" &sol;><figcaption id&equals;"caption-attachment-371" class&equals;"wp-caption-text">Horizontal Orientation<&sol;figcaption><&sol;figure>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version