<?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 Shivam Chopra on Medium]]></title>
        <description><![CDATA[Stories by Shivam Chopra on Medium]]></description>
        <link>https://medium.com/@shivamdev31?source=rss-e82c5e52eb7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZZUNYpBMDdj6Y_-CGRt6KA.jpeg</url>
            <title>Stories by Shivam Chopra on Medium</title>
            <link>https://medium.com/@shivamdev31?source=rss-e82c5e52eb7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 17:04:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shivamdev31/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[Observe SharedPreferences changes using RxJava]]></title>
            <link>https://proandroiddev.com/observe-sharedpreferences-changes-using-rxjava-f0f3b43c6329?source=rss-e82c5e52eb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0f3b43c6329</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[rxjava2]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Shivam Chopra]]></dc:creator>
            <pubDate>Sun, 19 May 2019 10:30:21 GMT</pubDate>
            <atom:updated>2019-05-19T10:30:21.319Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*wCRvD5vuR2i63cU0DjbySQ.png" /><figcaption>SharedPreferences using RxJava and Kotlin</figcaption></figure><p>Hope you have gone through my previous articles <a href="https://medium.com/mindorks/recyclerview-item-click-using-rxjava2-993fc0c78047">RecyclerView item click using RxJava</a> and <a href="https://medium.com/mindorks/auto-viewpager-slider-using-rxjava2-fb1da80878bf">Auto ViewPager slider using RxJava</a>.</p><p>Today I’m back with another article about how you can create a RxJava wrapper over SharedPreferences to observe the changes in your <strong>SharedPreference </strong>values. I won’t be using any architecture for this sample application as that is out of the scope of this article.</p><p>If you just want to have a quick peek at the code and don’t want to read this article then jump over to the same project <a href="https://github.com/ShivamDev31/RxPrefs">here</a>. Let&#39;s get started…</p><p>The sample application looks something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xb-GKRMshQbxTJ4q3cVyaQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/253/1*uY9pQWkuvIHnSUR_ADdFGQ.gif" /><figcaption>The end result</figcaption></figure><p>Now let&#39;s jump into the code.</p><p>First, let’s look into the <strong>NameRepository </strong>where all the magic is happening.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0a3202371fb295ba4a83ad311283249/href">https://medium.com/media/e0a3202371fb295ba4a83ad311283249/href</a></iframe><p>This is just a contract interface which <strong>SharedPreferencesNameRepository </strong>will implement. Now let’s see what is happening in <strong>SharedPreferencesNameRepository.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fe2c04c4b2f4852b34011a8b456ff1a/href">https://medium.com/media/7fe2c04c4b2f4852b34011a8b456ff1a/href</a></iframe><p>I know its too much to understand in one glance so I’ll try to break it into smaller parts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5fcd90b0e82e179069523720fdc3bb5/href">https://medium.com/media/d5fcd90b0e82e179069523720fdc3bb5/href</a></iframe><p>Firstly we are creating a static create(context: Context) method which helps in the creation of <strong>SharedPreferencesNameRepository</strong>. Inside create we are creating the <strong>SharedPreferences</strong> and passing it over to the constructor of <strong>SharedPreferencesNameRepository</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0deb56ca515568d835e993252883450b/href">https://medium.com/media/0deb56ca515568d835e993252883450b/href</a></iframe><p>Then I’m creating a <a href="http://reactivex.io/rxjs/manual/overview.html#behaviorsubject">BehaviorSubject</a> with the default SharedPreferences we have got from the constructor.</p><p>After that, I create a <strong>SharedPreferences.OnSharedPreferenceChangeListener </strong>which gives me the updated <strong>SharedPreferences </strong>whenever the value in <strong>SharedPreferences </strong>changes.</p><p>And then I set the <strong>PreferenceChangeListener </strong>to the default <strong>SharedPreferences</strong> which was passed in the constructor in the <strong>init</strong> block.</p><p>So now we have completed the setup of <strong>SharedPreferencesNameRepository </strong>and now we will implement the methods from <strong>NameRepository</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/142cda27c581bc4720d951bb816e679e/href">https://medium.com/media/142cda27c581bc4720d951bb816e679e/href</a></iframe><p>The first one is <strong>saveName()</strong> which takes in a String and returns a <a href="http://reactivex.io/RxJava/javadoc/io/reactivex/Completable.html">Completable</a>. Then we convert our Subject(prefSubject) to a <a href="http://reactivex.io/RxJava/javadoc/io/reactivex/Single.html">Single</a> using <a href="http://reactivex.io/RxJava/javadoc/io/reactivex/Observable.html#firstOrError--">firstOrError()</a> and then I’ve created a Kotlin extension function over it to edit the shared preference value and save it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91aaf924511a38ce8eda59eb47a27681/href">https://medium.com/media/91aaf924511a38ce8eda59eb47a27681/href</a></iframe><p>Then we have the <strong>name()</strong> which returns an <a href="http://reactivex.io/RxJava/javadoc/io/reactivex/Observable.html">Observable</a> and whenever the SharedPreference value changes our Subject(prefSubject) emits the updated <strong>SharedPreferences </strong>and we get the latest saved name from this method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73b6bc93130d0978339ee5568055401f/href">https://medium.com/media/73b6bc93130d0978339ee5568055401f/href</a></iframe><p>Then we have <strong>clear()</strong> at the last. This method like saveName() calls the extension function on the <strong>prefSubject </strong>and removes the name from <strong>SharedPreferences</strong>.</p><p>That&#39;s it for Repository. The main stuff is in the initialization of the repository rest changes as per your requirements.</p><p>Now, let&#39;s discuss how we are handling the data in MainActivity. This is a simple activity class which takes the view references and set listeners on the views.</p><p>The first thing we do is create a reference for <strong>NameRepository</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4946f545f0d5ccbefd0a187679e0a865/href">https://medium.com/media/4946f545f0d5ccbefd0a187679e0a865/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4b2d26f9e1e9399cade78a9c248e393/href">https://medium.com/media/a4b2d26f9e1e9399cade78a9c248e393/href</a></iframe><p>We are observing the fields in <strong>handleData() </strong>when the <strong>Submit </strong>button is clicked. Firstly we are getting the text from the EditText and then calling the <strong>repository.saveName()</strong> with the text and then subscribing to it as its a <strong>Completable</strong>.</p><p>Then we are observing over the saved text by subscribing to <strong>repository.name() </strong>which will emit a value whenever the value in shared preferences changes.</p><p>At last, when the <strong>Clear </strong>button is clicked we are removing the saved text from SharedPreferences.</p><p>If anyone would like to help me to convert this into a library then drop me a message at <strong>shivamdev31@gmail.com</strong></p><p>So that’s it for this article, find the code on github: <a href="https://github.com/ShivamDev31/RxPrefs">RxPrefs</a>.</p><p>Constructive feedback is welcomed in the comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0f3b43c6329" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/observe-sharedpreferences-changes-using-rxjava-f0f3b43c6329">Observe SharedPreferences changes using RxJava</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RecyclerView item click using RxJava2]]></title>
            <link>https://medium.com/mindorks/recyclerview-item-click-using-rxjava2-993fc0c78047?source=rss-e82c5e52eb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/993fc0c78047</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[rxandroid]]></category>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[rxjava]]></category>
            <dc:creator><![CDATA[Shivam Chopra]]></dc:creator>
            <pubDate>Thu, 22 Jun 2017 08:50:18 GMT</pubDate>
            <atom:updated>2018-08-16T02:26:13.860Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey Folks,</p><p>First of all thanks for liking my previous article <a href="https://blog.mindorks.com/auto-viewpager-slider-using-rxjava2-fb1da80878bf">Auto ViewPager slider using RxJava2</a>. If you haven’t read that article then I would highly recommend to read that article.</p><p>In this article I’ll tell you guys how you can set a click listener on <strong>RecyclerView</strong> items using <strong>RxJava2</strong>. I’ve seen many of the developers are using RxJava in their Android project but still they are using interfaces(which I won’t cover in this article) to interact between their <strong>RecyclerView</strong> <strong>adapter</strong> and <strong>View</strong>(Activity/Fragment).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/264/1*I4alJqfZjwBn7cJrUUpfuA.gif" /><figcaption>Demo of sample project</figcaption></figure><p>So I thought of writing this article to explain how you guys can use <strong>RxJava</strong> to listen to the click events of your <strong>RecyclerView</strong>. I’ve written all the code using <strong>Kotlin </strong>and I’ve just started using Kotlin so please comment below if you find any issues in the code or the code can be optimised more.</p><p>So guys to interact between our view and recyclerview adapter I’ve used RxJava <strong>Subjects</strong>. Now what is a Subject, as per RxJava wiki :</p><blockquote>A <a href="http://reactivex.io/RxJava/javadoc/rx/subjects/Subject.html">Subject</a> is a sort of bridge or proxy that acts both as an Subscriber and as an Observable. Because it is a Subscriber, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by reemitting them, and it can also emit new items.</blockquote><p>So as you have seen a Subject acts both as <strong>Subscriber</strong> and an <strong>Observable</strong>. So what we will be doing is in our adapter our <strong>Subject</strong> will act as an <strong>Observable</strong> and in our view we will subscribe to this subject so it will work as a <strong>Subscriber</strong> in our view.</p><p>Now there are multiple types of Subjects available in RxJava which you can checkout <a href="http://reactivex.io/documentation/subject.html">here</a>. For our use case we will be using <strong>PublishSubject</strong>. So what is a <strong>PublishSubject</strong>, lets get back to the RxJava wiki for its definition.</p><blockquote>Subject that, once an <a href="http://reactivex.io/RxJava/javadoc/rx/Observer.html">Observer</a> has subscribed, emits all subsequently observed items to the subscriber.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zqwv1JV61tLJSg5b.png" /><figcaption>Source: RxJava wiki</figcaption></figure><p>So now we know what we will be using so lets get to the fun part(show me some code). So lets start with our RecyclerView <strong>Adapter</strong>:</p><blockquote>private val clickSubject = PublishSubject.create&lt;String&gt;()</blockquote><p>In our adapter I create a global variable of <strong>PublishSubject&lt;String&gt;</strong> which will emit <strong>String</strong> items. So in our <strong>ViewHolder</strong> class where we setup our click listener on an item we will call this <strong>clickSubject </strong>and call <strong>.onNext(item) </strong>on it. So whenever a click event comes our subject will emit the event.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a64303f2a7355339da0100145076a4b/href">https://medium.com/media/7a64303f2a7355339da0100145076a4b/href</a></iframe><p>Then we will create a method/function which will return an Observable&lt;String&gt; like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93a1273dc4c98f0112fe3005afba911e/href">https://medium.com/media/93a1273dc4c98f0112fe3005afba911e/href</a></iframe><p>So here our adapter work id done. Now we will move to our view and call this <strong>getClickListener()</strong> function on our adapter reference and subscribe to this <strong>Observable&lt;String&gt; </strong>like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e812c5df7f8242463170f408ff292daa/href">https://medium.com/media/e812c5df7f8242463170f408ff292daa/href</a></iframe><p>So guys that’s it for this tutorial and you can find the sample project on github : <a href="https://github.com/ShivamDev31/RxRecyclerItemClick">RxRecyclerItemClick</a>.</p><p>Hope you liked this tutorial. Constructive feedback is most welcomed.</p><p><strong>Check out all the top articles at </strong><a href="https://blog.mindorks.com/"><strong>blog.mindorks.com</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=993fc0c78047" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mindorks/recyclerview-item-click-using-rxjava2-993fc0c78047">RecyclerView item click using RxJava2</a> was originally published in <a href="https://medium.com/mindorks">MindOrks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Auto ViewPager slider using RxJava2]]></title>
            <link>https://medium.com/mindorks/auto-viewpager-slider-using-rxjava2-fb1da80878bf?source=rss-e82c5e52eb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb1da80878bf</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[rxandroid]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[rxjava]]></category>
            <dc:creator><![CDATA[Shivam Chopra]]></dc:creator>
            <pubDate>Thu, 15 Jun 2017 17:40:30 GMT</pubDate>
            <atom:updated>2018-08-16T02:24:53.252Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey folks,</p><p>This is my first article on medium and in this article I’ll tell you how to automate the viewpager so that it automatically slides to next screen after a predefined interval. And for this task I’ll use RxJava’s operator <a href="http://reactivex.io/documentation/operators/interval.html"><strong>interval</strong></a><strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/266/1*WQ70200whhHvYtOO_Krm9g.gif" /><figcaption>End result will look like this</figcaption></figure><p>So as per the RxJava documentation an <strong>interval</strong> is :</p><blockquote>create an Observable that emits a sequence of integers spaced by a given time interval</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xKPUtkFkoT-kcLfV.png" /><figcaption>Source : RxJava wiki</figcaption></figure><blockquote>The Interval operator returns an Observable that emits an infinite sequence of ascending integers, with a constant interval of time of your choosing between emissions.</blockquote><p>The <strong>interval</strong> operator takes in 2 parameters.</p><blockquote>interval(time, TimeUnit)</blockquote><p>So in this sample project I’ll have an activity which will contain a <strong>ViewPager </strong>and I’ll be using <strong>RxJava2</strong> for automatic scroll and <strong>Glide</strong> for image loading for random images URL.</p><p>Mostly in application <strong>OnBoarding </strong>we use ViewPagers where we want to guide the user about the application using different images/screens. In those cases we need that the images should slide automatically.</p><p>If you don’t want to go through all the boring stuff then the project link is available at the end of the post.</p><p>If we have to do it by using only android components then we can do it like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80926ab0e707580b50a58b2c850d6c1a/href">https://medium.com/media/80926ab0e707580b50a58b2c850d6c1a/href</a></iframe><p>In the above snippet we have created a <strong>Handler </strong>and providing it with a <strong>Runnable </strong>and then we are calling <strong>postDelayed </strong>on the <strong>Handler </strong>every <strong>2 seconds </strong>so that it executes the same <strong>Runnable</strong> code again and again.</p><p>So now we have seen how to implement this by using default android components. Now we will see how to implement the same feature using RxJava’s <strong>interval </strong>operator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/653854c42aeabba96db25ae6eb2defef/href">https://medium.com/media/653854c42aeabba96db25ae6eb2defef/href</a></iframe><p>Please note that you should explicitly execute this code on <strong>observeOn(AndroidSchedulers.mainThread()) </strong>else you will get :</p><blockquote>Caused by: java.lang.IllegalStateException: Must be called from main thread of fragment host</blockquote><p>Don’t forget to unsubscribe/dispose your subscription/disposable. And implement <strong>onError </strong>incase you want to know the cause of your crash.</p><p>So that’s it for today you can find the sample project on github : <a href="https://github.com/ShivamDev31/RxViewPagerScroll">RxViewPagerScroll</a></p><p><strong>Check out all the top articles at </strong><a href="https://blog.mindorks.com/"><strong>blog.mindorks.com</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb1da80878bf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mindorks/auto-viewpager-slider-using-rxjava2-fb1da80878bf">Auto ViewPager slider using RxJava2</a> was originally published in <a href="https://medium.com/mindorks">MindOrks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>