<?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: K M Rejowan Ahmmed</title>
    <description>The latest articles on DEV Community by K M Rejowan Ahmmed (@ahmmedrejowan).</description>
    <link>https://dev.to/ahmmedrejowan</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%2F633726%2F69cf1035-ebcd-4140-bc8c-144006ab06f3.png</url>
      <title>DEV Community: K M Rejowan Ahmmed</title>
      <link>https://dev.to/ahmmedrejowan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmmedrejowan"/>
    <language>en</language>
    <item>
      <title>Android 13’s Restricted Setting: A Stronghold Against Malicious Apps</title>
      <dc:creator>K M Rejowan Ahmmed</dc:creator>
      <pubDate>Sun, 09 Jul 2023 12:25:06 +0000</pubDate>
      <link>https://dev.to/ahmmedrejowan/android-13s-restricted-setting-a-stronghold-against-malicious-apps-1ea9</link>
      <guid>https://dev.to/ahmmedrejowan/android-13s-restricted-setting-a-stronghold-against-malicious-apps-1ea9</guid>
      <description>&lt;p&gt;Dear Android users, good news is on the horizon! Google is significantly bolstering its security features with Android 13. The &lt;strong&gt;“Restricted Setting”&lt;/strong&gt; feature is ready to safeguard your device from potentially harmful apps. Let’s break it down.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2400%2F0%2A3lsXNVog2Iu6ybjV.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2400%2F0%2A3lsXNVog2Iu6ybjV.jpg" alt="Infographics"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem: Potentially Harmful Apps
&lt;/h2&gt;

&lt;p&gt;There are instances when we download apps not from the official app stores, but from other sources like a browser or a messaging app. These are called sideloaded apps.&lt;/p&gt;

&lt;p&gt;Unfortunately, not all sideloaded apps are benign. Some might have harmful intentions and could infiltrate your phone’s Accessibility settings and Notification Listener. These areas can allow apps to intercept your incoming notifications and gain access to sensitive information.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution: Restricted Setting Feature
&lt;/h2&gt;

&lt;p&gt;This is where the “Restricted Setting” feature steps in. It acts like a secure gatekeeper, barring any sideloaded app from accessing your Accessibility settings and Notification Listener.&lt;/p&gt;

&lt;p&gt;The real beauty is in its alert mechanism. If Android 13 spots a potentially harmful app, it locks down the Accessibility settings for that app. A dialog pops up stating, &lt;strong&gt;“Restricted settings. For your security, this setting is currently unavailable”&lt;/strong&gt;, along with an ‘OK’ button to dismiss the message. Quite a smart move!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AVSg4xqJdndFytfA5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AVSg4xqJdndFytfA5.jpg" alt="Screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What About Apps From The Official App Stores?
&lt;/h2&gt;

&lt;p&gt;If you’re an adherent downloader from the official app stores, there’s no need for concern. These app stores utilize something called a session-based package installer. The “Restricted Setting” feature doesn’t impact apps sourced from app stores. Rest assured, you’re in safe hands here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is There A Way To Override The Restricted Setting?
&lt;/h2&gt;

&lt;p&gt;Indeed, Google has thought of that too. Even if the “Restricted Setting” feature blocks an app, there is a way to unblock it.&lt;/p&gt;

&lt;p&gt;You can first dismiss the restricted setting dialog by pressing ‘OK’ (which signifies your awareness of the potential risk). After this, visit the App Info page of the blocked app. From there, click on the top-right corner options menu and select ‘Allow Restricted Settings’. Be sure to use this feature cautiously, and only if you’re confident the app is safe.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AxgbZC5s4zDRIC4VT.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AxgbZC5s4zDRIC4VT.jpg" alt="Screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that’s it! Android 13’s “&lt;strong&gt;Restricted Setting&lt;/strong&gt;” feature is a powerful ally in your fight against malicious apps.&lt;/p&gt;

&lt;p&gt;Always remember, your data and privacy are valuable. Always download apps from trusted sources and keep your Android updated for the best protection.&lt;/p&gt;

&lt;h2&gt;
  
  
  More
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/navigating-pages-with-ease-tap-left-right-a-custom-viewpager-in-android/" rel="noopener noreferrer"&gt;Navigating Pages with Ease: Tap Left/Right — A Custom ViewPager in Android&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/recyclerview-last-item-extra-bottom-margin/" rel="noopener noreferrer"&gt;RecyclerView Last Item Extra Bottom Margin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/making-android-views-draggable-a-practical-guide/" rel="noopener noreferrer"&gt;Making Android Views Draggable: A Practical Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/hide-the-soft-keyboard-and-remove-focus-from-edittext-in-android/" rel="noopener noreferrer"&gt;Hide the Soft Keyboard and Remove Focus from EditText in Android&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/building-a-visual-password-strength-meter-in-android-a-simple-guide/" rel="noopener noreferrer"&gt;Building a Visual Password Strength Meter in Android: A Simple Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/android-searchview-oncloselistener-not-working-here-is-how-to-fix-it/" rel="noopener noreferrer"&gt;Android SearchView OnCloseListener Not Working? Here’s How to Fix It&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/design/cardview-vs-materialcardview/" rel="noopener noreferrer"&gt;CardView vs. MaterialCardView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/android-studio/boost-your-android-development-with-adb-idea-plugin/" rel="noopener noreferrer"&gt;Boost Your Android Development with ADB Idea Plugin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="https://blog.rejowan.com/qa/android-qa/troubleshooting-execution-failed-for-task-appmergedebugresources-error-in-android-xml-files/" rel="noopener noreferrer"&gt;Troubleshooting “Execution Failed for Task app:mergeDebugResources” Error in Android XML Files&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/series/android-basics/android-activity-lifecycle-a-comprehensive-guide/" rel="noopener noreferrer"&gt;Android Activity Lifecycle: A Comprehensive Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>android</category>
      <category>security</category>
    </item>
    <item>
      <title>Navigating Pages with Ease (Tap Left/Right) : A Custom ViewPager in Android</title>
      <dc:creator>K M Rejowan Ahmmed</dc:creator>
      <pubDate>Tue, 06 Jun 2023 15:47:58 +0000</pubDate>
      <link>https://dev.to/ahmmedrejowan/navigating-pages-with-ease-tap-leftright-a-custom-viewpager-in-android-497h</link>
      <guid>https://dev.to/ahmmedrejowan/navigating-pages-with-ease-tap-leftright-a-custom-viewpager-in-android-497h</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Android provides a range of components that allow developers to create engaging and intuitive user interfaces. One such component is the ViewPager, a layout manager that allows users to flip through pages of data, akin to sliding through pages of a book. But what if we could make this navigation even more intuitive? What if a simple tap could transport you through pages? In this blog post, we'll explore how to create a custom ViewPager that responds to single-tap events for page navigation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Purpose
&lt;/h2&gt;

&lt;p&gt;The traditional ViewPager in Android responds to swipe gestures for moving between pages. However, there might be cases where you want a simpler interaction, such as a single tap. Our aim here is to create a custom ViewPager that responds to single-tap events. Tapping on the left half of the screen will take the user to the previous page, while tapping on the right half navigates to the next page.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Achieve That
&lt;/h2&gt;

&lt;p&gt;To achieve this, we will extend the default ViewPager class and customize it to handle our desired tap events. This involves using Android's GestureDetector, which will enable us to listen for single-tap events and respond accordingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Sample
&lt;/h2&gt;

&lt;p&gt;Here's a sample code of a custom ViewPager in Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.content.Context&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.util.AttributeSet&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.view.GestureDetector&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.view.MotionEvent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;androidx.viewpager.widget.ViewPager&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Objects&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomViewPager&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;ViewPager&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;GestureDetector&lt;/span&gt; &lt;span class="n"&gt;gestureDetector&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;CustomViewPager&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;CustomViewPager&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AttributeSet&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;gestureDetector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GestureDetector&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GestureDetector&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SimpleOnGestureListener&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nd"&gt;@Override&lt;/span&gt;
            &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;onSingleTapConfirmed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MotionEvent&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;halfWidth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getWidth&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;2.0f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getX&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;halfWidth&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getCurrentItem&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;setCurrentItem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getCurrentItem&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                    &lt;span class="o"&gt;}&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getCurrentItem&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;requireNonNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getAdapter&lt;/span&gt;&lt;span class="o"&gt;()).&lt;/span&gt;&lt;span class="na"&gt;getCount&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;setCurrentItem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getCurrentItem&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                    &lt;span class="o"&gt;}&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
                &lt;span class="n"&gt;performClick&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;onSingleTapConfirmed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;});&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;onTouchEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MotionEvent&lt;/span&gt; &lt;span class="n"&gt;ev&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;performClick&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;gestureDetector&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;onTouchEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ev&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;onTouchEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ev&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;performClick&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;performClick&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code Breakdown
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;CustomViewPager&lt;/code&gt; class extends the default &lt;code&gt;ViewPager&lt;/code&gt; and modifies its behavior through overriding certain methods and adding a GestureDetector. The GestureDetector is set up to detect single-tap events, and based on the location of the tap (left or right half of the screen), the ViewPager navigates to the previous or next page respectively. This is done in the &lt;code&gt;init()&lt;/code&gt; method which is called from both constructors.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;onTouchEvent()&lt;/code&gt; is overridden to ensure that each touch event is also sent to the GestureDetector for processing, and a click action is performed for possible use by other components or accessibility services.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use the Custom ViewPager
&lt;/h2&gt;

&lt;p&gt;The custom ViewPager can be used in your application just like any other view component. Here's a guide on how to do it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Java&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, you need to create an instance of the CustomViewPager in your activity and set an adapter to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;CustomViewPager&lt;/span&gt; &lt;span class="n"&gt;customViewPager&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CustomViewPager&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;PagerAdapter&lt;/span&gt; &lt;span class="n"&gt;adapter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CustomPagerAdapter&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// This should be your custom PagerAdapter&lt;/span&gt;
&lt;span class="n"&gt;customViewPager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAdapter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;setContentView&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customViewPager&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also get the current item, and set the current item programmatically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;currentItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;customViewPager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getCurrentItem&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;customViewPager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setCurrentItem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentItem&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;XML&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Alternatively, you can declare the CustomViewPager in your layout XML file and reference it in your activity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;com.example.yourapp.CustomViewPager&lt;/span&gt;
    &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/customViewPager"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in your activity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;CustomViewPager&lt;/span&gt; &lt;span class="n"&gt;customViewPager&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CustomViewPager&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;findViewById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;customViewPager&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;PagerAdapter&lt;/span&gt; &lt;span class="n"&gt;adapter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CustomPagerAdapter&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// This should be your custom PagerAdapter&lt;/span&gt;
&lt;span class="n"&gt;customViewPager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAdapter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember, in order for the &lt;code&gt;ViewPager&lt;/code&gt; to display anything, you will need to set a &lt;code&gt;PagerAdapter&lt;/code&gt; to it that supplies the views representing each page.&lt;/p&gt;

&lt;p&gt;Also, please replace &lt;code&gt;com.example.yourapp&lt;/code&gt; with your actual package name in XML usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Cases
&lt;/h2&gt;

&lt;p&gt;The custom ViewPager is perfect for applications where users prefer minimal hand movement or find swiping through pages inconvenient. It can also be beneficial for applications targeting users with accessibility needs, as the single-tap navigation may be easier for them to use. For example, in a digital book reading app, this would allow users to navigate between pages with a single tap, mimicking the act of flipping a book's page on either side.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Enhancing the user experience is a crucial aspect of Android development. By customizing a ViewPager to respond to single-tap events, we can make navigation through an app's pages easier and more intuitive. While this post focused on single-tap events, Android's GestureDetector offers a wide range of gesture detection capabilities that developers can leverage to build engaging and user-friendly interfaces.&lt;/p&gt;

&lt;h2&gt;
  
  
  Documentation References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/reference/androidx/viewpager/widget/ViewPager"&gt;ViewPager | Android Developers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/reference/android/view/GestureDetector"&gt;GestureDetector | Android Developers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/reference/android/view/MotionEvent"&gt;MotionEvent | Android Developers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/guide/topics/ui/custom-components"&gt;Building a Custom Component | Android Developers&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Remember, the code is just a tool to serve your user's needs. By understanding your users and tailoring the code to their preferences, you can build an application that truly stands out. Happy coding!&lt;/p&gt;

&lt;h2&gt;
  
  
  More
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/recyclerview-last-item-extra-bottom-margin/"&gt;RecyclerView Last Item Extra Bottom Margin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/making-android-views-draggable-a-practical-guide/"&gt;Making Android Views Draggable: A Practical Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/hide-the-soft-keyboard-and-remove-focus-from-edittext-in-android/"&gt;Hide the Soft Keyboard and Remove Focus from EditText in Android&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/building-a-visual-password-strength-meter-in-android-a-simple-guide/"&gt;Building a Visual Password Strength Meter in Android: A Simple Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/android-searchview-oncloselistener-not-working-here-is-how-to-fix-it/"&gt;Android SearchView OnCloseListener Not Working? Here’s How to Fix It&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/design/cardview-vs-materialcardview/"&gt;CardView vs. MaterialCardView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/news/android-news/android-13s-restricted-setting-a-stronghold-against-malicious-apps/"&gt;Android 13’s Restricted Setting: A Stronghold Against Malicious Apps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/android-studio/boost-your-android-development-with-adb-idea-plugin/"&gt;Boost Your Android Development with ADB Idea Plugin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="https://blog.rejowan.com/qa/android-qa/troubleshooting-execution-failed-for-task-appmergedebugresources-error-in-android-xml-files/"&gt;Troubleshooting “Execution Failed for Task app:mergeDebugResources” Error in Android XML Files&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/series/android-basics/android-activity-lifecycle-a-comprehensive-guide/"&gt;Android Activity Lifecycle: A Comprehensive Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>android</category>
      <category>ux</category>
      <category>viewpager</category>
      <category>ui</category>
    </item>
    <item>
      <title>Building a Visual Password Strength Meter in Android: A Simple Guide</title>
      <dc:creator>K M Rejowan Ahmmed</dc:creator>
      <pubDate>Mon, 05 Jun 2023 17:16:48 +0000</pubDate>
      <link>https://dev.to/ahmmedrejowan/building-a-visual-password-strength-meter-in-android-a-simple-guide-m55</link>
      <guid>https://dev.to/ahmmedrejowan/building-a-visual-password-strength-meter-in-android-a-simple-guide-m55</guid>
      <description>&lt;p&gt;Creating an interactive and user-friendly interface is crucial for ensuring user engagement. One such tool that significantly boosts user experience is a password strength indicator. Today, we're going to break down the creation of a simple yet effective password strength meter implemented in Android using the Java programming language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Breakdown
&lt;/h2&gt;

&lt;p&gt;Let's first review the entire code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PasswordStrengthIndicator&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;passwordStrength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// range 0 - 10&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Paint&lt;/span&gt; &lt;span class="n"&gt;filledPaint&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Paint&lt;/span&gt; &lt;span class="n"&gt;unfilledPaint&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;PasswordStrengthIndicator&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AttributeSet&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;filledPaint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Paint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Paint&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ANTI_ALIAS_FLAG&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;unfilledPaint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Paint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Paint&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ANTI_ALIAS_FLAG&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;unfilledPaint&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setColor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ContextCompat&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getColor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;grey&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Color of unfilled dots&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;onDraw&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Canvas&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;onDraw&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getWidth&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getHeight&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;dotWidth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Adjusted to allow space for gaps&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;dotHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dotWidth&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;getResources&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getDisplayMetrics&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;density&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 5dp to px&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;totalDotWidth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;dotWidth&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;dotWidth&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// total width of all dots and spaces&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;totalDotWidth&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the starting x value to center the dots&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dotWidth&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
            &lt;span class="nc"&gt;RectF&lt;/span&gt; &lt;span class="n"&gt;rectF&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RectF&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;dotHeight&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dotWidth&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dotHeight&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;passwordStrength&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passwordStrength&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;filledPaint&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setColor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ContextCompat&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getColor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getContext&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;red&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passwordStrength&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;filledPaint&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setColor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ContextCompat&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getColor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getContext&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;yellow&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;filledPaint&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setColor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ContextCompat&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getColor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getContext&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;green&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
                &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;drawRoundRect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rectF&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filledPaint&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;drawRoundRect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rectF&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unfilledPaint&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setPasswordStrength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;passwordStrength&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;passwordStrength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;passwordStrength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;invalidate&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The class &lt;code&gt;PasswordStrengthIndicator&lt;/code&gt; extends &lt;code&gt;View&lt;/code&gt;, the base class for widgets in Android, which allows it to be used in your UI layouts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initializing Paint Objects
&lt;/h3&gt;

&lt;p&gt;In the constructor, we initialize two &lt;code&gt;Paint&lt;/code&gt; objects. The &lt;code&gt;Paint&lt;/code&gt; class holds style and color information about how to draw geometries, text, and bitmaps. In our case, we use two separate instances: &lt;code&gt;filledPaint&lt;/code&gt; for filled dots and &lt;code&gt;unfilledPaint&lt;/code&gt; for unfilled ones.&lt;/p&gt;

&lt;h3&gt;
  
  
  Drawing on Canvas
&lt;/h3&gt;

&lt;p&gt;The drawing takes place in the &lt;code&gt;onDraw(Canvas canvas)&lt;/code&gt; method. We first determine the width and height of our widget and the size of the dots that represent the password strength.&lt;/p&gt;

&lt;p&gt;We calculate the total width required for the dots and the gaps between them and calculate a starting point to center them. We then loop through 10 times (our password strength range is 0 - 10), drawing either a filled or unfilled dot depending on the current password strength.&lt;/p&gt;

&lt;p&gt;The color of the filled dots changes based on the password strength:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strength less than or equal to 3: Red&lt;/li&gt;
&lt;li&gt;Strength between 4 and 7: Yellow&lt;/li&gt;
&lt;li&gt;Strength greater than 7: Green&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Updating the Password Strength
&lt;/h3&gt;

&lt;p&gt;Finally, we have a public method &lt;code&gt;setPasswordStrength(int passwordStrength)&lt;/code&gt;. This method sets the password strength and calls &lt;code&gt;invalidate()&lt;/code&gt;. The &lt;code&gt;invalidate()&lt;/code&gt; method tells the system that this view should be redrawn, causing the &lt;code&gt;onDraw&lt;/code&gt; method to be called again.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does This Work?
&lt;/h2&gt;

&lt;p&gt;This password strength indicator is purely a visual representation. You must create a mechanism to calculate the actual strength of the password entered by the user. The calculation can be based on the inclusion of uppercase letters, lowercase letters, digits, special characters, and the overall length of the password.&lt;/p&gt;

&lt;p&gt;Whenever the password input changes, call &lt;code&gt;setPasswordStrength(int passwordStrength)&lt;/code&gt; on this view with the new strength. The indicator will update automatically, providing real-time feedback to the user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Customizations
&lt;/h2&gt;

&lt;p&gt;The customization possibilities are endless. You could modify the colors to better fit your app's theme. Moreover, you could adjust the dot size and radius to best suit your needs. You might also consider creating a gradient password strength meter for a more visually pleasing indicator.&lt;/p&gt;

&lt;h2&gt;
  
  
  Color Resources
&lt;/h2&gt;

&lt;p&gt;In the provided code, we refer to color resources like &lt;code&gt;R.color.red&lt;/code&gt;, &lt;code&gt;R.color.yellow&lt;/code&gt;, and &lt;code&gt;R.color.green&lt;/code&gt;. These color resources should be defined in your project's &lt;code&gt;res/values/colors.xml&lt;/code&gt; file. You can learn more about color resources in the Android Developer's guide to &lt;a href="https://developer.android.com/guide/topics/resources/more-resources#Color"&gt;More Resource Types&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use It in Your Layout
&lt;/h2&gt;

&lt;p&gt;To utilize the &lt;code&gt;PasswordStrengthIndicator&lt;/code&gt; in your Android layout, use the XML code snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;com.yourPackage.PasswordStrengthIndicator&lt;/span&gt;
    &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/password_strength_indicator"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"30dp"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_gravity=&lt;/span&gt;&lt;span class="s"&gt;"center"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;com.yourPackage&lt;/code&gt; with the actual package name where you have placed the &lt;code&gt;PasswordStrengthIndicator&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;And in java, call using this way to set strength according to your needs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;PasswordStrengthIndicator&lt;/span&gt; &lt;span class="n"&gt;passwordStrengthIndicator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;findViewById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;password_strength_indicator&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;passwordStrengthIndicator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setPasswordStrength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Lastly
&lt;/h2&gt;

&lt;p&gt;I hope this post has given you a thorough understanding of how to create a visually appealing and interactive password strength indicator. With this simple guide, you can create your very own password strength meter and customize it according to your app's requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  More
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/navigating-pages-with-ease-tap-left-right-a-custom-viewpager-in-android/"&gt;Navigating Pages with Ease: Tap Left/Right — A Custom ViewPager in Android&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/recyclerview-last-item-extra-bottom-margin/"&gt;RecyclerView Last Item Extra Bottom Margin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/making-android-views-draggable-a-practical-guide/"&gt;Making Android Views Draggable: A Practical Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/hide-the-soft-keyboard-and-remove-focus-from-edittext-in-android/"&gt;Hide the Soft Keyboard and Remove Focus from EditText in Android&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/android-searchview-oncloselistener-not-working-here-is-how-to-fix-it/"&gt;Android SearchView OnCloseListener Not Working? Here’s How to Fix It&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/design/cardview-vs-materialcardview/"&gt;CardView vs. MaterialCardView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/news/android-news/android-13s-restricted-setting-a-stronghold-against-malicious-apps/"&gt;Android 13’s Restricted Setting: A Stronghold Against Malicious Apps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/android-studio/boost-your-android-development-with-adb-idea-plugin/"&gt;Boost Your Android Development with ADB Idea Plugin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="https://blog.rejowan.com/qa/android-qa/troubleshooting-execution-failed-for-task-appmergedebugresources-error-in-android-xml-files/"&gt;Troubleshooting “Execution Failed for Task app:mergeDebugResources” Error in Android XML Files&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/series/android-basics/android-activity-lifecycle-a-comprehensive-guide/"&gt;Android Activity Lifecycle: A Comprehensive Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>android</category>
      <category>password</category>
      <category>ui</category>
    </item>
    <item>
      <title>RecyclerView Last Item Extra Bottom Margin</title>
      <dc:creator>K M Rejowan Ahmmed</dc:creator>
      <pubDate>Fri, 02 Jun 2023 20:34:26 +0000</pubDate>
      <link>https://dev.to/ahmmedrejowan/recyclerview-last-item-extra-bottom-margin-1pi5</link>
      <guid>https://dev.to/ahmmedrejowan/recyclerview-last-item-extra-bottom-margin-1pi5</guid>
      <description>&lt;p&gt;&lt;strong&gt;RecyclerView&lt;/strong&gt; has been an everyday used item in Android App Development. During our use case, we often need some extra twists with the existing RecyclerView. One of them is to have some extra bottom margin for the last element of RecyclerView. Today, we'll try a good solution for that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;A good way to make this possible is to use LayoutParams in onBindViewHolder in the adapter class. We can set params for the last item and params for the rest of the items. The last item will have an extra bottom margin to achieve the goal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;onBindViewHolder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@NonNull&lt;/span&gt; &lt;span class="nc"&gt;ViewHolder&lt;/span&gt; &lt;span class="n"&gt;holder&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;RecyclerView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;LayoutParams&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RecyclerView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;LayoutParams&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;holder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;itemView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getLayoutParams&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;bottomMargin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// last item bottom margin&lt;/span&gt;
            &lt;span class="n"&gt;holder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;itemView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setLayoutParams&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;RecyclerView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;LayoutParams&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RecyclerView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;LayoutParams&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;holder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;itemView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getLayoutParams&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;bottomMargin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// other items bottom margin&lt;/span&gt;
            &lt;span class="n"&gt;holder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;itemView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setLayoutParams&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// all other codes&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;by &lt;code&gt;position == last.size() - 1&lt;/code&gt; we find out the last item of the list. And with the LayoutParams we set the bottom margin for the last item. And in the else section, we set the bottom margin for the rest of the items.&lt;br&gt;
Now you know how to add an extra bottom margin to the last item to RecyclerView.&lt;/p&gt;

&lt;p&gt;Find me on - &lt;a href="https://github.com/ahmmedrejowan"&gt;Github&lt;/a&gt;, &lt;a href="https://www.facebook.com/ahmmedrejowan"&gt;Facebook&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  More
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/navigating-pages-with-ease-tap-left-right-a-custom-viewpager-in-android/"&gt;Navigating Pages with Ease: Tap Left/Right — A Custom ViewPager in Android&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/making-android-views-draggable-a-practical-guide/"&gt;Making Android Views Draggable: A Practical Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/hide-the-soft-keyboard-and-remove-focus-from-edittext-in-android/"&gt;Hide the Soft Keyboard and Remove Focus from EditText in Android&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/building-a-visual-password-strength-meter-in-android-a-simple-guide/"&gt;Building a Visual Password Strength Meter in Android: A Simple Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/code/java/android-searchview-oncloselistener-not-working-here-is-how-to-fix-it/"&gt;Android SearchView OnCloseListener Not Working? Here’s How to Fix It&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/design/cardview-vs-materialcardview/"&gt;CardView vs. MaterialCardView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/news/android-news/android-13s-restricted-setting-a-stronghold-against-malicious-apps/"&gt;Android 13’s Restricted Setting: A Stronghold Against Malicious Apps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/android-studio/boost-your-android-development-with-adb-idea-plugin/"&gt;Boost Your Android Development with ADB Idea Plugin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="https://blog.rejowan.com/qa/android-qa/troubleshooting-execution-failed-for-task-appmergedebugresources-error-in-android-xml-files/"&gt;Troubleshooting “Execution Failed for Task app:mergeDebugResources” Error in Android XML Files&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rejowan.com/tutorial/series/android-basics/android-activity-lifecycle-a-comprehensive-guide/"&gt;Android Activity Lifecycle: A Comprehensive Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>android</category>
      <category>recyclerview</category>
      <category>java</category>
    </item>
    <item>
      <title>Making Android Views Draggable: A Practical Guide</title>
      <dc:creator>K M Rejowan Ahmmed</dc:creator>
      <pubDate>Fri, 02 Jun 2023 20:25:14 +0000</pubDate>
      <link>https://dev.to/ahmmedrejowan/making-android-views-draggable-a-practical-guide-44fo</link>
      <guid>https://dev.to/ahmmedrejowan/making-android-views-draggable-a-practical-guide-44fo</guid>
      <description>&lt;p&gt;In Android, views are fundamental building blocks of UI (User Interface). They define components like buttons, labels, text fields, image views, etc. Typically, these views have fixed positions as defined in layout files or programmatically. &lt;strong&gt;But what if we want to make these views movable or draggable by the user?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This post focuses on how to make any view in Android draggable. We will be working with Floating Action Button, ImageView, and LottieAnimation View, converting these into custom views using a reference code snippet. This feature can significantly enhance user interactivity, offering a more dynamic UI experience.&lt;/p&gt;

&lt;p&gt;We will leverage &lt;strong&gt;Android's onTouch listener&lt;/strong&gt; to detect &lt;strong&gt;touch events&lt;/strong&gt;, calculate the new position, and then &lt;strong&gt;reposition the view based on the user's drag&lt;/strong&gt;. The reference code we're using here illustrates the logic using a Floating Action Button (FAB), a common element that could benefit from this feature.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Reference Code
&lt;/h2&gt;

&lt;p&gt;Here's the original Java code that we will modify for our purpose. This code shows how to make a FloatingActionButton draggable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.content.Context&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.util.AttributeSet&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.view.MotionEvent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.view.View&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.view.ViewGroup&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.google.android.material.floatingactionbutton.FloatingActionButton&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MovableFloatingActionButton&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;FloatingActionButton&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;OnTouchListener&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="no"&gt;CLICK_DRAG_TOLERANCE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;downRawX&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;downRawY&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;dX&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dY&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MovableFloatingActionButton&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MovableFloatingActionButton&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AttributeSet&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MovableFloatingActionButton&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AttributeSet&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;defStyleAttr&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;defStyleAttr&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;setOnTouchListener&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;onTouch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;MotionEvent&lt;/span&gt; &lt;span class="n"&gt;motionEvent&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;

        &lt;span class="nc"&gt;ViewGroup&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MarginLayoutParams&lt;/span&gt; &lt;span class="n"&gt;layoutParams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ViewGroup&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MarginLayoutParams&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getLayoutParams&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;motionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAction&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;MotionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ACTION_DOWN&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

            &lt;span class="n"&gt;downRawX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;motionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRawX&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;downRawY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;motionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRawY&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;dX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getX&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;downRawX&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;dY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getY&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;downRawY&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Consumed&lt;/span&gt;

        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;MotionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ACTION_MOVE&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;viewWidth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getWidth&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;viewHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getHeight&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="n"&gt;viewParent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getParent&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;parentWidth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewParent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getWidth&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;parentHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewParent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getHeight&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;newX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;motionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRawX&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dX&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;newX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;layoutParams&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;leftMargin&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newX&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Don't allow the FAB past the left hand side of the parent&lt;/span&gt;
            &lt;span class="n"&gt;newX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parentWidth&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;viewWidth&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;layoutParams&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;rightMargin&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newX&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Don't allow the FAB past the right hand side of the parent&lt;/span&gt;

            &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;newY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;motionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRawY&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dY&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;newY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;layoutParams&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;topMargin&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newY&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Don't allow the FAB past the top of the parent&lt;/span&gt;
            &lt;span class="n"&gt;newY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parentHeight&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;viewHeight&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;layoutParams&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;bottomMargin&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newY&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Don't allow the FAB past the bottom of the parent&lt;/span&gt;

            &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;animate&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newX&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newY&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setDuration&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Consumed&lt;/span&gt;

        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;MotionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ACTION_UP&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

            &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;upRawX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;motionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRawX&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;upRawY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;motionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRawY&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;upDX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;upRawX&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;downRawX&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;upDY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;upRawY&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;downRawY&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;abs&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upDX&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;CLICK_DRAG_TOLERANCE&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;abs&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upDY&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;CLICK_DRAG_TOLERANCE&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// A click&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;performClick&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// A drag&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Consumed&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;onTouchEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;motionEvent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Breaking Down the Code
&lt;/h2&gt;

&lt;p&gt;The above class extends the &lt;strong&gt;FloatingActionButton&lt;/strong&gt; and implements the View.OnTouchListener interface. The OnTouchListener interface is crucial here, as it allows us to handle touch events.&lt;/p&gt;

&lt;p&gt;In the onTouch method, we handle different touch events:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ACTION_DOWN:&lt;/strong&gt; This is when the user first touches the view. We record the position where the user touched down.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ACTION_MOVE:&lt;/strong&gt; This is when the user moves their finger while still touching the screen. We calculate the new X and Y coordinates for the view, ensuring it doesn't move past the parent's edges.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ACTION_UP:&lt;/strong&gt; This is when the user lifts their finger from the screen. Here, we handle both click and drag actions. If the movement was small enough, we consider it a click and call performClick(). Otherwise, it's a drag event, and we simply consume the event.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making Other Views Draggable
&lt;/h2&gt;

&lt;p&gt;We can apply the same concept to make other views draggable. The key change is to extend the view you want to make draggable. For example, to make an &lt;strong&gt;ImageView&lt;/strong&gt; draggable, you'd use &lt;code&gt;public class DraggableImageView extends ImageView implements View.OnTouchListener&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, for a &lt;strong&gt;LottieAnimationView&lt;/strong&gt;, &lt;code&gt;public class DraggableLottieAnimationView extends LottieAnimationView implements View.OnTouchListener&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Remember to replace any &lt;code&gt;FloatingActionButton&lt;/code&gt; specific code with code applicable to your view. The main concept remains the same - implement the OnTouchListener, handle the different touch events, and update the view's position during a move event.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By implementing the View.OnTouchListener and handling different touch events, you can make any Android view draggable, significantly improving user interactivity. Whether it's a FloatingActionButton, ImageView, BottomNavigationView, or a LottieAnimationView, the logic remains consistent. The sky is the limit!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/reference/android/view/View.OnTouchListener"&gt;Android Developers Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/guide/topics/ui/ui-events"&gt;Handling Touches&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://material.io/components/buttons-floating-action-button"&gt;Floating Action Button&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://airbnb.io/lottie/#/android"&gt;Lottie Animation View&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/reference/com/google/android/material/bottomnavigation/BottomNavigationView"&gt;Bottom Navigation View&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>android</category>
      <category>draggable</category>
      <category>java</category>
      <category>ui</category>
    </item>
    <item>
      <title>Hide the Soft Keyboard and Remove Focus from EditText in Android</title>
      <dc:creator>K M Rejowan Ahmmed</dc:creator>
      <pubDate>Wed, 31 May 2023 11:44:53 +0000</pubDate>
      <link>https://dev.to/ahmmedrejowan/hide-the-soft-keyboard-and-remove-focus-from-edittext-in-android-ehp</link>
      <guid>https://dev.to/ahmmedrejowan/hide-the-soft-keyboard-and-remove-focus-from-edittext-in-android-ehp</guid>
      <description>&lt;p&gt;Hello &lt;strong&gt;Android Developers&lt;/strong&gt;,&lt;/p&gt;

&lt;p&gt;If you're reading this, chances are you're looking for a way to improve your app's user experience by managing the visibility of the soft keyboard and the focus of EditText views more effectively. You might be wondering how to automatically hide the keyboard and remove focus from an EditText (or its subclass) when a user clicks elsewhere in your app. In this post, we'll go through a simple solution for this common situation.&lt;/p&gt;




&lt;h2&gt;
  
  
  The dispatchTouchEvent() Method
&lt;/h2&gt;

&lt;p&gt;Before diving into the solution, let's introduce dispatchTouchEvent(). This is an Android Activity class method called whenever a touch event is dispatched (i.e., when a user interacts with the touch screen). The touch event contains key information about the interaction, such as the location and type of touch (e.g., down, move, up).&lt;/p&gt;




&lt;h3&gt;
  
  
  The Solution Explained
&lt;/h3&gt;

&lt;p&gt;Here's the code snippet that handles this UX enhancement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Override&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;dispatchTouchEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MotionEvent&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAction&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;MotionEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ACTION_DOWN&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getCurrentFocus&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;EditText&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Rect&lt;/span&gt; &lt;span class="n"&gt;outRect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Rect&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getGlobalVisibleRect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outRect&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;outRect&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRawX&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRawY&lt;/span&gt;&lt;span class="o"&gt;()))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;clearFocus&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="nc"&gt;InputMethodManager&lt;/span&gt; &lt;span class="n"&gt;imm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InputMethodManager&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;getSystemService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;INPUT_METHOD_SERVICE&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;imm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hideSoftInputFromWindow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getWindowToken&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;dispatchTouchEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Let's break this down:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Checking for ACTION_DOWN:&lt;/strong&gt; We're interested in the 'down' event, which signifies the start of a touch (i.e., the user's finger first makes contact with the screen). We check for this event with if (event.getAction() == MotionEvent.ACTION_DOWN).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Identifying the Current Focus:&lt;/strong&gt; The getCurrentFocus() function is used to identify the view currently in focus - this is usually the view the user is or has last interacted with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verifying if it's an EditText:&lt;/strong&gt; The if (v instanceof EditText) statement checks if the currently focused view is an instance of EditText. We want to ensure the following operations (like hiding the keyboard) only happen when an EditText loses focus.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Defining the Touch Boundary&lt;/strong&gt;: We create a Rect object (representing a rectangle) to denote the visible area of the EditText view on the screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Detecting a Touch Outside EditText:&lt;/strong&gt; We then check if the touch event is within this rectangle. If it's not, it means that the user has touched outside of the EditText view.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Removing Focus and Hiding Keyboard:&lt;/strong&gt; If the touch is detected outside the EditText, we clear the focus from the EditText and fetch the Input Method Manager (a system service that deals with the soft keyboard). We use this to hide the keyboard.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passing on the Event:&lt;/strong&gt; Finally, we pass the touch event on to the super class's dispatchTouchEvent(). This is crucial as the touch event may need further processing by other views or the system.&lt;/p&gt;




&lt;h2&gt;
  
  
  Working with TextInputEditText
&lt;/h2&gt;

&lt;p&gt;For those wondering about TextInputEditText (a subclass of EditText), fear not! This code works perfectly well for TextInputEditText views as well. If you wish to specifically apply this functionality to TextInputEditText views, you can replace if (v instanceof EditText) with if (v instanceof TextInputEditText).&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;This simple yet effective method can drastically improve the user experience of your Android app, particularly when working with forms or text inputs. Remember, a seamless user experience often leads to higher user satisfaction.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Keep coding and enhancing!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>android</category>
      <category>java</category>
      <category>ux</category>
    </item>
    <item>
      <title>Android SearchView.OnCloseListener Not Working? Here is how to fix it.</title>
      <dc:creator>K M Rejowan Ahmmed</dc:creator>
      <pubDate>Mon, 27 Feb 2023 05:25:17 +0000</pubDate>
      <link>https://dev.to/ahmmedrejowan/android-searchviewoncloselistener-not-working-here-is-how-to-fix-it-2i8g</link>
      <guid>https://dev.to/ahmmedrejowan/android-searchviewoncloselistener-not-working-here-is-how-to-fix-it-2i8g</guid>
      <description>&lt;p&gt;I was working on an android app project which required a &lt;strong&gt;SearchView&lt;/strong&gt;. While working on the &lt;strong&gt;SearchView&lt;/strong&gt;, I discovered that the method &lt;strong&gt;setOncloseListener&lt;/strong&gt; is not working for the &lt;strong&gt;SearchView&lt;/strong&gt;. I did some research and found that a lot of people like are having the same issues. After some testing I found a solution to make the close icon click listener or the &lt;strong&gt;OnCloseListener&lt;/strong&gt; for &lt;strong&gt;SearchView&lt;/strong&gt; work. It is not ideal, but here we go.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4rrcsvvlhp75wys1j57x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4rrcsvvlhp75wys1j57x.png" alt="SearchView"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh6cnvjc52mkvx909boj9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh6cnvjc52mkvx909boj9.png" alt="SearchView with Close Icon"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SearchViewSearchView&lt;/strong&gt; with Close &lt;strong&gt;IconBefore&lt;/strong&gt; going further here are some primary details,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I'm using androidx.appcompat.widget.SearchView in a layout.&lt;/li&gt;
&lt;li&gt;I needed the method to clear queries from searchView, remove focus and finally hide the soft keyboard.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;In your java code add these lines to make the magic work:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 java
        SearchView searchView = findViewById(R.id.search_view);

        int searchCloseButtonId = searchView.findViewById(androidx.appcompat.R.id.search_close_btn).getId();
        ImageView closeButton = searchView.findViewById(searchCloseButtonId);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchView.setQuery("", false);
                searchView.clearFocus();
                binding.searchResultLayout.setVisibility(GONE);
                hideKeyboard();
            }
        });


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;for &lt;strong&gt;hideKeyboard&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 java
        private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(binding.searchView.getWindowToken(), 0);
        }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And that's it. Now you can make your &lt;code&gt;searchView&lt;/code&gt; close icon work as you like.&lt;/p&gt;

&lt;h3&gt;
  
  
  How it's working?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;SearchView&lt;/strong&gt; has IDs for its own components, in this case, the close icon. In the code have &lt;code&gt;searchView.findViewById(androidx.appcompat.R.id.search_close_btn).getId()&lt;/code&gt; which calls the id of the close image from &lt;strong&gt;searchView&lt;/strong&gt; itself. &lt;br&gt;
Later we added this id to an &lt;strong&gt;ImageView&lt;/strong&gt; by using &lt;code&gt;searchView.findViewById(searchCloseButtonId)&lt;/code&gt; this. And then just setting on click listener for the ImageView and creating a method for &lt;strong&gt;hideKeyboard&lt;/strong&gt; from &lt;strong&gt;searchView windowToken&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, now you have a solution to fix problems with &lt;code&gt;searchView.setOnCloseListener&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Find me on - &lt;a href="https://github.com/ahmmedrejowan" rel="noopener noreferrer"&gt;Github&lt;/a&gt;, &lt;a href="https://www.facebook.com/ahmmedrejowan" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>java</category>
      <category>searchview</category>
      <category>onclicklistener</category>
    </item>
    <item>
      <title>CuteDialog - A Custom Material Design Dialog Library for Android</title>
      <dc:creator>K M Rejowan Ahmmed</dc:creator>
      <pubDate>Fri, 28 Oct 2022 14:00:07 +0000</pubDate>
      <link>https://dev.to/ahmmedrejowan/cutedialog-a-custom-material-design-dialog-library-for-android-4p2m</link>
      <guid>https://dev.to/ahmmedrejowan/cutedialog-a-custom-material-design-dialog-library-for-android-4p2m</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hi&lt;/strong&gt;,&lt;br&gt;
Today I'm sharing a &lt;strong&gt;Custom Material Design Dialog Library&lt;/strong&gt; named &lt;strong&gt;CuteDialog&lt;/strong&gt; created by &lt;strong&gt;CuteLibs Project&lt;/strong&gt; . I'm a co-author of this repo and tried my best to make a &lt;strong&gt;good&lt;/strong&gt; and &lt;strong&gt;reliable&lt;/strong&gt; library for the community.&lt;/p&gt;
&lt;h2&gt;
  
  
  CuteDialog
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zLyDVNpp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/CuteLibs/CuteDialog/raw/master/files/feature.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zLyDVNpp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/CuteLibs/CuteDialog/raw/master/files/feature.jpg" alt="feature" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;As mentioned on the repo page-&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CuteDialog is a Highly Customizable Material Design Android Library. CuteDialog allows developer to create beautiful dialogs with material design. It is highly customizable and can be used for lots of different purposes.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Screenshot
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Icon&lt;/th&gt;
&lt;th&gt;Image&lt;/th&gt;
&lt;th&gt;Animation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YbYJwXwu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/CuteLibs/CuteDialog/raw/master/files/shot1.png" alt="icon" width="417" height="844"&gt;&lt;/td&gt;
&lt;td&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WFvsDfte--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/CuteLibs/CuteDialog/raw/master/files/shot2.png" alt="image" width="417" height="844"&gt;&lt;/td&gt;
&lt;td&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--87jxjzIx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://github.com/CuteLibs/CuteDialog/raw/master/files/shot3.gif" alt="animation" width="417" height="844"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Add this to your root build.gradle&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Theme
&lt;/h2&gt;

&lt;p&gt;You must use any MaterialComponents Theme. Example-&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Basic Usage
&lt;/h2&gt;


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


&lt;h2&gt;
  
  
  Advanced
&lt;/h2&gt;


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


&lt;h2&gt;
  
  
  Customizations
&lt;/h2&gt;


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


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_DSKJXS---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/CuteLibs/CuteDialog/raw/master/files/collage.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_DSKJXS---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/CuteLibs/CuteDialog/raw/master/files/collage.jpg" alt="collage" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;( Visit &lt;a href="https://github.com/CuteLibs/CuteDialog/wiki"&gt;Wiki&lt;/a&gt; for more ) &lt;/p&gt;

&lt;h2&gt;
  
  
  More
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/CuteLibs/CuteDialog"&gt;Check CuteDialog on Github&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/CuteLibs"&gt;Check CuteLibs Project&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for Reading. Have a good day.&lt;/p&gt;

&lt;p&gt;Find me on - &lt;a href="https://github.com/ahmmedrejowan"&gt;Github&lt;/a&gt;, &lt;a href="https://www.facebook.com/ahmmedrejowan"&gt;Facebook&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>CardView Vs MaterialCardView</title>
      <dc:creator>K M Rejowan Ahmmed</dc:creator>
      <pubDate>Thu, 27 Oct 2022 19:35:11 +0000</pubDate>
      <link>https://dev.to/ahmmedrejowan/cardview-vs-materialcardview-1epn</link>
      <guid>https://dev.to/ahmmedrejowan/cardview-vs-materialcardview-1epn</guid>
      <description>&lt;p&gt;As we're focusing more on design, &lt;strong&gt;Material Design&lt;/strong&gt; is a hot topic here. Google's Material Library ( &lt;em&gt;com.google.android.material&lt;/em&gt; ) created a huge impact in this area. Today I'm going to discuss one of the most used elements in the design, the &lt;strong&gt;CardView&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Google has introduced a new version of &lt;strong&gt;CardView&lt;/strong&gt; called &lt;strong&gt;MaterialCardView&lt;/strong&gt; . Both &lt;strong&gt;CardView&lt;/strong&gt; and &lt;strong&gt;MaterialCardView&lt;/strong&gt; could be used in the same cases and they look pretty similar. But there is still a vast difference in both design and functionality.&lt;/p&gt;

&lt;p&gt;Let's start with some background data about these twos-&lt;/p&gt;

&lt;h2&gt;
  
  
  CardView
&lt;/h2&gt;

&lt;p&gt;Previously, &lt;strong&gt;CardView&lt;/strong&gt; was provided by &lt;strong&gt;Android Support Library&lt;/strong&gt; (&lt;em&gt;android.support.v7.widget.CardView&lt;/em&gt;). But with the latest migration to &lt;strong&gt;Andoridx&lt;/strong&gt;, now it is provided by &lt;strong&gt;Androidx&lt;/strong&gt; (&lt;em&gt;androidx.cardview.widget.CardView&lt;/em&gt;) Library. &lt;strong&gt;CardView&lt;/strong&gt; is created by extending the &lt;strong&gt;FrameLayout&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  MaterialCardView
&lt;/h2&gt;

&lt;p&gt;This is introduced with Google Material Library (&lt;em&gt;com.google.android.material&lt;/em&gt;). It is provided by &lt;strong&gt;Material Components Library&lt;/strong&gt; ( &lt;em&gt;com.google.android.material.card.MaterialCardView&lt;/em&gt;). &lt;strong&gt;MaterialCardView&lt;/strong&gt; is created by extending the &lt;strong&gt;CardView&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Differences
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;MaterialCardView&lt;/strong&gt; has all the elements of &lt;strong&gt;CardView&lt;/strong&gt; with its extra features. Here is a list containing the differences-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Stroke Color&lt;/strong&gt; and &lt;strong&gt;Stroke Width&lt;/strong&gt; can be added to &lt;strong&gt;MaterialCardView&lt;/strong&gt; which wasn't possible using &lt;strong&gt;CardView&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MaterialCardView&lt;/strong&gt; is Checkable. Check icon, color, and position can be set in &lt;strong&gt;MaterialCardView&lt;/strong&gt;. Check conditions can be called also. This will be really useful as CardView has a huge usage in lists and grids.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ripple Color&lt;/strong&gt; can be set to MaterialCardView which is not available in CardView.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;CardView &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
MaterialCardView &lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Shots
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;CardView&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkq54nlhfbgbvkl0pdocf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkq54nlhfbgbvkl0pdocf.png" alt="CardView Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MaterialCardView&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ww1yipycp3tzxb7gfc2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ww1yipycp3tzxb7gfc2.png" alt="MaterialCardView Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Details
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/reference/androidx/cardview/widget/CardView" rel="noopener noreferrer"&gt;CardView docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/reference/com/google/android/material/card/MaterialCardView" rel="noopener noreferrer"&gt;MaterialCardView docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://m2.material.io/develop/android/components/cards/" rel="noopener noreferrer"&gt;MaterialCardView on material.io&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading. Have a good day.&lt;/p&gt;

&lt;p&gt;Find me on - &lt;a href="https://github.com/ahmmedrejowan" rel="noopener noreferrer"&gt;Github&lt;/a&gt;, &lt;a href="https://www.facebook.com/ahmmedrejowan" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More- &lt;a href="https://github.com/CuteLibs" rel="noopener noreferrer"&gt;CuteLibs Projects&lt;/a&gt;, &lt;a href="https://github.com/CuteLibs/CuteDialog" rel="noopener noreferrer"&gt;CuteDialog&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>material</category>
      <category>design</category>
    </item>
  </channel>
</rss>
