<?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 Marcin Kitowicz on Medium]]></title>
        <description><![CDATA[Stories by Marcin Kitowicz on Medium]]></description>
        <link>https://medium.com/@kitek?source=rss-5918eebc4ecb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dtubOgs_HQx6K39WNfhSoQ.jpeg</url>
            <title>Stories by Marcin Kitowicz on Medium</title>
            <link>https://medium.com/@kitek?source=rss-5918eebc4ecb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 23:13:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kitek/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[One card to rule them all]]></title>
            <link>https://medium.com/@kitek/one-card-to-rule-them-all-f2bb060a0b3d?source=rss-5918eebc4ecb------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2bb060a0b3d</guid>
            <category><![CDATA[cardview]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-ui]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Marcin Kitowicz]]></dc:creator>
            <pubDate>Tue, 16 Jan 2018 15:58:14 GMT</pubDate>
            <atom:updated>2018-01-22T15:54:23.195Z</atom:updated>
            <content:encoded><![CDATA[<h4>Or how to present items on one card in a recycler view</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D8j9FF-I_xfXOTKJ0axuhA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/5dB9WGpJbFc?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Thomas Schweighofer</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>The Material Design introduced many good ideas into the Android UI world. One it’s fundamental component is card view. Basically, it’s a rectangle layer with rounded corners and smooth shadow around, making it looking elevated a bit. In some cases, the use of the <em>CardView</em> may not be sufficient or even unnecessary. For example, placing many rows inside one card with recycler view can be tricky.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/0*qOD-DjtcS1OZ8yUM." /><figcaption>Items inside CardView</figcaption></figure><p>As we now know that we will have fixed items number, then we can just build a simple static layout for them. What if we don’t know exact items count or we know there might be a lot of items? This post describes how to use <em>RecyclerView</em>, a view’s property <em>“elevation”</em> and specially prepared backgrounds to presents many items on a card.</p><p><strong>Just a simple card list</strong></p><p>Creating a list containing cards with whatever you want content is quite simple. You just need to create an adapter, which one will handle creation and item view holder binding. For item’s layout, we’ll use <em>CardView</em> and… that’s it!</p><p><strong>Ideas and issues</strong></p><p>In our case, to achieve the presented result, we won’t use CardView component, because it’s not allowing us to set which corners should be rounded. For example: we would have rounded only top corners — the first item on the list.</p><p>Next idea could be placing a <em>CardView</em> as a single view in a RecyclerView and drawing rows as a subview of the card. This approach isn’t perfect as well because we prevent items from being recycled (from the <em>RecyclerView</em> perspective we have only one big item).</p><p>Another solution could be placing <em>RecylerView</em> inside <em>CardView</em> and setting the height to wrap content. Unfortunately, this makes items scrollable inside <em>CardView</em> instead of the whole screen.</p><p><strong>Better way</strong></p><p>My suggestion is to create item layout based on standard layout (<em>RelativeLayout</em> in our case) and set proper elevation to it. Of course, we also need to set a padding and “<a href="https://developer.android.com/reference/android/view/ViewGroup.html#setClipToPadding(boolean)"><em>clipToPadding</em></a>” flag to false on our <em>RecyclerView</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*nMAJgV3267HaMRiM6pL5pA.png" /><figcaption>First version — just elevation and paddings</figcaption></figure><p>Next step is to prepare four backgrounds for list items. Why four? We need a background for:</p><ul><li>the first item — rounded only top corners and bottom horizontal separator’s line,</li><li>the last item — rounded only bottom corners,</li><li>middle elements — just horizontal line,</li><li>single element — when there’s only one item on the list — rounded all corners.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*2165g0ctOpBDAwkD-Gr41Q.png" /><figcaption>Different backgrounds for items: first, last, middle, single</figcaption></figure><p>Prepared in that way backgrounds apply in <a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html"><em>onBindViewHolder</em></a> method depending on item position.</p><pre>fun bindBackground(itemView: View, position: Int, size: Int) {<br>    val drawableRes: Int = when {<br>        size == 1 -&gt; R.drawable.<em>item_top_bottom<br>        </em>position == 0 -&gt; R.drawable.<em>item_top<br>        </em>position == size - 1 -&gt; R.drawable.<em>item_bottom<br>        </em>else -&gt; R.drawable.<em>item_middle<br>    </em>}<br>    <strong>itemView.<em>background </em>= ContextCompat<br>            .getDrawable(itemView.<em>context</em>, drawableRes)</strong><br>    itemView.setPadding(contentPadding, contentPadding,<br>            contentPadding, contentPadding)<br>}</pre><p><strong>One more thing…</strong></p><p>It seems that everything is ready but when you take a closer look at items separator line you can notice strange graphical artefacts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*BxpSwNvHfUD68RncnceKRA.png" /><figcaption>Undesirable graphical artefacts</figcaption></figure><p>This is due to the overlap of the item’s shadow. To get rid of that we’ll set custom <a href="https://developer.android.com/reference/android/view/ViewOutlineProvider.html">outline provider</a>. We need to use <em>ViewOutlineProvider</em> class and override <em>getOutline</em> method with fixed shadow’s outline. We apply this provider for all items except the first one. For the first or single row we use <a href="https://developer.android.com/reference/android/view/ViewOutlineProvider.html#BACKGROUND"><em>ViewOutlineProvider.BACKGROUND</em></a><em>.</em></p><pre>override fun getOutline(view: View, outline: Outline) {<br>    val resources = view.<em>resources<br>    </em>outline.setRect(<br>            0,<br>            <strong>resources.getDimensionPixelSize(R.dimen.<em>cardElevation</em>)</strong>,<br>            view.<em>width</em>,<br>            view.<em>height<br>    </em>)<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eSC5H-9cdoEDgIHQg_cFog.gif" /><figcaption>Final result</figcaption></figure><p>As you might have noticed, using components provided with framework or library is not always the best option. Sometimes writing your own custom implementation can be really useful and very exciting.</p><p>Links:</p><ul><li><a href="https://github.com/kitek/android-rv-one-cardview">Source code on Github</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2bb060a0b3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RecyclerView swipe to delete easier than you thought]]></title>
            <link>https://medium.com/@kitek/recyclerview-swipe-to-delete-easier-than-you-thought-cff67ff5e5f6?source=rss-5918eebc4ecb------2</link>
            <guid isPermaLink="false">https://medium.com/p/cff67ff5e5f6</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[gesture-recognition]]></category>
            <category><![CDATA[recyclerview]]></category>
            <dc:creator><![CDATA[Marcin Kitowicz]]></dc:creator>
            <pubDate>Mon, 26 Jun 2017 07:42:26 GMT</pubDate>
            <atom:updated>2017-06-27T17:52:09.510Z</atom:updated>
            <content:encoded><![CDATA[<p>As is often the case, sometimes you need to enhance the functionality in the application using 3rd party libraries.</p><p>Of course, there’s nothing wrong with it:</p><ul><li>often such code has good quality,</li><li>you can save some time,</li><li>in general, reinventing the wheel is not a good idea.</li></ul><p>There is also the other side of the coin:</p><ul><li>open source is cool, but when you find a bug in the library, you need to fix it by yourself,</li><li>customisation might be cumbersome,</li><li>you have to accept library’s dependencies, even if you’re not using them,</li></ul><p>Last but not least, in my opinion, using a ready-made solution prevents us from discovering and learning new stuff.</p><p>It was similar in my case. Ability to delete rows from RecyclerView’s list was all that I needed. A quick look at the documentation and I realised that can be accomplished very easily!</p><h4><a href="https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.SimpleCallback.html">ItemTouchHelper.SimpleCallback</a></h4><p>Using this class, we can detect and react to events related to:</p><ul><li>swipe items (from right, left or both sides)</li><li>move items (up, down or both)</li></ul><p>Let’s create our implementation based on SimpleCallback <em>(to make code cleaner I placed only the most important fragments, check </em><a href="https://github.com/kitek/android-rv-swipe-delete/blob/master/app/src/main/java/pl/kitek/rvswipetodelete/SwipeToDeleteCallback.kt"><em>full code)</em></a>:</p><pre>abstract class SwipeToDeleteCallback(context: Context) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.<em>LEFT</em>) {<br><br>    override fun onMove(...): Boolean {<br>        return false // We don&#39;t want support moving items up/down<br>    }<br><br>    // Let&#39;s draw our delete view<br>    override fun onChildDraw(canvas, recyclerView, viewHolder, ..) {<br>        val itemView = viewHolder.<em>itemView<br>        </em>val itemHeight = itemView.<em>bottom </em>- itemView.<em>top<br><br>        </em>// Draw the red delete background<br>        background.<em>color </em>= backgroundColor<br>        background.setBounds(<br>                itemView.<em>right </em>+ dX.toInt(),<br>                itemView.<em>top</em>,<br>                itemView.<em>right</em>,<br>                itemView.<em>bottom<br>        </em>)<br>        background.draw(canvas)<br><br>        // Calculate position of delete icon<br>        val iconTop = itemView.<em>top </em>+ (itemHeight - inHeight) / 2<br>        val iconMargin = (itemHeight - inHeight) / 2<br>        val iconLeft = itemView.<em>right </em>- iconMargin - inWidth<br>        val iconRight = itemView.<em>right </em>- iconMargin<br>        val iconBottom = iconTop + inHeight<br><br>        // Draw the delete icon<br>        icon.setBounds(iconLeft, iconTop, iconRight, iconBottom)<br>        icon.draw(canvas)<br>        <br>        super.onChildDraw(canvas, recyclerView, viewHolder, ...)<br>    }<br>}</pre><p>SimpleCallback’s constructor is using two parameters — which directions we want to support, in our case just swipe from right to left. In onChildDraw method we need to manually calculate and draw background with icon (I used <em>ic_delete</em> from <a href="https://material.io/icons/">Material icons</a>).</p><p>Next thing is to connect Callback with our <a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html">RecyclerView</a>:</p><pre><em>recyclerView</em>.<em>layoutManager </em>= LinearLayoutManager(context)<em><br>recyclerView</em>.<em>adapter </em>= SimpleAdapter()<br><br>val swipeHandler = object : SwipeToDeleteCallback(context) {<br>    override fun onSwiped(...) {<br>        val adapter = <em>recyclerView</em>.<em>adapter </em>as SimpleAdapter<br>        adapter.removeAt(viewHolder.<em>adapterPosition</em>)<br>    }<br>}</pre><pre>val itemTouchHelper = ItemTouchHelper(swipeHandler)<br>itemTouchHelper.attachToRecyclerView(<em>recyclerView</em>)</pre><p>Finally, the last thing is ability to delete item in <a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html">adapter</a>. We can achieve this by doing that:</p><pre>class SimpleAdapter : RecyclerView.Adapter&lt;SimpleAdapter.VH&gt;() {<br><br>    // ...<br>    <br>    fun removeAt(position: Int) {<br>        items.removeAt(position)<br>        notifyItemRemoved(position)<br>    }<br>}</pre><p>That’s it! Thanks to this quick implementation you can add cool feature in your application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*junhn88r59SSVUu1muHkcQ.gif" /><figcaption>Swipe to remove</figcaption></figure><p>Links:</p><ul><li><a href="https://github.com/kitek/android-rv-swipe-delete">Full code</a></li><li><a href="https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.Callback.html">ItemTouchHelper.Callback</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cff67ff5e5f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fall in love with Spock]]></title>
            <link>https://medium.com/@kitek/feel-in-love-with-spock-a923fc6db4a7?source=rss-5918eebc4ecb------2</link>
            <guid isPermaLink="false">https://medium.com/p/a923fc6db4a7</guid>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[groovy]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[spock]]></category>
            <dc:creator><![CDATA[Marcin Kitowicz]]></dc:creator>
            <pubDate>Tue, 14 Feb 2017 18:58:16 GMT</pubDate>
            <atom:updated>2017-02-14T19:44:24.802Z</atom:updated>
            <content:encoded><![CDATA[<p>Just another awesome test framework…</p><p><strong>Super simple</strong></p><p>It uses Groovy language which tries to be as natural as possible for Java developers. It means it’s very expressive, easy to learn, has no explicit types, no semicolons… and it works with Java and Kotlin :)</p><p><strong>Test names</strong></p><p>Creating names for tests to describe correctly what we’re testing is really hard. Writing it as very long camel case string doesn’t look nice. In Spock, we can use strings as tests names.</p><pre>def &quot;Creates message with content and submission date&quot;() {</pre><pre>}</pre><p><strong>Given When Then</strong></p><p>Spock provides: given, when, then sections by default. In a simple way, we can separate preparation, execution, and assertion.</p><pre>def &quot;Message cannot be canceled twice&quot;() {<br>    given: &quot;Any message&quot;<br>    def message = <em>anyMessage</em>()<br>    message.cancel()<br><br>    when: &quot;cancel message 2nd time&quot;<br>    message.cancel()<br><br>    then: &quot;the error should occur&quot;<br>    thrown(MessageStateChangeException)<br>    message.getState() == <em>CANCELED<br></em>}</pre><p>What’s more, in each block we can add <a href="http://spockframework.org/spock/docs/1.0/spock_primer.html#specs-as-doc">additional information</a> used in <a href="https://github.com/renatoathaydes/spock-reports">reports</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2oyYdvIVm8CJ5eZEMXSQ4g.jpeg" /><figcaption>HTML report generated by Spock Reports Extension</figcaption></figure><p><strong>Assertions</strong></p><p>In Spock, we don’t need to explicitly call <em>assertSomething(...).</em> Each line in the “<em>then:</em>” block is treated as another assertion.</p><pre>then:<br>message.isCanceled()<br>1 + 1 == 3<br>1 * remoteClient.send(message)</pre><p>In cases where invocation order matters we need to provide multiple <em>“then:”</em> blocks:</p><pre>then:<br>message.state == <em>CANCELED<br></em>then:<br>message.isCanceled()</pre><p>When assertions fail errors are presented like this:</p><pre>Condition not satisfied:<br><br>1 + 1 == 3<br>|   |<br>2   false<br><br>Expected :3<br><br>Actual   :2</pre><p><strong>Mocking and stubbing</strong></p><p>Providing mocks is also really simple. With right-shift (<a href="http://spockframework.org/spock/docs/1.0/interaction_based_testing.html#_stubbing"><em>&gt;&gt;</em></a>)<em> </em>operator we can easily return fixed value. If mocked method requires an argument we can put _ to pass any value.</p><pre>def mockMessage = Mock(Message)<br>def mockRemoteClient = Mock(RemoteClient)<br><br>mockMessage.isCanceled() &gt;&gt; true<br>mockRemoteClient.send(_) &gt;&gt; { throw new IllegalStateException(&quot;&quot;) }</pre><p><strong>More</strong></p><p>That’s only beginning, you can find more features and examples in <a href="http://spockframework.org/spock/docs/1.1-rc-3/index.html">official Spock documentation</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a923fc6db4a7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>