<?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: Samuel Ochuba</title>
    <description>The latest articles on DEV Community by Samuel Ochuba (@kolanse).</description>
    <link>https://dev.to/kolanse</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%2F475637%2Fca897410-aeda-4155-9fd2-c21cf9cfadaa.jpg</url>
      <title>DEV Community: Samuel Ochuba</title>
      <link>https://dev.to/kolanse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kolanse"/>
    <language>en</language>
    <item>
      <title>Your Users Will Do the Darnedest thing: Hedging Against That as a Mobile Developer.</title>
      <dc:creator>Samuel Ochuba</dc:creator>
      <pubDate>Wed, 30 Aug 2023 22:20:42 +0000</pubDate>
      <link>https://dev.to/kolanse/your-users-will-do-the-darnedest-thing-hedging-against-that-as-a-mobile-developer-1ejm</link>
      <guid>https://dev.to/kolanse/your-users-will-do-the-darnedest-thing-hedging-against-that-as-a-mobile-developer-1ejm</guid>
      <description>&lt;p&gt;As I sit on my bed, struck down with malaria, trying not to work and not to work at the same time, I am reminded of the consequences of underestimating mosquitoes. (For those who don't know, mosquitoes are the vectors of malaria.) This got me thinking that there is something else in the mobile world that is underestimated but is the biggest source of user-perceived bugs: bad UX. I decided to write a quick article on it and share my perspective.&lt;/p&gt;

&lt;p&gt;You resume work on a calm Monday morning. Less than two hours later, Slack is buzzing. Customer service is forwarding a common complaint from users about some strange bugs happening in the app. No one, including them and the QA team, seems to be able to reproduce the bug. It's a big conundrum. You try to replicate it, but you also hit the same snag. You decide to hope and pray that this issue goes away. But annoyingly, this problem seems to arise at intervals.&lt;/p&gt;

&lt;p&gt;I'm guessing this is a common situation in many companies. The reason is that you can never fully test the ways users will use your app. It will be used by thousands, if not millions, of consumers on different devices in different conditions. Your app will be pressed by angry people, busy people, sad people, happy people. It will be virtually impossible for you to test all the different flows that can happen when your app is used.&lt;/p&gt;

&lt;p&gt;This article is not meant to discourage you. It is just meant to share, to the best of my limited experience, things you could do to prevent strange bugs.&lt;/p&gt;

&lt;p&gt;The most important thing is to have robust validations. For each input field in your app, are you sure that the correct format is inputted before the data is sent for processing? Many mobile devs leave validations only for the login and signup phases of the app. However, it is important for it to be used everywhere your app allows your user to input data. Remember, validation without a clear message is not the correct way. A clear message telling the user why the input is wrong is better than no message at all. Also, using the correct input field for either numbers or emails is the proper way to go.&lt;/p&gt;

&lt;p&gt;Your users are impatient. Don't think that the user will just click that button and wait for its outcome. Also, for users with slow devices, the effect of clicking a button might not be immediately apparent. You want to make it impossible for the user to double-click a button until the outcome of the button click is returned. Either disable the button or cover the screen with a dialog. In all cases, you cannot prevent this. But you need to also be sure that double-clicking of that button does not cause an unexpected bug or a bug.&lt;/p&gt;

&lt;p&gt;Your users will pause what they are doing on the app and navigate to another app temporarily, then come back. Are you handling for that exact scenario? For Android developers, does Android's aggressive memory management cause unexpected bugs for you? Have you simulated what could happen in these scenarios? And can your app live with the consequences of Android's aggressiveness?&lt;/p&gt;

&lt;p&gt;Your users will not navigate the app the way you want them to. They will ignore that back button you provided for them and click on the system back button. Are you properly shepherding them to the page you want them to go to no matter where they navigate to?&lt;/p&gt;

&lt;p&gt;Who knows? I might make this a longer series with each one dedicated to a particular war story. Or it might go the way of the majority of my stories: forever in my drafts.&lt;/p&gt;

&lt;p&gt;shout out to &lt;a href="https://unsplash.com/@nublson?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopy"&gt;Nubison&lt;/a&gt; for the image &lt;/p&gt;

</description>
      <category>mobile</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Android Workmanager: Implementation, Tips and Pitfalls to Avoid (1)</title>
      <dc:creator>Samuel Ochuba</dc:creator>
      <pubDate>Tue, 07 Feb 2023 17:19:23 +0000</pubDate>
      <link>https://dev.to/kolanse/android-workmanager-implementation-tips-and-pitfalls-to-avoid-4lc9</link>
      <guid>https://dev.to/kolanse/android-workmanager-implementation-tips-and-pitfalls-to-avoid-4lc9</guid>
      <description>&lt;p&gt;For over a year i have been interacting with the android Workmanager Api . A very powerful api, I resolved to publish some articles around what I have been building and this is the first in the series. &lt;/p&gt;

&lt;p&gt;You have been presented with a situation. The business wants all interactions and flow happening in the app to happen offline and whenever the user's internet is turned on synchronise that data to the backend.  You do your research and you come across a powerful API in android called Workmanager and you start building.  The aim of this article is to give a short tutorial on workmanager as well as give tips on how to avoid potential catastrophic events that could lead to issues later on. &lt;/p&gt;

&lt;p&gt;Workmanager is fantastic when you want to schedule work to be done sometime in the future with some constraints. Take for example, in the example given above, the constraint is internet access. Workmanager gives you the ability to specify the conditions that should be met before your work begins.   &lt;/p&gt;

&lt;p&gt;Let us declare a workmanager class called &lt;code&gt;ScoresAverageWorker&lt;/code&gt; this class syncs all the attendance results taken by a teacher for their class. &lt;/p&gt;

&lt;p&gt;The first thing you want to do is to create the Workmanager Class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ScoresAverageWorker(
    context: Context,
    workerParams: WorkerParameters
) : Worker(context, workerParameters) {

override fun doWork(): Result {

if (work is successful) {

return Result.success()

} else {

 if (work needs to be retried) {
     // try again if there is a server error

 return Result.retry()
     }
 return Result.failure()
     }
     }
}

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

&lt;/div&gt;



&lt;p&gt;this class extends the Worker class and takes in two parameters, which are context and worker parameters.   Worker parameters refer to data you might need to do work. we will talk about it later, for now let us focus on other aspects.  &lt;/p&gt;

&lt;p&gt;The above class overrides a function called doWork and that is where the work we would like workmanager to perform gets done. &lt;a href="https://developer.android.com/reference/androidx/work/ListenableWorker.Result#Result()"&gt;This function returns a Class Result&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The three different methods you can return from the class are Result.success() , which indicates that the work was successful. Result.failure() which indicates that the work was not successful and Result.retry() which tells Workmanager to retry the work. &lt;/p&gt;

&lt;p&gt;Now that we are done setting up the workmanager class. we can now move ahead to figuring out how to instantiate it.   You can determine how periodically you want your work to occur, either once or at intervals. &lt;/p&gt;

&lt;p&gt;In this article, we would stick to only one-time work requests.  To start a one-time work, we need to first instantiate our workmanager class, then use OneTimeWorkRequestBuilder to build our request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val workManager = WorkManager.getInstance(this)

val scoresAverageWorker: OneTimeWorkRequest =
    OneTimeWorkRequestBuilder&amp;lt;ScoresAverageWorker&amp;gt;()
        .setInitialDelay(5, TimeUnit.SECONDS)
        .build()


workManager.beginUniqueWork("Unique_Work_Name", ExistingWorkPolicy.REPLACE, scoresAverageWorker).enqueue()

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

&lt;/div&gt;



&lt;p&gt;The above code instantiates a one-time work request builder, sets an initial delay of 5 seconds to it and begins a unique work. &lt;/p&gt;

&lt;p&gt;The second part of this series will focus on how to send data to our workmanager class and how to create a coroutine worker. &lt;/p&gt;

</description>
      <category>android</category>
      <category>workmanager</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Understanding OOP Concepts With Kotlin</title>
      <dc:creator>Samuel Ochuba</dc:creator>
      <pubDate>Tue, 06 Jul 2021 14:04:17 +0000</pubDate>
      <link>https://dev.to/kolanse/understanding-oop-concepts-with-kotlin-2kd8</link>
      <guid>https://dev.to/kolanse/understanding-oop-concepts-with-kotlin-2kd8</guid>
      <description>&lt;p&gt;You just finished learning variables in kotlin and you are wondering how do they come together to create complex amazing programs you have read about. This article will introduce you to the concepts of object oriented programming in a simplified way. We will use practical examples to explain how Complex methods are created. So let us dive right in &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/xUNda6q8X3vwXzNvA4/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/xUNda6q8X3vwXzNvA4/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First of all we need to understand what Object oriented programming(OOP) is. OOP relies on the concepts of classes and object. it is a way we use to structure our software program into simple pieces that can be used multiple times.&lt;/p&gt;

&lt;p&gt;This brings us to another question . What are classes? . A class is a blueprint that is used to create more specific objects. Let us take the case of an architect.&lt;/p&gt;

&lt;p&gt;An architect has a blueprint for how he wants a building to look like. From that blueprint multiple buildings can be built. Each of the building can have a unique name and properties, but they all share the same fundamental features&lt;/p&gt;

&lt;p&gt;Let us use a real life example to explain OOP concepts. Let us use the example of a school. Imagine you were awarded the contract to write an application for a school. The aim of the application is to make the school more productive.&lt;/p&gt;

&lt;p&gt;Your application will allow the principal admit students, a student can take multiple courses, a teacher can teach a course.. etc. &lt;/p&gt;

&lt;p&gt;Creating a program this complex would require a lot of code. Your school could have thousands of students and it would be terribly inefficient to write unique code for each of the students. We could create a class called student in kotlin &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class Student(){
}


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

&lt;/div&gt;

&lt;p&gt;Each of the student could have properties, which can include a name, age, class etc.. &lt;br&gt;
In kotlin properties of class can be declared in the primary constructor using this format &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class Student(name: String, age:String , class:String){
}


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

&lt;/div&gt;

&lt;p&gt;By creating a student class we can create multiple students. In this instance the multiple students are instances of the student class. i.e types of the student class. They are called objects. In kotlin objects are instantiated this way &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

var samuel = Student("samuel", 23, "Grade 5")


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

&lt;/div&gt;

&lt;p&gt;Samuel which is an object of the student class has three attributes. which are his name, age and Grade. &lt;/p&gt;

&lt;p&gt;Attributes are information associated with a class . sometimes it will be necessary for a student to perform some actions like take a course or take examinations. In kotlin methods are declared this way&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

fun takeCourse(course:String){
}


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

&lt;/div&gt;

&lt;p&gt;A method can take in information and perform an action. it can also return information&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

fun takeCourse(course:String): Boolean{

return true
}


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

&lt;/div&gt;

&lt;p&gt;Now let us explore the four principles of OOP&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;li&gt;Encapsulation&lt;/li&gt;
&lt;li&gt;Abstraction &lt;/li&gt;
&lt;li&gt;Polymorphism&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;Inheritance is a feature in OOP that allows classes to inherit from other classes. Inheritance supports reusability. it reduces the number of boilerplate code that needs to be written. Let us use the case of our school project. The different roles in a school. i.e teachers, principals, students. etc are all human beings. so they all need to have a name, age etc. &lt;/p&gt;

&lt;p&gt;if we were to use that model this scenario in kotlin we would need to first create a class person and make student, principal and staff class inherit from it. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

open class Person(name:String){
}

class Student(name:String): Person(name){
}


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

&lt;/div&gt;

&lt;p&gt;In the code snippet above class student inherits the name attribute from the Person class&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

open class Person(name:String){
open fun dance(){}
}

class Student(name:String): Person(name){
override fun dance() {  }
}


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

&lt;/div&gt;

&lt;p&gt;You can also override methods used in the parent class&lt;/p&gt;

&lt;h2&gt;
  
  
  Encapsulation
&lt;/h2&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%2Fgqmy1i52lj9gdgtth0o8.jpeg" 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%2Fgqmy1i52lj9gdgtth0o8.jpeg" alt="hide"&gt;&lt;/a&gt;&lt;br&gt;
Encapsulation is the art of hiding important information from other classes and exposing them only through a select method. Imagine the requirements of our app dictates that the principal should only be able to see the list of all students and add students to the list. we would have to look for a way to make that happen. That way is called encapsulation.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class Principal(){
 private var studentsName = mutableListOf&amp;lt;String&amp;gt;()


fun getStudentsName(): List&amp;lt;String&amp;gt;{
  return studentsName
}


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

&lt;/div&gt;

&lt;p&gt;The names of the students have been hidden and can only be gotten through the getStudentsName method. The only way it can be modified is only within the class.  We can achieve encapsulation through the use of &lt;a href="https://kotlinlang.org/docs/visibility-modifiers.html" rel="noopener noreferrer"&gt;visibility modifiers&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstraction
&lt;/h2&gt;

&lt;p&gt;Abstraction means that the user can only interact with select attributes and methods of an object. In our case if a principal admits applicants and makes them students. An outsider does not care how the principal goes about it. The other parts of the program would only want to get the students or pass the applicant to be converted to students. If there is a complex operation ongoing in the principal class that changes an applicant to student,  other parts of the program do not care about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polymorphism
&lt;/h2&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%2Fkpdkigk3yl9xz99iiiv3.jpeg" 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%2Fkpdkigk3yl9xz99iiiv3.jpeg" alt="Polymorphism"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This refers to the art of designing objects to share behaviour either through method overloading or method overriding.&lt;/p&gt;

&lt;p&gt;In kotlin these are achieved through the following syntax&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

fun admitApplicant(applicant: String){
}

fun admitApplicant(applicants: List&amp;lt;String&amp;gt;){
}


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

&lt;/div&gt;

&lt;p&gt;From the code both methods can be named similarly but the method that gets called is dependent on what you passed in as a parameter in this case one method takes a string while the other takes in a list of string. &lt;/p&gt;

&lt;p&gt;If all users in our school application can delete themselves from the database. That means in our Person class there would be a method called delete  &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

open class Person(name:String){
open fun delete(){}
}

class Student(name:String): Person(name){
override fun delete() { 
  //students can delete themselves from the database
 }
}


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

&lt;/div&gt;

&lt;p&gt;Learning object oriented programming is a continous process that never ends in the life of a developer. Each program you would write during the course of our career would require you to think deep on how best to implement it keeping OOP principles in mind.&lt;/p&gt;

&lt;p&gt;If you have any observations or questions you can reach out to me on twitter &lt;a href="https://twitter.com/rake_code" rel="noopener noreferrer"&gt;@rake_code&lt;/a&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>oop</category>
      <category>android</category>
      <category>mobile</category>
    </item>
    <item>
      <title>How to add a remote Github repository to local</title>
      <dc:creator>Samuel Ochuba</dc:creator>
      <pubDate>Thu, 01 Jul 2021 01:44:37 +0000</pubDate>
      <link>https://dev.to/kolanse/how-to-add-a-remote-github-repository-to-local-1ie5</link>
      <guid>https://dev.to/kolanse/how-to-add-a-remote-github-repository-to-local-1ie5</guid>
      <description>&lt;p&gt;You might have been given a task to accomplish using github classrooms or created a remote repository on github and you are wondering how to push your local project to the remote repository. In this article i will quickly show you how to accomplish that task. Sit tight. &lt;br&gt;
&lt;a href="https://i.giphy.com/media/pOJSTD1nNXUyN0xnwN/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/pOJSTD1nNXUyN0xnwN/giphy.gif" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the terminal of your ide . you will want to initialize the local directory as a git repository by using this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init -b main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command converts your local repository into a git repository. The next thing you would want to do is add the files you have in your local repository into your git repository by committing them.  Before committing the files you would first have to add them . The command for adding is below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding the files you then commit it to the local git repository by using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit -m "First commit"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First commit represents the commit message. it is usually a short title that is an overview of what you have done.  This local commit also prepares the files to be pushed to a remote repository. &lt;/p&gt;

&lt;p&gt;At the top of your github online repository setup page you would see the repository url . either HTTPS or ssh. depending on your organization's requirements&lt;/p&gt;

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

&lt;p&gt;You would need to add the url you have copied to your local github repository by using the below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote add origin  &amp;lt;REMOTE_URL&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then verify the remote url&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding the url to the local github repo. You can then push your changes to your online repo by using the below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; git push -u origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With main being the name of the branch online you are pushing your local changes to &lt;/p&gt;

&lt;p&gt;Thank you very much and i hope this article helps you with your first steps in github. I am always free to answer questions and help. you can check my profile out on twitter &lt;br&gt;
@rake_code&lt;/p&gt;

</description>
      <category>github</category>
    </item>
    <item>
      <title>You Could Write Simpler Code in Android: Monitoring Changed Text in An EditText Field</title>
      <dc:creator>Samuel Ochuba</dc:creator>
      <pubDate>Fri, 27 Nov 2020 01:57:11 +0000</pubDate>
      <link>https://dev.to/kolanse/you-could-write-simpler-code-in-android-monitoring-changed-text-in-an-edittext-field-1bl7</link>
      <guid>https://dev.to/kolanse/you-could-write-simpler-code-in-android-monitoring-changed-text-in-an-edittext-field-1bl7</guid>
      <description>&lt;p&gt;While working on my recent project, several times while trying to implement a feature I came across several articles online whose code advocated writing long lines of code. This could be as a result of the articles being written a long time ago(In android, a long time could be as short as six months). This informed my choice to create a series where I highlight simpler ways of writing code than the ones commonly available online when using ktx extensions.&lt;/p&gt;

&lt;p&gt;This article is going to be a short one just to illustrate how much fewer lines of code you could write. A particular scenario you might be faced with is getting the changing text when a user inputs value into an edittext field. The situation might also mean you want a particular event to occur anytime the text in the field changes. &lt;/p&gt;

&lt;p&gt;To accomplish this task, a cursory search on google might take you to a stackoverflow page where the top three answers may look like this one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fknyqmujep3wffgljxcsu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fknyqmujep3wffgljxcsu.png" alt="Alt Text" width="714" height="627"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or this one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faw0e3w958cga3c84urb2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faw0e3w958cga3c84urb2.png" alt="Alt Text" width="714" height="627"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After seeing these answers you get the feeling that you are writing a lot of code just to get the text that was changed or to be notified when the text changes. Don't worry I get the feeling. I had the same exact feeling&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/cdkQ2bLcwOE1cEzEeL/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/cdkQ2bLcwOE1cEzEeL/giphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I discovered that it could be accomplished with just three lines of code. A call to the doOnTextChanged was enough to do my bidding.&lt;/p&gt;


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


&lt;p&gt;My next article would be on Navigation drawer stay tuned&lt;/p&gt;

</description>
      <category>android</category>
      <category>androidlayout</category>
      <category>codenewbie</category>
      <category>ontextchanged</category>
    </item>
    <item>
      <title>I Just Created a spinner with Android Material Design. Let me share my pains with you</title>
      <dc:creator>Samuel Ochuba</dc:creator>
      <pubDate>Sat, 21 Nov 2020 18:03:12 +0000</pubDate>
      <link>https://dev.to/kolanse/i-just-created-a-spinner-with-android-material-design-let-me-share-my-pains-with-you-26n7</link>
      <guid>https://dev.to/kolanse/i-just-created-a-spinner-with-android-material-design-let-me-share-my-pains-with-you-26n7</guid>
      <description>&lt;p&gt;The task was to create a spinner item to display a list of banks for users to select. I decided to use the material design spinner to accomplish the task. This was the first time I would use material design spinner but I didn't worry about the limited timeline of my sprint because I expected it to be an easy thing to do, Considering I had used Material design components multiple times. My first point of call was google's material design  &lt;a href="https://material.io/" rel="noopener noreferrer"&gt;website&lt;/a&gt;. The result included progress bars, menu etc, but nothing on spinners. I was shocked that a very common UI component like spinner wasn't available in material design. &lt;/p&gt;

&lt;p&gt;Not wanting to believe that possibility, I decided to do a google search on how a material design spinner in android could be implemented. One of the very first articles I came across was hilarious and made my already souring mood to cheer up a little. if you are interested in a little laugh you can read the article &lt;a href="https://blog.usejournal.com/there-is-no-material-design-spinner-for-android-3261b7c77da8" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After reading the article i found out that google decided to call a spinner in android material design an Exposed Dropdown Menu. If not for that article I would never have found out or I would have done a considerable search before finding out what exactly a spinner is called in material design.&lt;/p&gt;

&lt;p&gt;Because Exposed Dropdown Menu isn't exactly a spinner, you are forced to think in terms of a spinner but code in terms of a TextInputLayout. Good luck finding relevant materials as a dev on material design spinner (it took me over 30 minutes to figure out how to get a user's selected item).&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%2Fi%2Futez4ve4pqrxx4ilazoy.jpeg" 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%2Fi%2Futez4ve4pqrxx4ilazoy.jpeg" alt="WhatsApp Image 2020-11-21 at 14.57.48"&gt;&lt;/a&gt;&lt;br&gt;
 All I Was Trying to Accomplish&lt;/p&gt;
&lt;h3&gt;
  
  
  Battle 1: Setting a background colour to the spinner
&lt;/h3&gt;

&lt;p&gt;Setting the background color in most UI components mostly involves just using the "android:background" attribute i.e. &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;android:background="@color/black"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;I was pleasantly suprised that it didn't work as intended i decided to do a little digging and found out that the appriopate attribute to use was &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; app:boxBackgroundColor="@color/black"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Battle 2: Setting a Stroke Color
&lt;/h2&gt;

&lt;p&gt;As an android developer especially a junior one or a beginner, Most of the times you get to customize an edittext field or button, It is usually done by creating a drawable xml file and setting the background of the view to the xml drawable. It doesn't work that way on a material design spinner aka Exposed Dropdown Menu. &lt;br&gt;
After trying out all the usual ways with ever-increasing frustration. I decided to take a chilled coke and think up another way I could do it. it is amazing what a chilled coke could do to the brain in terms of inducing a eureka moment. &lt;br&gt;
I decided to use themes to colour the stroke and unsurprisingly it worked. It just involved me creating a style like this &lt;br&gt;
 &lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Then i set the style of my TextInputLayout to: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       style="@style/spinnerTheme"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Battle 3: Setting the Spinner Dropdown Icon and Getting the Selected Item
&lt;/h2&gt;

&lt;p&gt;After winning the first two battles, I felt confident that the next battle would be the easiest of the three. I was wrong in that aspect. Setting the icon drawable meant I had to go through all the suggested properties android studio offered me before I found out that the endIcon attribute was the attribute I had to change. &lt;br&gt;
Once i found out it was just a matter of overriding it in my themes. &lt;br&gt;
My completed style is below: &lt;/p&gt;


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



&lt;p&gt;The path to finding out how to get the user-selected item took quite some time than i estimated.. over 15 minutes. In a normal android spinner, overriding onItemSelected Material design spinner ..coughs* ..  Exposed Dropdown Menu doesn't work like that.  After a little search, i found out that onTextChanged listener could work for it. But to me, it felt like an over kill since I was just looking to get the user selected bank. &lt;br&gt;
After a deeper search, I found out that AutoCompleteTextView was a child of EditText. Going along with that reasoning i hypothesized that the getText() method would work in getting the selected item. it worked!!. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   var bank = spinner.editText?.text.toString 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Though my solution seemed to work I am still anticipating bugs. &lt;/p&gt;

&lt;p&gt;I decided to post this article to add some material and help some other junior dev or beginner who might have the fortune to navigate the material design spinner maze. Articles on Material design spinner seemed scarce online.&lt;/p&gt;

</description>
      <category>android</category>
      <category>xml</category>
      <category>materialdesign</category>
    </item>
  </channel>
</rss>
