<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Krzysztof Balana</title>
    <description>The latest articles on DEV Community by Krzysztof Balana (@krzysztofbalana).</description>
    <link>https://dev.to/krzysztofbalana</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F132630%2F44e8b3f9-ab69-4360-b880-da9a5497b527.jpeg</url>
      <title>DEV Community: Krzysztof Balana</title>
      <link>https://dev.to/krzysztofbalana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/krzysztofbalana"/>
    <language>en</language>
    <item>
      <title>Creating responsive “Buttons Bar” widget in Android (part 1)</title>
      <dc:creator>Krzysztof Balana</dc:creator>
      <pubDate>Tue, 29 Jan 2019 06:47:50 +0000</pubDate>
      <link>https://dev.to/krzysztofbalana/creating-responsive-buttons-bar-widget-in-android-part-1-180c</link>
      <guid>https://dev.to/krzysztofbalana/creating-responsive-buttons-bar-widget-in-android-part-1-180c</guid>
      <description>&lt;p&gt;Today’s web developers have plenty tools to build modern, fast and (most of all) responsive web pages. But when it comes to Android, things are not so bright. We’re tightly bounded to what our native “system” has to offer. And there is not so much of responsivness there ;)&lt;/p&gt;

&lt;h1&gt;
  
  
  The Problem
&lt;/h1&gt;

&lt;p&gt;Recently I’ve had to do some custom ui, which will act accordingly to different language translations and changing numbers of elements inside. Believe me, one which looks good in english, can get pretty extended in other languages.&lt;/p&gt;

&lt;p&gt;Below, you can find an example ui visualisation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3zzP1TzP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/m2zyoihtimave0165r8v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3zzP1TzP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/m2zyoihtimave0165r8v.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Everything looks great! We have slick icons, great labels, tremendeous white spaces…but what if someone would like to use longer names, of more than three buttons? And what about smaller phones screens?&lt;/p&gt;

&lt;p&gt;We could use LinearLayout’s weight approach, with some TextView’s autosizing and try to divide elements accordingly, but without knowing theirs initial width, we could end like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---9rcmVIM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/eeublctxa3afacxw3xlj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---9rcmVIM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/eeublctxa3afacxw3xlj.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some will say: “ Success. Everything is distributed evenly. It’s readable. We can end it right know.”&lt;/p&gt;

&lt;p&gt;But not us. We want greater control and to have everything unified. Fonts, spaces, icons… you name it. We want this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BS2n7UpL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/be7j8ndb83e7aaz8jc6m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BS2n7UpL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/be7j8ndb83e7aaz8jc6m.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Much better! Each element has same font size and takes as much space as he wants and can get from its parent.&lt;br&gt;
The aim is to apply strategies to containers view children, untill they reach desirable state, whether by having smaller font, smaller paddings or even icons hidden! Every strategy should apply to each children, so we could get an consistent and more pleasant look.&lt;/p&gt;
&lt;h1&gt;
  
  
  The Solution
&lt;/h1&gt;

&lt;p&gt;I assume you know how to create new project, so I will skip this unrelevant part ;)&lt;br&gt;
What I want to do, is to prepare a compound view (ButtonBarViewHolder), which will act as a container for particular view items (ButtonBarItem). So, my ButtonBarViewHolder could implement it’s own way of dealing with measuring and placing its children. I think, to achieve best results I shouldn’t just limit myself to applying only one method for optimising child items, but I will talk about it later (how it could be used as a responsive bottom bar widget).&lt;/p&gt;

&lt;p&gt;Let’s start with preparing some building blocks.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;attr.xml&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;ButtonBartemsHolder.kt&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We will use it more in just a moment.&lt;/p&gt;

&lt;p&gt;view_button_bar_item.xml&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;activity_main.xml&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Ok! Having all what is needed to present problem, we can now start and measure desired width and after that, children’s width’s in order to determine if they cumulative size exceeds our desired width (or not).&lt;/p&gt;

&lt;p&gt;In order to achieve a proper and measured childrens, we will need to use a recursion during viewholder’s onMeasure() pass. It makes widget computational heavier, due too more than two measuring passes (normally from 4 to 5, and sometimes more). Be careful! Using this approach in many places on one screen can lead to frame drops or stackoverflow.&lt;/p&gt;

&lt;p&gt;ButtonBarItemsHolder:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;As you can see, we’ve measured deisred (parent) width and having that knoweledge asked its children to measure themself . Next we will match measured childrens againts parent and try to use a strategy, which will execute some kind of “tweaks” to childrens.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For this we’ll use an interface:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;and concrete implemetation:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This strategy will try to downsize fonts of given view and call “onDepleted” when reach end (which in this case is minimum font sie of 10 points).&lt;/p&gt;

&lt;p&gt;We could this strategy it in ButtonBarItemsHolder directly, but (in my opinion) it will be better to delegate it to other class. We could use some kind of strategies executor, which will execute all the strategies we will provide to it.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Having working executor, we can now add it to our ButtonBarItemsHolder and launch inside recalculateChildren() methods body.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Uff! Finally made it trough part 1 of our mini series! Now our “widget” is a little more responsive than it was before. In further steps we will try to add more strategies (like changing paddins, hiding icons etc) and position.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DPc8JIUg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/r85uj43iqakl4ny92zha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DPc8JIUg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/r85uj43iqakl4ny92zha.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can always check working example on my github if you like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/krzysztofbalana/buttons-bar-navigation"&gt;https://github.com/krzysztofbalana/buttons-bar-navigation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>androidappdev</category>
      <category>androiddev</category>
    </item>
  </channel>
</rss>
