<?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 Mihir Patel on Medium]]></title>
        <description><![CDATA[Stories by Mihir Patel on Medium]]></description>
        <link>https://medium.com/@mhrpatel12?source=rss-315b019b2035------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*BYCFTkV2PyMLYtk7xiqEKg.jpeg</url>
            <title>Stories by Mihir Patel on Medium</title>
            <link>https://medium.com/@mhrpatel12?source=rss-315b019b2035------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 00:07:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mhrpatel12/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[Android Accessibility Service : The Unexplored Goldmine]]></title>
            <link>https://mhrpatel12.medium.com/android-accessibility-service-the-unexplored-goldmine-d336b0f33e30?source=rss-315b019b2035------2</link>
            <guid isPermaLink="false">https://medium.com/p/d336b0f33e30</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[user-experience]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[ux]]></category>
            <dc:creator><![CDATA[Mihir Patel]]></dc:creator>
            <pubDate>Sat, 12 Dec 2020 17:22:00 GMT</pubDate>
            <atom:updated>2020-12-12T17:22:00.445Z</atom:updated>
            <content:encoded><![CDATA[<h3>Android Accessibility Service: The Unexplored Goldmine</h3><p>Accessibility service is a long-running privileged service/special feature of the Android framework designed to communicate to the user or perform actions on behalf of user/application installed on an Android device.</p><p>Examples include converting text to speech, providing haptic feedback when a user is hovering on something, performing touch/swipe gestures.<br>In this article, we’ll primarily focus on how to perform touch &amp; swipe gestures on-screen on behalf of the user.</p><p>Basic steps are to declare accessibility service in AndroidManifest &amp; creating a new Class extending <a href="https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html">AccessibilityService</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a2cfa61579c5a0020bc3cf267a5576e/href">https://medium.com/media/9a2cfa61579c5a0020bc3cf267a5576e/href</a></iframe><p><em>Here, creating separate resource XML for accessibility service metadata is mandatory to avoid exceptions from PackageParser.java.</em></p><p><strong><em>canPerformGestures</em></strong> is meta-tag that let’s service to perform gestures on behalf of the user. This meta also can be granted at runtime.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e24b4798ed341a074327f0b039f6e98/href">https://medium.com/media/2e24b4798ed341a074327f0b039f6e98/href</a></iframe><p>Your accessibility service class should extend <a href="https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html">AccessibilityService</a>. Primary functions should you override if required are <strong><em>onServiceConnected(), onAccessibilityEvent() &amp; onInterrupt().</em></strong><br>In case one need to add any flags or eventTypes at runtime then it can be done in <strong><em>onServiceConnected()</em></strong> via,</p><pre><em>serviceInfo</em>.eventTypes &amp; <em>serviceInfo</em>.flags</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da874a5fce24615767281b5f2d6ec1a2/href">https://medium.com/media/da874a5fce24615767281b5f2d6ec1a2/href</a></iframe><p>All privileged actions must be done from the class which extends AccessibilityService or via the instance of accessibility service. For our example, we will be injecting touch &amp; swipe gestures from this class.</p><p>GestureDescription is the first step to do so and it describes Gestures that are made up of one or more strokes. Strokes consist of Path, start time &amp; duration. To create strokes another class Accessibility provides is StrokeDescription. Gestures are immutable once built and will be dispatched to the specified display. <br>Dimensions we specify throughout are in screen pixels &amp; Time is in milliseconds.</p><p>Here’s the example of creating GestureDescriptions for simulating tap &amp; swipe gesture.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1e418ab4b7c8fca8aff69f5bfa48092/href">https://medium.com/media/b1e418ab4b7c8fca8aff69f5bfa48092/href</a></iframe><p>For the click, X &amp; Y coordinates on screen where touch needs to get performed. To make it a swipe gesture we just need another coordinates for where to end the gesture &amp; duration for it. <br>Function <strong><em>buildSwipe()</em></strong> deliberately does simple horizontal/vertical swipe, just to demonstrate the capability of the service, it can be extended further by building Path as per your very own needs.</p><p>Once we have the GestureDescription, all we need is the instance of accessibility service to dispatch the gesture.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a48f34dcd342674771d8b1455eaf305/href">https://medium.com/media/0a48f34dcd342674771d8b1455eaf305/href</a></iframe><p>dispatchGesture takes GestureDescription as an argument, for which created <strong><em>buildClick() &amp; buildSwipe()</em></strong> functions above. Second &amp; third arguments are GesureResultCallback &amp; Handler respectively, both of ’em are optional.</p><p>Once these are in place, one can create APIs out of these to make things easier for end-user or to automate things, possibilities are endless for this.</p><p>Gesture support on accessibility service was introduced from Android API level 24. Classes GestureDescription, StrokeDescription &amp; function dispatchGesture all are available only from Android N onwards.</p><h4>Starting Accessibility Service</h4><p>To start the service, a user must navigate to Android settings &gt; Accessibility &gt; Your Accessibility Service &gt; Click on Use Service. <br>These steps are very important as there is no way to start accessibility service programmatically. This is done very deliberately to avoid any android app taking over on entire device.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*armn2CECP0yyhIwCG6W2mQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nnNYPQ0wS6o6DIkFhCW9Qg.png" /><figcaption>starting your accessibility service</figcaption></figure><p>We can ease a few clicks for the user by directly landing user to the Accessibility settings screen. This can be done simply by launching an intent from your activity.</p><pre>val intent = Intent(Settings.<em>ACTION_ACCESSIBILITY_SETTINGS</em>)<br>context.startActivity(intent)</pre><p>Restriction for starting service programmatically remains the same for all other capabilities regardless if your service wants to perform gestures or not.</p><p>As soon as a user starts your service onServiceConnected() function is called. <br>To check if your service is started &amp; running or not we need to fetch all enabled accessibility services on device and loop through it to match our service.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf43669d36400d03a5c4f692b09fb429/href">https://medium.com/media/bf43669d36400d03a5c4f692b09fb429/href</a></iframe><p>Much more can be done here other than just dispatching gestures for the user. <br>Like draw over other apps, accessibility service also allows putting content on the screen, above all. Doing so also is pretty simple.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa8b12b05fd8634577322454d3dce9d7/href">https://medium.com/media/fa8b12b05fd8634577322454d3dce9d7/href</a></iframe><p>As soon as the user navigates to Accessibility settings and starts the service, the layout will be visible on the screen.</p><p>Unlike drawing over other apps, which is just limited to manipulation within the package, here we have control across the device. <br>We can put some buttons which can perform a series of actions which may take require user ages to perform. <br>Another function provided by Accessibility service is performGlobalAction() which takes an integer as an argument. <br>This allows to perform an action that can be performed at any moment regardless of the current application or user location in that application. For example going back, going home, opening recent, etc.</p><pre>public static final int GLOBAL_ACTION_BACK = 1;<br>public static final int GLOBAL_ACTION_HOME = 2;<br>public static final int GLOBAL_ACTION_RECENTS = 3;<br>public static final int GLOBAL_ACTION_NOTIFICATIONS = 4;<br>public static final int GLOBAL_ACTION_QUICK_SETTINGS = 5;<br>public static final int GLOBAL_ACTION_POWER_DIALOG = 6;<br>public static final int GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN = 7;<br>public static final int GLOBAL_ACTION_LOCK_SCREEN = 8;<br>public static final int GLOBAL_ACTION_TAKE_SCREENSHOT = 9;</pre><p>One of the few catches that make using Accessibility service a difficult is not being able to start the service programmatically. This, however, is obvious to not have a loophole in System. Still, privileges this service have are unmatched.</p><p>Another thing to keep in mind is, accessibility service needs to be restarted on each app update even if service was started by the user. This behaviour is the same even in case there was no change in accessibility service as part of App Update. This also is to prevent apps from misusing the privilege by exploiting service on an app update.<br>To avoid this, we can make accessibility service a separate module and install as additional apk in the user’s device when required. This will ultimately a new app without any activity or UI but only Accessibility service. This will avoid the android system from stopping service when your actual app is updated.</p><p>I have not covered each every function of what Accessibility service is capable of but it is worth to explore what all is offered on <a href="https://developer.android.com/reference/android/accessibilityservice/AccessibilityService">official docs</a>.</p><p>I hope this article will be helpful in some way! <br>Would love to hear your comments/suggestions/corrections/questions!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d336b0f33e30" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Darkness is coming. Prepare your Android App to support Dark Mode]]></title>
            <link>https://mhrpatel12.medium.com/darkness-is-coming-prepare-your-android-app-to-support-dark-mode-e29730e63370?source=rss-315b019b2035------2</link>
            <guid isPermaLink="false">https://medium.com/p/e29730e63370</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[android-q]]></category>
            <dc:creator><![CDATA[Mihir Patel]]></dc:creator>
            <pubDate>Sat, 07 Sep 2019 13:14:44 GMT</pubDate>
            <atom:updated>2019-09-07T13:14:44.477Z</atom:updated>
            <content:encoded><![CDATA[<p>The stable release of Android 10 is out and it’s just a matter of time other manufacturers also will start OTA updates. Android 10 comes with support for Dark theme.</p><h4>No Dark mode = Horrible UX</h4><p>If you have migrated to Android 10 and have not done any changes in your app then even if the user has enabled the dark mode system-wide, it does not affect your app in any way. It just creates super weird UX because of your wonky decision for ignoring dark mode. If user is surfing through apps, opens your app which does not support dark mode then user suddenly gets super bright light blinding the eyes!</p><p>If you live in 2030 and have already dark-themed design for your app, you don’t have to any change when you migrate your app to API 29. Darkness is your Ally :-)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*A2BeSjDz_iVrAuA9Z0ne2g.jpeg" /></figure><p>Anyways, the question still remains is how do we make sure that the Android app is par with this change and supports Dark mode.</p><h3>Quick and dirty way</h3><p>If dark mode is the least important thing you care about your app and don’t want to spend any efforts implementing this then adding a single line in your app theme is all it takes. Rest system takes care of it for you.</p><p>Force Dark analyzes each view of your light-themed app and applies a dark theme automatically before it is drawn to the screen.</p><p>This serves the purpose but results may not be the best. Here is a screenshot of light and forced dark theme of our <a href="https://play.google.com/store/apps/details?id=com.erbauen.hush">Hush</a> app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z8h8l8gPvRRUmj7m_2dIHg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*00fZLe_f4pAfDZjPFT78sQ.png" /><figcaption>Light mode on left and forced dark mode on right</figcaption></figure><p>Force dark mode does it job but not perfectly. If primarily relies on inverting colours. As mentioned already, this is a quick and dirty way, if you want to gain more control solution is to use DayNight theme explicitly.</p><h3><strong>Implementing in Legitimate and Pristine way</strong></h3><p>In order to support Dark theme, you must set your app’s theme (usually found in res/values/styles.xml) to inherit from a DayNight theme:</p><pre>&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.AppCompat.DayNight&quot;&gt;</pre><p>or <a href="https://material.io/develop/android/theming/dark">MaterialComponents’ dark theming</a>:</p><pre>&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.DayNight&quot;&gt;</pre><p>This ties the app’s main theme to the system-controlled night mode flags and gives the app a default Dark theme (when enabled).</p><p>Once this is done, you need to specify values and drawables for night(dark) mode. Just like you declare values/drawables for specific android version, now you have to create another directory for night as drawable-night and values-night. In values directory, you have to define another colors.xml which will contain colour values for dark mode.</p><p>After this change, here is the screenshot of the same app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*00fZLe_f4pAfDZjPFT78sQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MyyDSyzJ7mXrNqF3FIwDGw.png" /><figcaption>forced dark mode on left vs customised dark mode on right</figcaption></figure><p>Here you have complete control over how your layouts will be portrayed on dark mode. More or less, you have to define each colour/drawable for dark mode. Appropriate name for this technique is called “using night-qualified resources”.</p><p><strong>Official documentation also recommends using theme attributes</strong> as,textColorPrimary. Here, in values-night directory, we created colors.xml in the previous section, instead of different colors.xml we create different styles.sml.</p><p>Something like</p><p>values-night/styles.xml.</p><pre>&lt;<strong>style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.DayNight&quot;</strong>&gt;<br><strong>  &lt;item name=&quot;android:colorBackground&quot;&gt;</strong>@color/backgroundDark<strong>&lt;/item&gt; </strong><br>  &lt;<strong>item name=&quot;</strong><strong>textColorPrimary&quot;</strong>&gt;@color/textColorPrimaryLight&lt;/<strong>item</strong>&gt;<br>&lt;/<strong>style</strong>&gt;</pre><p>values/styles.xml:</p><pre>&lt;<strong>style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.DayNight&quot;</strong>&gt;<br><strong>  &lt;item name=&quot;android:colorBackground&quot;&gt;</strong>@color/backgroundLight<strong>&lt;/item&gt; </strong><br>  &lt;<strong>item name=&quot;</strong><strong>textColorPrimary&quot;</strong>&gt;@color/textColorPrimaryDark&lt;/<strong>item</strong>&gt;<br>&lt;/<strong>style</strong>&gt;</pre><p>In any case, you should not have hardcoded colours or icons, else Whichever approach you follow, both are correct. However, using theme attributes is better approach any day and also requires a very strong foundation of a theming structure.</p><p>If you were following best practices for android Themes &amp; styles adding support for Dark mode is not that difficult. You can also refer to <a href="https://github.com/googlesamples/android-DarkTheme">Dark Mode Example</a> for working demo.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e29730e63370" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making most out of Android Bottom Sheet]]></title>
            <link>https://mhrpatel12.medium.com/making-most-out-of-android-bottom-sheet-352c04551fb4?source=rss-315b019b2035------2</link>
            <guid isPermaLink="false">https://medium.com/p/352c04551fb4</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[material-design]]></category>
            <dc:creator><![CDATA[Mihir Patel]]></dc:creator>
            <pubDate>Sun, 01 Sep 2019 08:10:57 GMT</pubDate>
            <atom:updated>2019-09-01T13:47:36.338Z</atom:updated>
            <content:encoded><![CDATA[<p>BottomSheet was released a few years back and it’s a material component that slides up from the bottom of the screen to reveal more content. Though there have been a few years since the release, I strongly believe it is still one of the most underutilised material component. You can find more detailed information on Bottom Sheet on <a href="https://material.io/design/components/sheets-bottom.html#">Google Material Design</a> guidelines.</p><p>As per the material design guideline for BottomSheet, there are two types of BottomSheets:</p><ol><li><strong>Modal bottom sheets</strong>: It Can be implemented using BottomSheetDialog or BottomSheetDialogFragment. These<strong> </strong>are an alternative to inline menus or simple dialogs on mobile and provide room for additional items, longer descriptions, and iconography. They must be dismissed in order to interact with the underlying content.</li><li><strong>Persistent bottom sheets</strong>: It can be implemented using <a href="https://material.io/components/android/catalog/bottom-sheet-behavior/">BottomSheetBehavior</a> in conjunction with a <a href="https://material.io/components/android/catalog/coordinator-layout/">CoordinatorLayout</a>.</li></ol><p>We’ll primarily focus on <strong>Modal bottom sheets.</strong></p><p>Using BottomsheetDialogFragment is fairly very easy, and it is a very strong alternative to conventional widgets i.e. PopupWindow, AlertDialog, DialogFragment. As it slides up from the bottom and is well within reach of a thumb, it’s a perfect solution for asking the user to choose from multiple options. Conventional widgets are somewhat useless these days as the average size of smartphones is 6&quot; and requires the use of both hands for 100% screen reachability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WwxswoTLeIUHdI4SnWDivQ.jpeg" /></figure><p>When implemented smartly, the very same component can be reused across the application for different use cases.</p><p>Many apps have adopted BottomSheetDialogFragment beautifully to fit in their use cases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yS42MCpgaeo5JnCah9rMrA.jpeg" /><figcaption>Folks at Twitter has almost eliminated the context menu from their app and have replaced it with BottomSheetDialogFragment.</figcaption></figure><p>In terms of implementation, using BottomSheetDialogFragment is not at all a difficult task, All you need to do is extend your class with <strong>BottomSheetDialogFragment </strong>and call it just like an ordinary fragment.</p><p>You may have to add Gradle dependency if not using material components already.</p><pre>//ANDROID X<br>implementation &#39;com.google.android.material:material:1.1.0-alpha09&#39;</pre><pre><strong>OR</strong></pre><pre>//SUPPORT LIBRARY<br>implementation &#39;com.android.suppor:design:28.0.8&#39;</pre><p>Let’s start with very basic BottomSheetDialogFragment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z97l2LdD-bhjzF-nGq8E8A.jpeg" /><figcaption>#1 No bells and whistles, just a plain BottomSheet fragment.</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e33bf5625e9d5fe714105956ffc21fe/href">https://medium.com/media/2e33bf5625e9d5fe714105956ffc21fe/href</a></iframe><p>Now, there’s no straight forward way to add more customisations to basic BottomSheet.</p><p>Say, you would like to have top corners on BottomSheet to be curved. Something like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iMkFvA5xm94B91xXCIEnPQ.jpeg" /><figcaption>BottomSheet with curved top corners</figcaption></figure><p>If you just apply the background with curved top corners it won’t work. You’ll get something ugly like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*9tHiH77-FQ20U_kL-BvJ4w.jpeg" /></figure><p>Those ugly stubborn white corners are there because by default bottomsheet takes parent view background colour. To solve this parent view needs a transparent background, which we can set by overriding onActivityCreated method of BottomSheetDialogFragment and set view background colour as transparent.</p><pre>override fun onActivityCreated(savedInstanceState: Bundle?) {<br>    super.onActivityCreated(savedInstanceState)<br>    (<em>view</em>?.<em>parent </em>as View).setBackgroundColor(Color.<em>TRANSPARENT</em>)<br>}</pre><p><strong>Easy!</strong></p><p>What if, your design needs margin on sides too? Will applying just margin on your fragment layout will do? Not really, it won’t work. BottomSheet takes margins also from parent view. Just like background colour we also have to override margins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XN9NZKbXXxK_EYQkFmvAQQ.png" /><figcaption>BottomSheet with curved top corners and margins on sides.</figcaption></figure><pre>override fun onActivityCreated(savedInstanceState: Bundle?) {<br>    super.onActivityCreated(savedInstanceState)<br>    (<em>view</em>?.<em>parent </em>as View).setBackgroundColor(Color.<em>TRANSPARENT</em>)<br>    val resources = <em>resources<br><br>    </em>if (resources.<em>configuration</em>.orientation == Configuration.<em>ORIENTATION_PORTRAIT</em>) {<br>        <em>assert</em>(<em>view </em>!= null)<br>        val parent = <em>view</em>?.<em>parent </em>as View<br>        val layoutParams = parent.<em>layoutParams </em>as CoordinatorLayout.LayoutParams<br>        layoutParams.setMargins(<br>            resources.getDimensionPixelSize(R.dimen.<em>screen_margin</em>), // LEFT 16dp<br>            0,<br>            resources.getDimensionPixelSize(R.dimen.<em>screen_margin</em>), // RIGHT 16dp<br>            0<br>        )<br>        parent.<em>layoutParams </em>= layoutParams<br>    }<br>}</pre><p>There’s a lot more can be done using BottomSheets, everything you do with normal fragments can be done with BottomSheetDialogFragment. However, due to its swipe down to dismiss gesture, it may create usability issues when multiline EditTexts are used. When a user has written multiple lines and tries to move the cursor or up/down, user might end up dismissing the fragment itself.</p><p>By default, BottomSheetDialogFragments doesn’t expand to full screen. Only some content of it made visible to user and is expanded to full screen only when user makes swipe up gesture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ghJ0DIm6zwaSBF2o9XLQg.png" /><figcaption>Only portion of entire content is visible initially.</figcaption></figure><p>To open BottomSheetDialogFragment on full screen, there’s no straight forward property available. onCreateDialog method needs to be overridden and have to set “state” of BottomSheetBehavior as “STATE_EXPANDED”.</p><pre>override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {<br>    val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog<br>    bottomSheetDialog.setOnShowListener <strong>{<br>        </strong>val dialog = <strong>it </strong>as BottomSheetDialog<br>        val bottomSheet = dialog.findViewById&lt;View&gt;(com.google.android.material.R.id.<em>design_bottom_sheet</em>)<br>        BottomSheetBehavior.from(bottomSheet).<em>state </em>= BottomSheetBehavior.<em>STATE_EXPANDED<br>    </em><strong>}</strong></pre><pre><strong>    </strong>return bottomSheetDialog<br>}</pre><p>Again, fairly easy!</p><p>Story still doesn’t end here, if you are using scrollview, dynamic views OR recyclerview in your fragment and contents are too small for covering entire EXPANDED state and too large for fit into a partially expanded state. When you don’t have enough content to fit into EXPANDED state of a fragment, too much of whitespace looks horrible. Solving this problem is most tricky as it only happens when views are dynamic.</p><p>Here, in onStart method of fragment, views are made visible to the user. Which means, you can calculate the height of contents and set peekHeight of BottomSheetBehavior. This more or less works as wrap_content.</p><pre>override fun onStart() {<br>    super.onStart()<br>    <em>dialog</em>?.<em>also </em><strong>{<br>        </strong>val bottomSheet = <em>dialog</em>?.findViewById&lt;View&gt;(R.id.<em>design_bottom_sheet</em>)<br>        bottomSheet?.<em>layoutParams</em>?.height = ViewGroup.LayoutParams.<em>WRAP_CONTENT<br>        </em>val behavior = BottomSheetBehavior.from&lt;View&gt;(bottomSheet)<br>        val layout = root?.findViewById(R.id.rootLayout) as LinearLayout //rootLayout is root of your fragment layout.</pre><pre>        layout.<em>viewTreeObserver</em>?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {<br>            override fun onGlobalLayout() {<br>                try {<br>                    layout.<em>viewTreeObserver</em>.removeGlobalOnLayoutListener(this)<br>                    behavior.<em>peekHeight </em>= layout.<em>height<br>                    view</em>?.requestLayout()<br>                } catch (e: Exception) {<br>                }<br>            }<br>        })<br>    <strong>}<br></strong>}</pre><p>I’ve covered most of the problems which didn’t had any out of the box solution. If you also have faced any problem and have/don’t have solution to it, put on a response to this article!</p><p>Thanks for reading! 🙂</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=352c04551fb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Curious Case of Android Font Size]]></title>
            <link>https://mhrpatel12.medium.com/the-curious-case-of-android-font-size-b6aae15aa540?source=rss-315b019b2035------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6aae15aa540</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[fonts]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android-development]]></category>
            <dc:creator><![CDATA[Mihir Patel]]></dc:creator>
            <pubDate>Sat, 13 Apr 2019 16:05:28 GMT</pubDate>
            <atom:updated>2021-02-13T14:52:30.290Z</atom:updated>
            <content:encoded><![CDATA[<p>Scratching your head over font size for different screens in your new Android app? Use <strong>“sp” for font size and “dp” for margins, that’s the Law</strong>. But is it that straight forward? Well, not really.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2UoR_Z-eowL6AKgLOs0Z9w.jpeg" /></figure><h3><strong>Basics first</strong></h3><p><strong>“dp” — Density-independent Pixel</strong> — An abstract unit based on the physical density of the screen. The unit which is relative to a dpi (dots per inch) screen, on MDPI devices 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen’s dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. More on these scaling later. Using dp units is a simple solution to making the view dimensions in your layout resize properly for different screen densities. That is, dp for margins.</p><p><strong>“sp” — Scale-independent Pixels</strong> — Has everything which “dp” got, but gets scaled by the user’s font size preference. Recommended unit for font sizes.</p><p>Clear enough, let’s use sp for font size and see results in different font preferences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cDjM51QXPmLPRZR6xIR-Ng.jpeg" /></figure><p>As expected, “sp” adopts the user’s font preference and ramps up the font size accordingly. <strong>While the idea of “sp” fonts has a good heart but is a poor idea. From a design point of view, using “sp” is not the right thing to do.</strong></p><p>Now, let’s break the law and use “dp” instead of “sp”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PZYxrrZ6YxWmkw2gkWanGg.jpeg" /></figure><p>Looks better, isn’t it?</p><h4>W<strong>hy go for “dp” whereas it is not recommended unit for font size</strong>?</h4><p>Because going forward with “sp” may end up breaking your beautiful design if the user has some wonky font size preference. Moreover, the user may end up blaming your app for not handling this and not their own life choice!</p><h4><strong>That’s not it. We can still have more superior control over this by using pixels(“px”) instead of “dp”.</strong></h4><p>We can define custom font size by declaring how much pixels should it accommodate in different density screens. This is more like “dp” only but with your custom scale factor.</p><p>To define a custom scale factor, let’s first understand how “dp” is scaled.</p><p><strong>MDPI</strong> is a base scale <strong>(1x)</strong></p><p>LDPI is <strong>0.75 x MDPI</strong></p><p>HDPI is <strong>1.5 x MDPI</strong></p><p>XHDPI is <strong>2 x MDPI</strong></p><p>XXHDPI is <strong>3 x MDPI</strong></p><p>XXXHDPI is <strong>4 x MDPI</strong></p><p>Now let’s define a font size name “font_size_m”, and declare it in the respective dimen folder.</p><h4><strong>MDPI</strong></h4><pre>&lt;dimen name=&quot;font_size_m&quot;&gt;14px&lt;/dimen&gt; &lt;!--1X--&gt;</pre><h4><strong>LDPI</strong></h4><pre>&lt;dimen name=&quot;font_size_m&quot;&gt;10.5px&lt;/dimen&gt; &lt;!--LDPI = (MDPI*0.75)--&gt;</pre><h4><strong>HDPI</strong></h4><pre>&lt;dimen name=&quot;font_size_m&quot;&gt;21px&lt;/dimen&gt; &lt;!--HDPI = (MDPI*1.5)--&gt;</pre><h4><strong>XHDPI</strong></h4><pre>&lt;dimen name=&quot;font_size_m&quot;&gt;28px&lt;/dimen&gt; &lt;!--XDPI = (MDPI*2)--&gt;</pre><h4><strong>XXHDPI</strong></h4><pre>&lt;dimen name=&quot;font_size_m&quot;&gt;42px&lt;/dimen&gt; &lt;!--XXDPI = (MDPI*3)--&gt;</pre><h4><strong>XXXHDPI</strong></h4><pre>&lt;dimen name=&quot;font_size_m&quot;&gt;56px&lt;/dimen&gt; &lt;!--XXXDPI = (MDPI*4)--&gt;</pre><p>This conversation is the same as how dp is scaled as per different screen sizes, but here <strong>we get an upper hand by varying number of pixels for particular screen size</strong>. For example, the font size we declared will occupy 21 pixels in HDPI screen size, if you feel 21 pixels are more, you can reduce the number of pixels or more until it fits into your design.</p><p>Let’s again take a previous example but with different DPI screens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jjGpxAk-O1vGHImjkt3HEg.jpeg" /><figcaption>XXHDPI looks fine but the <strong>text in HDPI looks HUGE</strong>.</figcaption></figure><p>dimen.xml(XXHDPI)</p><pre>&lt;resources&gt;&lt;!--XXDPI = (MDPI*3)--&gt;<br>    &lt;dimen name=&quot;font_size_xxxl&quot;&gt;150px&lt;/dimen&gt;<br>    &lt;dimen name=&quot;font_size_xxl&quot;&gt;120px&lt;/dimen&gt;<br>    &lt;dimen name=&quot;font_size_xl&quot;&gt;90px&lt;/dimen&gt;<br>&lt;/resources&gt;</pre><p>dimen.xml(HDPI)</p><pre>&lt;resources&gt;&lt;!--HDPI = (MDPI*1.5)--&gt;<br>    &lt;dimen name=&quot;font_size_xxxl&quot;&gt;75px&lt;/dimen&gt;<br>    &lt;dimen name=&quot;font_size_xxl&quot;&gt;60px&lt;/dimen&gt;<br>    &lt;dimen name=&quot;font_size_xl&quot;&gt;45px&lt;/dimen&gt;<br>&lt;/resources&gt;</pre><p>Now, as we have control over how much pixels it should occupy in particular screen density, let’s reduce the number of pixels for HDPI only and see results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g5M1wBime_PPYVTUEYTYtA.jpeg" /><figcaption>For each font size in HDPI, reduced number of pixels by 15.</figcaption></figure><p>dimen.xml(HDPI)</p><pre>&lt;resources&gt;&lt;!--HDPI = (MDPI*1.5)--&gt;<br>    &lt;dimen name=&quot;font_size_xxxl&quot;&gt;60px&lt;/dimen&gt;<br>    &lt;dimen name=&quot;font_size_xxl&quot;&gt;45px&lt;/dimen&gt;<br>    &lt;dimen name=&quot;font_size_xl&quot;&gt;30px&lt;/dimen&gt;<br>&lt;/resources&gt;</pre><p>This one looks neater than the previous image, as we have reduced pixel count. This change will affect only HDPI devices, XXHDPI and rest of configurations remain as it.</p><h4><strong>The question still remains, why “sp” is recommended unit for font size then?</strong></h4><p>Because it considers the font size preference of the user. This is the only primary reason for it, <strong>while I appreciate the point of using “sp”, this cannot be used when you are building an app which has complex design</strong> and you cannot compromise on even a pixel.</p><p>It all comes down to a preference of developers/designers of the app that how they want their app to look on different screens and if preference should be given to the user or not.</p><h3>How big players are doing it?</h3><p>Here is a comparison of how Reddit &amp; Swiggy have their own approach to font size.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4iKT1fcNss8oUG-s58xWOw.jpeg" /><figcaption>Comparison of <a href="https://play.google.com/store/apps/details?id=com.reddit.frontpage">Reddit</a> on the Same device with different font preferences</figcaption></figure><p>Reddit is primarily used for content consumption, for them, it makes sense to go with “sp” approach, where preference is given to the user. It’s clearly seen in comparison that what happens when the user increases font size.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*68hioGKRlFyDN7RrXfs4mQ.jpeg" /><figcaption>Comparison of <a href="https://play.google.com/store/apps/details?id=in.swiggy.android">Swiggy</a> on the Same device with different font preferences</figcaption></figure><p>Here is what Swiggy does. They’re rigid, they don’t care what preference user has, their design stays as it is, no matter what font size you choose. They’re focusing on containing too many elements in a screen, if they had used “sp”, their design would have been completely messed up in case of larger font size.</p><h3><strong>Conclusion</strong></h3><p>There is no definite rule for what unit of font size should be used. It depends on the preference of the developer/designer and the complexity of the design. In our app <a href="https://play.google.com/store/apps/details?id=com.erbauen.hush">Hush</a>, we followed a mixed approach. We defined our own scale factors where it was necessary and used “sp” as well.</p><p>I hope this article will be helpful in designing your app. I would love to hear your comments/suggestions/corrections/questions!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6aae15aa540" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>