<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Karthik Ramachandran on Medium]]></title>
        <description><![CDATA[Stories by Karthik Ramachandran on Medium]]></description>
        <link>https://medium.com/@karthikr1493?source=rss-4947121dbbd7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*nIN3yRrFYnACyPXd</url>
            <title>Stories by Karthik Ramachandran on Medium</title>
            <link>https://medium.com/@karthikr1493?source=rss-4947121dbbd7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 03:14:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@karthikr1493/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SmartVision — AI enabled mobile assistant for visually impaired & challenged individuals]]></title>
            <link>https://medium.com/google-cloud/smartvision-ai-enabled-mobile-assistant-for-visually-impaired-challenged-individuals-061bc5f5260a?source=rss-4947121dbbd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/061bc5f5260a</guid>
            <category><![CDATA[build-and-blog-marathon]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Karthik Ramachandran]]></dc:creator>
            <pubDate>Fri, 13 Dec 2024 10:00:20 GMT</pubDate>
            <atom:updated>2024-12-19T01:01:34.336Z</atom:updated>
            <content:encoded><![CDATA[<h3>SmartVision — AI enabled mobile assistant for visually impaired &amp; challenged individuals</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*NKBe3ud2IhL4NuaYZDKdOg.png" /></figure><p><strong>How to build an AI enabled mobile assistant for visually impaired and challenged individuals</strong></p><p><strong>Introduction/Overview</strong></p><p>In a world that revolves around sight, the visually impaired often face unique challenges that can hinder their ability to navigate and experience the world with confidence and independence. However, the relentless pursuit of technological innovation has given rise to the SmartVision App, designed specifically to empower and enhance the lives of the visually impaired.</p><p>The target audience for this blog is for anyone who wants to learn how to integrate Gemini API with an android app and also use firebase with Google ML-Kit(Barcode scanning API).(<strong>Note</strong> : Prerequisite knowledge of building android apps in which you can create few screen using activities and fragment is necessary).</p><p>By the end of this blog post you will learn to build an app with few use cases which are beneficial for visually impaired and challenged individuals.</p><p>The use cases that we will build are given below:</p><p><strong>1)</strong> <strong>Detect objects</strong> : By using this feature a visually impaired user can detect obstacles from a WiFi enabled camera(ESP32 cam module) placed on the smart cap. Images captured using the cam module will be wirelessly sent to android app &amp; Objects present in the image will be described by Gemini-2.0-Flash API and the announced to the visually impaired user using Text to Speech API. I am using Gemini Flash because it is faster and thus has less response time compared to Gemini Pro.</p><p><strong>2) Scan Products</strong>: In this feature, the user has to scan the barcode of a product using the on-device camera and the product details corresponding to the product will be fetched from the Firestore database and then announced to the user using Text to Speech API.</p><p><strong>Design</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*5hEWnzjt_DZN--_DdVj_mg.png" /><figcaption><strong>Note :</strong> For the scan products feature the visually impaired user will have to focus the smartphone’s camera on the barcode or QR-code of a product( ESP32 cam module is not necessary for this feature). Only for detecting objects feature the ESP32 cam is to be used.</figcaption></figure><h3>Prerequisites</h3><p><strong>Necessary software</strong></p><ul><li>Android Studio (I used android studio Koala. Feel free to use latest stable version of android studio)</li><li>Gemini API Key from Google AI studio</li><li>A wireless cam(preferably ESP32 cam module) such that it generates an image url.</li><li>Firebase should be configured for storing product details(using Firestore database).</li></ul><p><strong>Note : </strong>1.It is assumed that the ESP32 cam module is already set up such that it generates an http image url. I will not cover the cam module setup in this blog post.</p><p>2. Also, as mentioned before, prior knowledge of building android apps is necessary where you create simple screens using either XML or Jetpack compose.</p><h3>Step-by-step instructions</h3><ol><li><strong>Detect Objects </strong>: In the section, the thought process to build the detect objects feature of SmartVision is explained. You need to build a settings screen with Text Fields(in android TextFields are known as EditText) to set the image url exposed by the ESP32 cam module and prompt text to describe the image. Also, write code to store these values in SharedPreferences so that you can retrieve it from other screens throughout the app. (Ignore, the other things like minimum threshold and mode-1 and mode-2 in the screenshot given below.)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/0*XR-L58PXcp-Bcarc" /></figure><p>Next, build a screen with two options Detect Objects and Scan Product option as given below. This will serve as your home screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/292/1*8_lHOumXWolumcxO8kiptQ.png" /><figcaption>Please ignore other use cases like Reading Mode, Summarize docs &amp; Detect Faces. These use cases are outside the scope of this blog</figcaption></figure><p>Once the user taps on the Detect objects menu item from the home screen, build a new screen so that the user sees a detect object screen as shown below. Images captured from the ESP32-cam module should be downloaded and displayed on the app using http image url and later on sent to Gemini API (Use Gemini-2.0-flash model) along with prompt text for getting the image description. The image description will be later on announced to the user using Text-to-Speech API. (Also, the cam module and phone should be in the same network so preferably turn on the hotspot on the phone and connect the cam module to the hotspot.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*3GGqGst9OzYUidgwRycAww.png" /><figcaption>Screenshot above shows the objects captured by the ESP32 cam module and image description provided by the Gemini API.</figcaption></figure><p>2. <strong>Scan Products </strong>:</p><p>In the next blog post I will show you how to build a Scan Products feature. So stay tuned.(I have already completed it and looks like the one given below.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*_yqbYSXHhjPgNv_tajBXFA.png" /><figcaption>A book is scanned in the screenshot given above.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dEwlh_-XQffqZuz4uO7tOA.png" /><figcaption>Firestore database has details of the product</figcaption></figure><h3>Result / Demo</h3><ul><li>Detect Objects use case is built using Gemini API.</li><li>Explore the live demo here of detect objects here: <a href="https://www.youtube.com/watch?v=DWTzKNwl3GM">SmartVision</a></li></ul><h3>What’s next</h3><ul><li>One additional idea I have is if an android smartphone supports on device Gemini API like Gemini Nano, then we can avoid latency created due to sending the request and receiving the response back.</li><li>Also, stay tuned for the blog post related to scan products.</li></ul><h3>Code Repository</h3><p>You can find the code repository below :<br><a href="https://github.com/karthik-r14/SmartVisionBuildnBlog">https://github.com/karthik-r14/SmartVisionBuildnBlog</a></p><h3>Call to Action</h3><p>You can connect with me on LinkedIn : <a href="http://linkedin.com/in/karthik-ramachandran-kr">Karthik Ramachandran</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=061bc5f5260a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/smartvision-ai-enabled-mobile-assistant-for-visually-impaired-challenged-individuals-061bc5f5260a">SmartVision — AI enabled mobile assistant for visually impaired &amp; challenged individuals</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building  a BMI computing Android App using Kotlin.]]></title>
            <link>https://medium.com/@karthikr1493/building-a-bmi-computing-android-app-using-kotlin-6088858dec17?source=rss-4947121dbbd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/6088858dec17</guid>
            <category><![CDATA[bmi-calculator]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android-jetpack]]></category>
            <category><![CDATA[mvvm-databinding]]></category>
            <dc:creator><![CDATA[Karthik Ramachandran]]></dc:creator>
            <pubDate>Sat, 05 Sep 2020 15:45:37 GMT</pubDate>
            <atom:updated>2020-09-05T15:45:37.725Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Building a BMI computing Android App using Kotlin.</strong></h3><p>In this blog, we will cover how to make an android app which computes <strong>Body Mass Index(BMI)</strong> and create a BMI table<strong> to visualize</strong> the BMI</p><p>For this project we will use <strong>Android Studio 4.0</strong> and above.</p><p>This blog post assumes that you know how to setup a project using android studio with an empty activity.</p><p><strong>Choice of Programming Language </strong>: <strong>Kotlin.</strong></p><p>Traditionally, Java was used for building android apps.But now, Kotlin is the official language for Android Development.</p><p><strong>Concepts used to build this app</strong>:</p><p>This blog post makes use of the following the following concepts of Android.</p><ol><li>Activities &amp; Lifecycle</li><li>MVVM (Model-View-ViemModel) Architecture</li><li>Live Data</li><li>Explicit Intent</li><li>Data Binding</li><li>View Model Factory</li><li>Recycler view</li></ol><p>These concepts are explained here briefly.</p><p><strong>Activities &amp; Lifecycle</strong></p><p>An activity is a single screen of user interface(UI).It comprises of an .xml file for coding the UI and a .java/.kt file to control the interaction of the UI.For instance you can have click handlers to a button and implement a functionality like lauching a new activtity upon click of a button.</p><p>As far as activity lifecycle is concerned, there are several methods are there like onCreate(), onStart() etc. And as a programmer we must understand the order of invocation of these methods.</p><p><strong>MVVM architecture : </strong>The <strong>Model-View-ViewModel</strong> architecture is officially endorsed by google for building Android apps.This is explained below briefly.</p><figure><img alt="MVVM-Architecture" src="https://cdn-images-1.medium.com/max/653/1*8Yj_OHVPckJX--Jr6K1Mkg.png" /></figure><p><strong>View </strong>: Activity or Fragment (This part is also known as UI controller)</p><p><strong>ViewModel</strong> :</p><p>It holds data for the UI.</p><p>It survives configuration change.For instance when the device is rotated or user changes language settings of the app.</p><p>It never contains reference to activities, fragments or views.It communicates back to the UI controller using <strong>LiveData </strong>(It is an observable data holder class that is lifecycle aware.LiveData updates UI controller only when focus is on screen.This is known as Lifecycle aware).</p><p><strong>Model </strong>: The repository which is responsible for fetching data either from a remote data source or from a local database is known as the model.</p><p><strong>Explicit Intent : </strong>This is used to launch a new activity and can be used pass data to another activity.<strong>Intent</strong> in general is known as a <strong>messaging object</strong> in android.</p><p>But with the advent of Navigation Components<strong> </strong>which is part of Android Jetpack, this way of transitioning from one activity to another is not recommended.But for simplicity I will use an explicit intent to move from one activity to another.</p><p>(There is an implict intent as well which I won’t be talking about in this blog post.)</p><p><strong>Data Binding :</strong> The findViewById() is an expensive operation.(As an alternative people started using Butterknife library, which is also outdated now a days) But now people use Data Binding (which is also part of android Jetpack) which allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programatically.</p><p><strong>ViewModel Factory</strong> : If we want to pass some data to our view model class then we cannot pass it directly to its constructor. This data can be passed using a ViewModelFactory class to the ViewModel.</p><p><strong>Recycler View </strong>: It is an advanced of form of listview and offers improved performance with other benefits. For implementing a recycler view we need to specify a layout manager, adapter, viewholder.Using Layout manager we can arrange the recycler view elements vertically or horizontally or even in a staggered manner.We will discuss about adapter and viewholder when we are implementing a recycler view to create a bmi status table.</p><p><strong>The implementation of the BMI app is divided into three parts :</strong></p><ol><li><strong>Computation of BMI.</strong></li><li><strong>Populating Computed BMI value in new Activity</strong></li><li><strong>Creating BMI Table using Recycler View</strong></li></ol><p>So lets get started with the first part then which is implementation of BMI functionality.</p><h3><strong>Part 1 : Computation of BMI</strong></h3><p>Enable databinding in app level build.gradle file using the following code :</p><pre>android {<br>    ...<br>    buildFeatures {<br>        dataBinding true<br>    }<br>}</pre><p>Now, lets start with xml.Assuming that you created an activity with the name MainActivity Place the xml code given below in activity_main.xml file.</p><p>activity_main.xml</p><pre><em>&lt;?</em><strong><em>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</em></strong><em>?&gt;</em><br>&lt;<strong>layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</strong><br>    <strong>xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;</strong>&gt;<br>    &lt;<strong>androidx.constraintlayout.widget.ConstraintLayout xmlns:tools=&quot;http://schemas.android.com/tools&quot;</strong><br>        <strong>android:layout_width=&quot;match_parent&quot;</strong><br>        <strong>android:layout_height=&quot;match_parent&quot;</strong><br>        <strong>android:background=&quot;@android:color/holo_blue_light&quot;</strong><br>        <strong>tools:context=&quot;.view.MainActivity&quot;</strong>&gt;<br>        &lt;<strong>TextView</strong><br>            <strong>android:id=&quot;@+id/bmi_calculator_title&quot;</strong><br>            <strong>android:layout_width=&quot;wrap_content&quot;</strong><br>            <strong>android:layout_height=&quot;wrap_content&quot;</strong><br>            <strong>android:text=&quot;@string/bmi_calculator_title&quot;</strong><br>            <strong>android:textColor=&quot;@android:color/white&quot;</strong><br>            <strong>android:textSize=&quot;36sp&quot;</strong><br>            <strong>android:textStyle=&quot;bold&quot;</strong><br>            <strong>app:layout_constraintBottom_toBottomOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintLeft_toLeftOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintRight_toRightOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintTop_toTopOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintVertical_bias=&quot;0.15&quot; </strong>/&gt;<br>        &lt;<strong>EditText</strong><br>            <strong>android:id=&quot;@+id/weight_edit_text&quot;</strong><br>            <strong>android:layout_width=&quot;wrap_content&quot;</strong><br>            <strong>android:layout_height=&quot;wrap_content&quot;</strong><br>            <strong>android:autofillHints=&quot;Weight&quot;</strong><br>            <strong>android:hint=&quot;@string/weight_hint&quot;</strong><br>            <strong>android:inputType=&quot;numberDecimal&quot;</strong><br>            <strong>android:textColorHint=&quot;@android:color/black&quot;</strong><br>            <strong>android:textSize=&quot;30sp&quot;</strong><br>            <strong>app:layout_constraintBottom_toBottomOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintLeft_toLeftOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintRight_toRightOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintTop_toBottomOf=&quot;@id/bmi_calculator_title&quot;</strong><br>            <strong>app:layout_constraintVertical_bias=&quot;0.15&quot; </strong>/&gt;<br>        &lt;<strong>EditText</strong><br>            <strong>android:id=&quot;@+id/height_edit_text&quot;</strong><br>            <strong>android:layout_width=&quot;wrap_content&quot;</strong><br>            <strong>android:layout_height=&quot;wrap_content&quot;</strong><br>            <strong>android:autofillHints=&quot;Height&quot;</strong><br>            <strong>android:hint=&quot;@string/height_hint&quot;</strong><br>            <strong>android:inputType=&quot;numberDecimal&quot;</strong><br>            <strong>android:textColorHint=&quot;@android:color/black&quot;</strong><br>            <strong>android:textSize=&quot;30sp&quot;</strong><br>            <strong>app:layout_constraintBottom_toBottomOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintLeft_toLeftOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintRight_toRightOf=&quot;parent&quot;</strong><br>          <strong>app:layout_constraintTop_toBottomOf=&quot;@id/weight_edit_text&quot;</strong><br>            <strong>app:layout_constraintVertical_bias=&quot;0.15&quot; </strong>/&gt;<br>        &lt;<strong>Button</strong><br>            <strong>android:id=&quot;@+id/compute_bmi_button&quot;</strong><br>            <strong>android:layout_width=&quot;wrap_content&quot;</strong><br>            <strong>android:layout_height=&quot;wrap_content&quot;</strong><br>            <strong>android:background=&quot;@android:color/holo_orange_dark&quot;</strong><br>            <strong>android:padding=&quot;1dp&quot;</strong><br>            <strong>android:text=&quot;@string/compute_bmi_button_text&quot;</strong><br>            <strong>android:textAllCaps=&quot;false&quot;</strong><br>            <strong>android:textColor=&quot;@android:color/white&quot;</strong><br>            <strong>android:textSize=&quot;30sp&quot;</strong><br>            <strong>android:textStyle=&quot;bold&quot;</strong><br>            <strong>app:layout_constraintBottom_toBottomOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintLeft_toLeftOf=&quot;parent&quot;</strong><br>            <strong>app:layout_constraintRight_toRightOf=&quot;parent&quot;</strong><br>         <strong>app:layout_constraintTop_toBottomOf=&quot;@id/height_edit_text&quot;</strong><br>            <strong>app:layout_constraintVertical_bias=&quot;0.25&quot; </strong>/&gt;<br>    &lt;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>&gt;<br>&lt;/<strong>layout</strong>&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/253/1*yJylfw6x-OxFgEXsr9zdVA.png" /></figure><p>The above xml code above will get resolved into a UI similar to the above design.</p><p>Now, lets take a look at the Activity and Viewmodel code for the Main Screen.</p><p>MainActivity.kt</p><pre><strong>package com.karthikr.bmi_application.view</strong></pre><pre><strong>import </strong>android.content.Intent<br><strong>import </strong>android.os.Bundle<br><strong>import </strong>android.widget.Toast<br><strong>import </strong>android.widget.Toast.<em>LENGTH_LONG</em><br><strong>import </strong>androidx.appcompat.app.AppCompatActivity<br><strong>import </strong>androidx.databinding.DataBindingUtil<br><strong>import </strong>androidx.lifecycle.Observer<br><strong>import </strong>androidx.lifecycle.ViewModelProviders<br><strong>import </strong>com.karthikr.bmi_application.R<br><strong>import </strong>com.karthikr.bmi_application.databinding.ActivityMainBinding<br><strong>import </strong>com.karthikr.bmi_application.viewmodel.MainViewModel</pre><pre><strong>class </strong>MainActivity : AppCompatActivity() {<br>    <strong>private lateinit var binding</strong>: ActivityMainBinding<br>    <strong>private lateinit var viewModel</strong>: MainViewModel<br>    <strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {<br>        <strong>super</strong>.onCreate(savedInstanceState)<br>        <strong>binding </strong>= DataBindingUtil.setContentView(<br>            <strong>this</strong>,<br>            R.layout.<em>activity_main</em><br>        )<br>        <strong>viewModel </strong>= ViewModelProviders.of(<strong>this</strong>).get(MainViewModel::<strong>class</strong>.<em>java</em>)<br>        <strong>binding</strong>.<strong>computeBmiButton</strong>.setOnClickListener <strong>{</strong><br>            <strong>viewModel</strong>.computeBmi(<br>                <strong>binding</strong>.<strong>weightEditText</strong>.<em>text</em>.toString(),<br>                <strong>binding</strong>.<strong>heightEditText</strong>.<em>text</em>.toString()<br>            )<br>        <strong>}</strong><br>        <strong>viewModel</strong>.<strong>validationMessageLiveData</strong>.observe(<strong>this</strong>, <em>Observer </em><strong>{ </strong>errorMessage <strong>-&gt;</strong><br>            showToastMessage(errorMessage)<br>        <strong>}</strong>)<br>        <strong>viewModel</strong>.<strong>bmiLiveData</strong>.observe(<strong>this</strong>, <em>Observer </em><strong>{ </strong>bmiValue <strong>-&gt;</strong><br>            launchChartActivity(bmiValue)<br>        <strong>}</strong>)<br>    }<br>    <strong>private fun </strong>showToastMessage(message: String?) {<br>        Toast.makeText(<br>            <em>applicationContext</em>,<br>            message,<br>            <em>LENGTH_LONG</em><br>        ).show()<br>    }<br>    <strong>private fun </strong>launchChartActivity(bmiValue: Double) {<br>        <strong>val </strong>intent = Intent(<strong>this</strong>@MainActivity, BmiStatusActivity::<strong>class</strong>.<em>java</em>)<br>        intent.putExtra(<strong>&quot;BMI_Value&quot;</strong>, bmiValue)<br>        startActivity(intent)<br>    }<br>}</pre><pre><br>Note that in the above code databinding is being implemented instead of findviewbyid.Also we are making use of an explicit intent to launch a new Activity in this case it is BmiStatusActivity.We will create this actitivity in the next section.</pre><p>MainViewModel.kt</p><pre><strong>package </strong>com.karthikr.bmi_application.viewmodel</pre><pre><strong>import </strong>androidx.lifecycle.MutableLiveData<br><strong>import </strong>androidx.lifecycle.ViewModel<br><strong>import </strong>java.lang.Math.round<br><strong>import </strong>kotlin.math.pow</pre><pre><strong>class </strong>MainViewModel : ViewModel() {<br>    <strong>var bmiLiveData </strong>= MutableLiveData&lt;Double&gt;()<br>    <strong>var validationMessageLiveData </strong>= MutableLiveData&lt;String&gt;()<br>    <br>    <strong>fun </strong>computeBmi(weight: String, height: String) {<br>        <strong>var </strong>computedBmi: Double<br>        <strong>if </strong>(weight.<em>trim</em>().<em>isEmpty</em>() || height.<em>trim</em>().<em>isEmpty</em>()) {<br>            <strong>validationMessageLiveData</strong>.<em>value </em>= <strong>&quot;The input field(s) should not be left blank&quot;</strong><br>        } <strong>else </strong>{<br>            <strong>val </strong>numerator = weight.<em>toFloat</em>()<br>            <strong>val </strong>denominator = (height.<em>toFloat</em>() * 0.01).<em>pow</em>(2.0)<br>            computedBmi = numerator / denominator<br>            computedBmi = round(computedBmi * 10.0) / 10.0<br>            <strong>bmiLiveData</strong>.<em>value </em>= computedBmi<br>        }<br>    }</pre><p>In the above code for viewmodel, we validate the input fields for which we have written a simple if statement.Also in the else case we compute the actual BMI using the formula given below<br>BMI = weight(in Kg) / height(in metres) * height(in metres)<br>Also, the BMI is rounded off to 2 decimal places.</p><p>Note that for simplicity this viewmodel doesn&#39;t comprise of any Repository as there is no need for local storage or REST API call to a remote server.</p><p>Once the BMI is computed using the above formula, we set this value to the live data object and the observer code in the activity gets triggered which in turn lauches a BmiStatusActivity using an explicit intent.We pass the computed bmi value to this activity using intent.putextra method as shown in the code above.</p><h3><strong>Part 2</strong> <strong>: Populating Computed BMI value in new Activity</strong></h3><p>Now, lets look at the xml of BmiStatusActivity.Place the xml code given below in activity_bmi_status.xml file.</p><p>activity_bmi_status.xml</p><pre><em>&lt;?</em><strong>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</strong><em>?&gt;</em><br>&lt;<strong>layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</strong><br>    <strong>xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;</strong>&gt;<br>    &lt;<strong>androidx.core.widget.NestedScrollView</strong><br>        <strong>android:layout_width=&quot;match_parent&quot;</strong><br>        <strong>android:layout_height=&quot;match_parent&quot;</strong>&gt;<br>        &lt;<strong>androidx.constraintlayout.widget.ConstraintLayout xmlns:tools=&quot;http://schemas.android.com/tools&quot;</strong><br>            <strong>android:layout_width=&quot;match_parent&quot;</strong><br>            <strong>android:layout_height=&quot;match_parent&quot;</strong><br>            <strong>android:background=&quot;@android:color/holo_blue_light&quot;</strong><br>            <strong>android:focusable=&quot;true&quot;</strong><br>            <strong>android:focusableInTouchMode=&quot;true&quot;</strong><br>            <strong>tools:context=&quot;.view.BmiStatusActivity&quot;</strong>&gt;<br>            &lt;<strong>TextView</strong><br>                <strong>android:id=&quot;@+id/bmi_value_title&quot;</strong><br>                <strong>android:layout_width=&quot;wrap_content&quot;</strong><br>                <strong>android:layout_height=&quot;wrap_content&quot;</strong><br>                <strong>android:text=&quot;@string/bmi_value_title&quot;</strong><br>                <strong>android:textAllCaps=&quot;true&quot;</strong><br>                <strong>android:textColor=&quot;@android:color/white&quot;</strong><br>                <strong>android:textSize=&quot;40sp&quot;</strong><br>                <strong>android:textStyle=&quot;bold&quot;</strong><br>                <strong>app:layout_constraintBottom_toTopOf=&quot;@id/bmi_value&quot;</strong><br>                <strong>app:layout_constraintEnd_toEndOf=&quot;parent&quot;</strong><br>                <strong>app:layout_constraintStart_toStartOf=&quot;parent&quot;</strong><br>                <strong>app:layout_constraintTop_toTopOf=&quot;parent&quot;</strong><br>                <strong>app:layout_constraintVertical_bias=&quot;0.2&quot;</strong><br>                <strong>app:layout_constraintVertical_chainStyle=&quot;packed&quot; </strong>/&gt;<br>            &lt;<strong>TextView</strong><br>                <strong>android:id=&quot;@+id/bmi_value&quot;</strong><br>                <strong>android:layout_width=&quot;wrap_content&quot;</strong><br>                <strong>android:layout_height=&quot;wrap_content&quot;</strong><br>                <strong>android:layout_marginTop=&quot;10dp&quot;</strong><br>                <strong>android:background=&quot;@android:color/holo_orange_dark&quot;</strong><br>                <strong>android:paddingStart=&quot;20dp&quot;</strong><br>                <strong>android:paddingTop=&quot;30dp&quot;</strong><br>                <strong>android:paddingEnd=&quot;20dp&quot;</strong><br>                <strong>android:paddingBottom=&quot;30dp&quot;</strong><br>                <strong>android:text=&quot;@string/bmi_sample_value&quot;</strong><br>                <strong>android:textColor=&quot;@android:color/white&quot;</strong><br>                <strong>android:textSize=&quot;35sp&quot;</strong><br>                <strong>app:layout_constraintBottom_toTopOf=&quot;@id/bmi_values_table&quot;</strong><br>                <strong>app:layout_constraintEnd_toEndOf=&quot;parent&quot;</strong><br>                <strong>app:layout_constraintStart_toStartOf=&quot;parent&quot;</strong><br>                <strong>app:layout_constraintTop_toBottomOf=&quot;@id/bmi_value_title&quot;</strong><br>                <strong>app:layout_constraintVertical_chainStyle=&quot;packed&quot; </strong>/&gt;<br><br>            &lt;<strong>androidx.recyclerview.widget.RecyclerView</strong><br>                <strong>android:id=&quot;@+id/bmi_values_table&quot;</strong><br>                <strong>android:layout_width=&quot;match_parent&quot;</strong><br>                <strong>android:layout_height=&quot;wrap_content&quot;</strong><br>                <strong>android:layout_marginTop=&quot;20dp&quot;</strong><br>                <strong>app:layout_constraintBottom_toBottomOf=&quot;parent&quot;</strong><br>                <strong>app:layout_constraintEnd_toEndOf=&quot;parent&quot;</strong><br>                <strong>app:layout_constraintStart_toStartOf=&quot;parent&quot;</strong><br>                <strong>app:layout_constraintTop_toBottomOf=&quot;@id/bmi_value&quot;</strong><br>                <strong>app:layout_constraintVertical_chainStyle=&quot;packed&quot; </strong>/&gt;<br>        &lt;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>&gt;<br>    &lt;/<strong>androidx.core.widget.NestedScrollView</strong>&gt;<br>&lt;/<strong>layout</strong>&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*Shln7mzdSaY1WhoQcPyiCA.png" /></figure><p>The above xml code above will get resolved into a UI similar to the above design.</p><p>Now lets take a look at the code of BmiStatusActivity and Bmi StatusViewModel adn BmiStatusViewModelFactory</p><p>BmiStatusActivity.kt</p><pre><strong>package </strong>com.karthikr.bmi_application.view</pre><pre><strong>import </strong>android.os.Bundle<br><strong>import </strong>androidx.appcompat.app.AppCompatActivity<br><strong>import </strong>androidx.databinding.DataBindingUtil<br><strong>import </strong>androidx.lifecycle.Observer<br><strong>import </strong>androidx.lifecycle.ViewModelProviders<br><strong>import </strong>androidx.recyclerview.widget.LinearLayoutManager<br><strong>import </strong>com.karthikr.bmi_application.R<br><strong>import </strong>com.karthikr.bmi_application.adapter.BmiValuesListAdapter<br><strong>import </strong>com.karthikr.bmi_application.databinding.ActivityBmiStatusBinding<br><strong>import </strong>com.karthikr.bmi_application.factory.BmiStatusActivityViewModelFactory<br><strong>import </strong>com.karthikr.bmi_application.viewmodel.BmiStatusViewModel<br><br><strong>class </strong>BmiStatusActivity : AppCompatActivity() {<br>    <strong>private lateinit var binding</strong>: ActivityBmiStatusBinding<br>    <strong>private lateinit var viewModel</strong>: BmiStatusViewModel<br>    <strong>private var computedBmiValue</strong>: Double = 0.0<br>    <strong>private lateinit var viewModelFactory</strong>: BmiStatusActivityViewModelFactory<br><br>    <strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {<br>        <strong>super</strong>.onCreate(savedInstanceState)<br>        <strong>binding </strong>= DataBindingUtil.setContentView(<strong>this</strong>, R.layout.<em>activity_bmi_status</em>)<br>        <strong>computedBmiValue </strong>= <em>intent</em>.getDoubleExtra(<strong>&quot;BMI_Value&quot;</strong>, 0.0)<br>        <strong>binding</strong>.<strong>bmiValue</strong>.<em>text </em>= <strong>computedBmiValue</strong>.toString()<br>        <strong>binding</strong>.<strong>bmiValuesTable</strong>.<em>layoutManager </em>= LinearLayoutManager(<strong>this</strong>)<br>        <strong>viewModelFactory </strong>= BmiStatusActivityViewModelFactory(<strong>computedBmiValue</strong>)<br>        <strong>viewModel </strong>=<br>            ViewModelProviders.of(<strong>this</strong>, <strong>viewModelFactory</strong>).get(BmiStatusViewModel::<strong>class</strong>.<em>java</em>)<br>        <strong>viewModel</strong>.<strong>bmiValuesListLiveData</strong>.observe(<strong>this</strong>, <em>Observer </em><strong>{ </strong>bmiValuesList <strong>-&gt;</strong><br>            <strong>binding</strong>.<strong>bmiValuesTable</strong>.<em>adapter </em>=<br>                BmiValuesListAdapter(<em>applicationContext</em>, bmiValuesList)<br>            (<strong>binding</strong>.<strong>bmiValuesTable</strong>.<em>adapter </em><strong>as </strong>BmiValuesListAdapter).notifyDataSetChanged()<br>        <strong>}</strong>)<br>    }<br>}</pre><p>BmiStatusViewModel.kt</p><pre><strong>package </strong>com.karthikr.bmi_application.viewmodel<br><strong>import </strong>androidx.lifecycle.MutableLiveData<br><strong>import </strong>androidx.lifecycle.ViewModel<br><strong>import </strong>com.karthikr.bmi_application.model.BmiItem<br><br><strong>class </strong>BmiStatusViewModel(<strong>val computedBmiValue</strong>: Double) : ViewModel() {<br>    <strong>var bmiValuesListLiveData </strong>= MutableLiveData&lt;MutableList&lt;BmiItem&gt;&gt;()<br>    <strong>init </strong>{<br>        <strong>bmiValuesListLiveData</strong>.<em>value </em>= computeBmiStatus()<br>    }<br>    <strong>private fun </strong>computeBmiStatus(): MutableList&lt;BmiItem&gt; {<br>        <strong>var </strong>bmiStatusList = getBmiStatusList()<br>        <strong>when </strong>(<strong>computedBmiValue</strong>) {<br>            <strong>in </strong>0.0..14.9 -&gt; bmiStatusList[0].<strong>isHighlighted </strong>= <strong>true</strong><br>            <strong>in </strong>15.0..16.0 -&gt; bmiStatusList[1].<strong>isHighlighted </strong>= <strong>true</strong><br>            <strong>in </strong>16.1..18.4 -&gt; bmiStatusList[2].<strong>isHighlighted </strong>= <strong>true</strong><br>            <strong>in </strong>18.5..24.9 -&gt; bmiStatusList[3].<strong>isHighlighted </strong>= <strong>true</strong><br>            <strong>in </strong>25.0..29.9 -&gt; bmiStatusList[4].<strong>isHighlighted </strong>= <strong>true</strong><br>            <strong>in </strong>30.0..34.9 -&gt; bmiStatusList[5].<strong>isHighlighted </strong>= <strong>true</strong><br>            <strong>in </strong>35.0..39.9 -&gt; bmiStatusList[6].<strong>isHighlighted </strong>= <strong>true</strong><br>            <strong>else </strong>-&gt; {<br>                bmiStatusList[7].<strong>isHighlighted </strong>= <strong>true</strong><br>            }<br>        }<br>        <strong>return </strong>bmiStatusList<br>    }<br>    <strong>private fun </strong>getBmiStatusList(): MutableList&lt;BmiItem&gt; {<br>        <strong>val </strong>bmiStatusList = <em>mutableListOf</em>&lt;BmiItem&gt;()<br>        bmiStatusList.add(BmiItem(<strong>&quot;Less than 15.0&quot;</strong>, <strong>&quot;Very severely underweight&quot;</strong>))<br>        bmiStatusList.add(BmiItem(<strong>&quot;15.0 - 16.0&quot;</strong>, <strong>&quot;Severely underweight&quot;</strong>))<br>        bmiStatusList.add(BmiItem(<strong>&quot;16.1 - 18.4&quot;</strong>, <strong>&quot;Underweight&quot;</strong>))<br>        bmiStatusList.add(BmiItem(<strong>&quot;18.5 - 24.9&quot;</strong>, <strong>&quot;Normal&quot;</strong>))<br>        bmiStatusList.add(BmiItem(<strong>&quot;25.0 - 29.9&quot;</strong>, <strong>&quot;Overweight&quot;</strong>))<br>        bmiStatusList.add(BmiItem(<strong>&quot;30.0 - 34.9&quot;</strong>, <strong>&quot;Obese Class I (Moderately Obese)&quot;</strong>))<br>        bmiStatusList.add(BmiItem(<strong>&quot;35.0 - 39.9&quot;</strong>, <strong>&quot;Obese Class II (Severely Obese)&quot;</strong>))<br>        bmiStatusList.add(BmiItem(<strong>&quot;Above 40&quot;</strong>, <strong>&quot;Obese Class III (Very Severely Obese)&quot;</strong>))<br>        <strong>return </strong>bmiStatusList<br>    }<br>}</pre><p>Note there is a bmi value that is passed to the BmiStatusviewmodel.We cannot directly pass values to the viewModel construutor. This is achieved using ViewModelFactory.The code for it is given below.</p><p>The code for the view model factoy is given below.</p><p>BmiStatusActivityViewModelFactory.kt</p><pre><strong>package </strong>com.karthikr.bmi_application.factory<br><strong>import </strong>androidx.lifecycle.ViewModel<br><strong>import </strong>androidx.lifecycle.ViewModelProvider<br><strong>import </strong>com.karthikr.bmi_application.viewmodel.BmiStatusViewModel</pre><pre><strong>class </strong>BmiStatusActivityViewModelFactory(<strong>private val computedBmiValue</strong>: Double) :<br>    ViewModelProvider.Factory {<br>    <strong>override fun </strong>&lt;T : ViewModel?&gt; create(modelClass: Class&lt;T&gt;): T {<br>        <strong>if </strong>(modelClass.isAssignableFrom(BmiStatusViewModel::<strong>class</strong>.<em>java</em>)) {<br>            <strong>return </strong>BmiStatusViewModel(<strong>computedBmiValue</strong>) <strong>as </strong>T<br>        }<br>        <strong>throw </strong>IllegalArgumentException(<strong>&quot;Unknown View Model Class&quot;</strong>)<br>    }<br>}</pre><h3><strong>Part 3 : Creating BMI Table using Recycler View</strong></h3><p>Now, let have a look at the code for recycler view adapter and the xml template of how every item in the recycler view list should appear.</p><p>bmi_list.xml</p><pre><em>&lt;?</em><strong>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</strong><em>?&gt;</em><br>&lt;<strong>androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</strong><br>    <strong>xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;</strong><br>    <strong>android:id=&quot;@+id/list_item&quot;</strong><br>    <strong>android:layout_width=&quot;match_parent&quot;</strong><br>    <strong>android:layout_height=&quot;wrap_content&quot;</strong><br>    <strong>android:layout_margin=&quot;2dp&quot;</strong><br>    <strong>android:padding=&quot;4dp&quot;</strong>&gt;<br>    &lt;<strong>TextView</strong><br>        <strong>android:id=&quot;@+id/bmi_index&quot;</strong><br>        <strong>android:layout_width=&quot;100dp&quot;</strong><br>        <strong>android:layout_height=&quot;wrap_content&quot;</strong><br>        <strong>android:text=&quot;@string/bmi_index&quot;</strong><br>        <strong>android:textColor=&quot;@android:color/white&quot;</strong><br>        <strong>android:textSize=&quot;15sp&quot;</strong><br>        <strong>android:textStyle=&quot;bold&quot;</strong><br>        <strong>app:layout_constraintBottom_toBottomOf=&quot;parent&quot;</strong><br>        <strong>app:layout_constraintEnd_toStartOf=&quot;@id/bmi_status&quot;</strong><br>        <strong>app:layout_constraintStart_toStartOf=&quot;parent&quot;</strong><br>        <strong>app:layout_constraintTop_toBottomOf=&quot;parent&quot; </strong>/&gt;<br>    &lt;<strong>TextView</strong><br>        <strong>android:id=&quot;@+id/bmi_status&quot;</strong><br>        <strong>android:layout_width=&quot;200dp&quot;</strong><br>        <strong>android:layout_height=&quot;wrap_content&quot;</strong><br>        <strong>android:text=&quot;@string/bmi_status&quot;</strong><br>        <strong>android:textColor=&quot;@android:color/white&quot;</strong><br>        <strong>android:textSize=&quot;15sp&quot;</strong><br>        <strong>android:textStyle=&quot;bold&quot;</strong><br>        <strong>app:layout_constraintBottom_toBottomOf=&quot;parent&quot;</strong><br>        <strong>app:layout_constraintEnd_toEndOf=&quot;parent&quot;</strong><br>        <strong>app:layout_constraintStart_toEndOf=&quot;@id/bmi_index&quot;</strong><br>        <strong>app:layout_constraintTop_toBottomOf=&quot;parent&quot; </strong>/&gt;<br>&lt;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>&gt;</pre><p>BmiItem Model</p><pre><strong>package </strong>com.karthikr.bmi_application.model</pre><pre><strong>data class </strong>BmiItem(<strong>val bmiIndex</strong>: String, <strong>val bmiStatus</strong>: String, <strong>var isHighlighted</strong>: Boolean = <strong>false</strong>)</pre><p>BMIValuesListAdapter.kt</p><pre><strong>package </strong>com.karthikr.bmi_application.adapter<br><strong>import </strong>android.content.Context<br><strong>import </strong>android.view.LayoutInflater<br><strong>import </strong>android.view.View<br><strong>import </strong>android.view.ViewGroup<br><strong>import </strong>androidx.recyclerview.widget.RecyclerView<br><strong>import </strong>com.karthikr.bmi_application.R<br><strong>import </strong>com.karthikr.bmi_application.model.BmiItem<br><strong>import </strong>kotlinx.android.synthetic.main.bmi_list_item.view.*</pre><pre><strong>class </strong>BmiValuesListAdapter(<br>    <strong>private val context</strong>: Context,<br>    <strong>private val bmiItemList</strong>: List&lt;BmiItem&gt;<br>) : RecyclerView.Adapter&lt;MyViewHolder&gt;() {<br>    <strong>override fun </strong>onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {<br>        <strong>val </strong>layoutInflater = LayoutInflater.from(parent.<em>context</em>)<br>        <strong>val </strong>listItem = layoutInflater.inflate(R.layout.<em>bmi_list_item</em>, parent, <strong>false</strong>)<br>        <strong>return </strong>MyViewHolder(<strong>context</strong>, listItem)<br>    }<br>    <strong>override fun </strong>getItemCount(): Int {<br>        <strong>return bmiItemList</strong>.<strong>size</strong><br>    }<br>    <strong>override fun </strong>onBindViewHolder(holder: MyViewHolder, position: Int) {<br>        holder.bind(<strong>bmiItemList</strong>[position])<br>    }<br>}</pre><pre><strong>class </strong>MyViewHolder(<strong>val context</strong>: Context, <strong>val view</strong>: View) : RecyclerView.ViewHolder(view) {<br>    <strong>fun </strong>bind(bmiItem: BmiItem) {<br>        <strong>view</strong>.bmi_index.<em>text </em>= bmiItem.<strong>bmiIndex</strong><br>        <strong>view</strong>.bmi_status.<em>text </em>= bmiItem.<strong>bmiStatus</strong><br>        <strong>if </strong>(bmiItem.<strong>isHighlighted</strong>) {<br>            <strong>view</strong>.list_item.setBackgroundColor(<br>                <strong>context</strong>.getResources().getColor(android.R.color.<em>holo_orange_dark</em>)<br>            )<br>        }<br>    }<br>}</pre><p>The above Adapter class is responsible for loading the values passed to its constructor into the recycler view. It also comprises of a ViewHolder class which is describes an item view and metadata about its place within the RecyclerView.</p><p>The Strings used in this project are stored in strings.xml and its content is given below.</p><p>Strings.xml</p><pre>&lt;<strong>resources</strong>&gt;<br>    &lt;<strong>string name=&quot;app_name&quot;</strong>&gt;BMI-Application&lt;/<strong>string</strong>&gt;<br>    &lt;<strong>string name=&quot;bmi_calculator_title&quot;</strong>&gt;BMI Calculator&lt;/<strong>string</strong>&gt;<br>    &lt;<strong>string name=&quot;weight_hint&quot;</strong>&gt;Enter your weight(in Kg)&lt;/<strong>string</strong>&gt;<br>    &lt;<strong>string name=&quot;height_hint&quot;</strong>&gt;Enter your height(in cm)&lt;/<strong>string</strong>&gt;<br>    &lt;<strong>string name=&quot;compute_bmi_button_text&quot;</strong>&gt;Compute BMI&lt;/<strong>string</strong>&gt;<br>    &lt;<strong>string name=&quot;bmi_value_title&quot;</strong>&gt;BMI Value&lt;/<strong>string</strong>&gt;<br>    &lt;<strong>string name=&quot;bmi_sample_value&quot;</strong>&gt;21.5&lt;/<strong>string</strong>&gt;<br>    &lt;<strong>string name=&quot;bmi_index&quot;</strong>&gt;BMI INDEX&lt;/<strong>string</strong>&gt;<br>    &lt;<strong>string name=&quot;bmi_status&quot;</strong>&gt;BMI STATUS&lt;/<strong>string</strong>&gt;<br>&lt;/<strong>resources</strong>&gt;</pre><p>Now, run the app and see how it looks.The screenshots of the app are given below:</p><figure><img alt="BMI_NORMAL" src="https://cdn-images-1.medium.com/max/422/1*cGzTe3e9BSzU-Jrg2Jr4RQ.png" /></figure><figure><img alt="BMI_UNDERWEIGHT" src="https://cdn-images-1.medium.com/max/418/1*4UgETIti1Q0v1fgLAh2IUQ.png" /></figure><figure><img alt="BMI_OVERWEIGHT" src="https://cdn-images-1.medium.com/max/412/1*elEDAUaapaVkjDGdk9eYUA.png" /></figure><p>So based on the computed BMI value the corresponding BMI range and description gets highlighted.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6088858dec17" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>